【空瓶识别】基于matlab灰度+二值化空瓶检测【含Matlab源码 806期】

举报
海神之光 发表于 2022/05/29 02:11:11 2022/05/29
【摘要】 一、灰度二值化简介 1 灰度化 (grayscale) 将彩色图像转化为灰度图像的过程称为图像灰度化。彩色图像中的像素值由RGB三个分量决定,每个分量都有0-255(256种)选择,这样一个像素点的像素...

一、灰度二值化简介

1 灰度化 (grayscale)
将彩色图像转化为灰度图像的过程称为图像灰度化。彩色图像中的像素值由RGB三个分量决定,每个分量都有0-255(256种)选择,这样一个像素点的像素值可以有1600万种可能(256256256),而灰度图的像素点的像素值是RGB三个分量值相同的一种特殊的彩色图像, 只有256种可能。所以在图像处理中,往往将各种图像首先灰度化成灰度图像以便后续处理,降低计算量。灰度是指只含亮度信息,不含色彩信息的图像。黑白照片就是灰度图,特点是亮度由暗到明,变化是连续的。灰度图像的描述与彩色图像一样仍然反映了整幅图像的整体和局部的色度和亮度等级的分布和特征,

使用灰度图的好处:
① RGB的值都一样。
② 图像数据即调色板索引值,就是实际的RGB值,也就是亮度值。
③ 因为是256色调色板,所以图像数据中一个字节代表一个像素,很整齐。
所以,做图像处理时一般都采用灰度图。
要表示灰度图,就需要把亮度值进行量化,有四种方法:

(1)分量法
将彩色图像中的三分量的亮度作为三个灰度图像的灰度值,可根据应用需要选取一种灰度图像。
(2)最大值法
将彩色图像中的三分量亮度的最大值作为灰度图的灰度值。
(3)均值法
将彩色图像中的三分量亮度求平均得到灰度图的灰度值。
(4)加权平均法
根据重要性及其它指标,将三个分量以不同的权值进行加权平均。由于人眼对绿色的敏感最高,对蓝色敏感最低,因此,按下式对RGB三分量进行加权平均能得到较合理的灰度图像,f(i,j)=0.30R(i,j)+0.59G(i,j)+0.11B(i,j))。

2 二值化(binaryzation)
图像的二值化是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果。将256个亮度等级的灰度图像通过适当的阀值选取而获得仍然可以反映图像整体和局部特征的二值化图像。所有灰度大于或等于阀值的像素被判定为属于特定物体,其灰度值为255,否则这些像素点被排除在物体区域以外,灰度值为0,表示背景或者例外的物体区域。在数字图像处理中,二值图像占有非常重要的地位,首先,图像的二值化有利于图像的进一步处理,使图像变得简单,而且数据量减小,能凸显出感兴趣的目标的轮廓。其次,要进行二值图像的处理与分析,首先要把灰度图像二值化,得到二值化图像。
二值化的常用算法有:

全局二值化: 一幅图像包括目标物体、背景还有噪声,要想从多值的数字图像中直接提取出目标物体,最常用的方法就是设定一个全局的阈值T,用T将图像的数据分成两部分:大于T的像素群和小于T的像素群。将大于T的像素群的像素值设定为白色(或者黑色),小于T的像素群的像素值设定为黑色(或者白色)。全局二值化,在表现图像细节方面存在很大缺陷。为了弥补这个缺陷,出现了局部二值化方法。

局部二值化:按照一定的规则将整幅图像划分为N个窗口,对这N个窗口中的每一个窗口再按照一个统一的阈值T将该窗口内的像素划分为两部分,进行二值化处理。局部二值化也有一个缺陷。这个缺陷存在于那个统一阈值的选定。这个阈值是没有经过合理的运算得来,一般是取该窗口的平局值。这就导致在每一个窗口内仍然出现的是全局二值化的缺陷。为了解决这个问题,就出现了局部自适应二值化方法。

局部自适应二值化:在局部二值化的基础之上,将阈值的设定更加合理化。该方法的阈值是通过对该窗口像素的平均值E,像素之间的差平方P,像素之间的均方根值Q等各种局部特征,设定一个参数方程进行阈值的计算,例如:T=aE+bP+c*Q,其中a,b,c是自由参数。这样得出来的二值化图像就更能表现出二值化图像中的细节。

3 反色(inverse)
反色的实际含义是将R、G、B值反转。若颜色的量化级别是256,则新图的R、G、B值为255减去原图的R、G、B值。这里针对的是所有图,包括真彩图、带调色板的彩色图(又称为伪彩色图)、和灰度图。真彩图不带调色板,每个象素用3个字节,表示R、G、B三个分量。所以处理很简单,把反转后的R、G、B值写入新图即可,比如一个点的颜色为(0,0,0),反色后为(255,255,255)。带调色板的彩色图,其位图中的数据只是对应调色板中的一个索引值,我们只需要将调色板中的颜色反转,形成新调色板,而位图数据不用动,就能够实现反转。

二、部分源代码

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/115981305

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。