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

本文介绍了如何在Matlab中进行图像灰度化和二值化处理,包括灰度化方法(分量法、最大值法、均值法和加权平均法)、二值化(全局、局部和局部自适应)以及在智能空瓶检测中的应用。作者还提供了部分Matlab代码示例,展示了从图像加载到处理过程的详细步骤。
摘要由CSDN通过智能技术生成

✅博主简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,Matlab项目合作可私信。
🍎个人主页:海神之光
🏆代码获取方式:
海神之光Matlab王者学习之路—代码获取方式
⛳️座右铭:行百里者,半于九十。

更多Matlab仿真内容点击👇
Matlab图像处理(进阶版)
路径规划(Matlab)
神经网络预测与分类(Matlab)
优化求解(Matlab)
语音处理(Matlab)
信号处理(Matlab)
车间调度(Matlab)

⛄一、灰度二值化简介

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 x1
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 x3
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©,‘个瓶子不合格’]) ;
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©,‘,’,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©,’,‘,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

⛄三、运行结果

在这里插入图片描述

⛄四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1]段峰,王耀南,刘焕军.基于机器视觉的智能空瓶检测机器人研究[J].仪器仪表学报. 2004(05)

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

🍅 仿真咨询
1 各类智能优化算法改进及应用

生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化

2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断

3 图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知

4 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化

5 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配

6 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化

7 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化

8 电力系统方面
微电网优化、无功优化、配电网重构、储能配置

9 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长

10 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合

  • 21
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值