一、简介
基于matlab灰度+二值化空瓶检测
二、源代码
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 24-Nov-2019 17:11:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 载入车牌图像
% set(handles.text_result, 'string', '');
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
handles.file = [];
handles.Plate = [];
handles.bw = [];
handles.words = [];
[filename, pathname, filterindex] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' });
if filename == 0
return;
end
file = fullfile(pathname, filename);
Img = imread(file);
img=imread(file);
[y, ~, ~] = size(Img);
if y > 800
rate = 800/y;
Img1 = imresize(Img, rate);
else
Img1 = Img;
end
axes(handles.axes1);
imshow(Img1); title('原图像', 'FontWeight', 'Bold');
handles.Img = Img;
handles.img = img;
guidata(hObject, handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img1=rgb2gray(handles.Img);
axes(handles.axes2);
imshow(img1);
title('灰度图'); %灰度图
handles.img1 = img1;
handles.img = handles.Img;
guidata(hObject, handles);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t=graythresh(handles.img1); %灰度阈值
caise=~im2bw(handles.img1,t); %二值化
axes(handles.axes3);
imshow(caise);
title('二值化图'); %二值图
handles.caise = caise;
guidata(hObject, handles);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
caise=bwmorph(handles.caise,'close'); %闭运算
caise= medfilt2(caise,[3 3]); %3×3中值滤波
caise= medfilt2(caise,[5 5]); %5×5中值滤波
axes(handles.axes4);
imshow(caise, []);
title('去除噪声'); %去噪图
handles.caise = caise;
guidata(hObject, handles);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[L1] = bwlabel(handles.caise);
STATS= regionprops(L1,'Area');
S=cat(1,STATS.Area);
caise1 = bwareaopen(handles.caise,10000);
axes(handles.axes5);
imshow(caise1);
title('去掉瓶盖') %去掉瓶盖
handles.caise1 = caise1;
guidata(hObject, handles);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[L2,num] = bwlabel(handles.caise1);
STATS1 = regionprops(L2,'Area');
w=[STATS1.Area];
S1=cat(1,STATS1.Area);
hold on;
x=0;
[mm,nn]=size(S1);
str=[];
for i=1:mm
if S1(i,1)<49500
x=x+1;
str(end+1)=i;
end
end
X=sprintf('共%d个瓶子,其中有%d个未装满,', mm,x);
if x==1
A=str(1,1);
axes(handles.axes6);
imshow(handles.Img);
title([X,'第',num2str(A)','个瓶子不合格']) ;
end
if x==2
A=str(1,1);
B=str(1,2);
axes(handles.axes6);
imshow(handles.Img);
title([X,'第',num2str(A)',',',num2str(B),'个瓶子不合格']) ;
end
if x==3
A=str(1,1);B=str(1,2);C=str(1,3);
axes(handles.axes6);
imshow(handles.Img);
title([X,'第',num2str(A)',',',num2str(B),',',num2str(C),'个瓶子不合格']) ;
end
if x==4
A=str(1,1);B=str(1,2);C=str(1,3);D=str(1,4);
axes(handles.axes6);imshow(handles.img);title([X,'第',num2str(A)',',',num2str(B),',',num2str(C),',',num2str(D),'个瓶子不合格']) ;
end
if x==5
A=str(1,1);
B=str(1,2);
C=str(1,3);
D=str(1,4);
E=str(1,5);
axes(handles.axes6);
imshow(handles.Img);
title([X,'第',num2str(A)',',',num2str(B),',',num2str(C),',',num2str(D),',',num2str(E),'个瓶子不合格']) ;
end
[L2,num] = bwlabel(handles.caise1);
STATS1 = regionprops(L2,'BoundingBox');
w=[STATS1.BoundingBox];
axes(handles.axes6);
imshow(handles.caise1);
title('显示最小外接矩形')
for i=1:num
rectangle('position',STATS1(i).BoundingBox,'edgecolor','r');%求白色封闭区域最小外接矩形
end
NR=length(w);
w1=zeros(NR/4,4);
[m, n]=size(w1);
k=1;
for a=1:m
for b=1:n
w1(a,b)=w(k);
k=k+1;
end
end
三、运行结果
四、备注
完整代码或者代写添加QQ 1564658423