一、灰度二值化简介

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});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_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 == 0return;
end
file = fullfile(pathname, filename);
Img = imread(file);
img=imread(file);
[y, ~, ~] = size(Img);
if y > 800rate = 800/y;Img1 = imresize(Img, rate);
elseImg1 = 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:mmif S1(i,1)<49500x=x+1;str(end+1)=i;end
end
X=sprintf('共%d个瓶子,其中有%d个未装满,', mm,x);
if x==1A=str(1,1);axes(handles.axes6);imshow(handles.Img);title([X,'第',num2str(A)','个瓶子不合格']) ;
end
if x==2A=str(1,1);B=str(1,2);axes(handles.axes6);imshow(handles.Img);title([X,'第',num2str(A)',',',num2str(B),'个瓶子不合格']) ;
end
if x==3A=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==4A=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==5A=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:numrectangle('position',STATS1(i).BoundingBox,'edgecolor','r');%求白色封闭区域最小外接矩形
end
NR=length(w);
w1=zeros(NR/4,4);
[m, n]=size(w1);
k=1;
for a=1:mfor b=1:nw1(a,b)=w(k);k=k+1;end
end

三、运行结果

四、matlab版本及参考文献

1 matlab版本
2014a

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

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

  1. 【Matlab生物电信号】生物电信号仿真【含GUI源码 684期】

    一.代码运行视频(哔哩哔哩) [Matlab生物电信号]生物电信号仿真[含GUI源码 684期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]董兵,超于毅,李 ...

  2. 【Matlab语音分析】语音信号分析【含GUI源码 1718期】

    一.代码运行视频(哔哩哔哩) [Matlab语音分析]语音信号分析[含GUI源码 1718期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]韩纪庆,张磊,郑铁 ...

  3. 【按键精灵安卓版教程】二值化过QQ滑块验证【源码分享】

    测试设备:雷电模拟器 540*960 dpi 240 基本原理: 把滑块验证图片转化成0和1的二值化图片,也就是黑白图 原图 二值化后的图片 然后找滑块序列,匹配成功,就找到滑块位置了 代码分享: D ...

  4. 利用OpenCV的函数createTrackbar创建滑动条查看二值化的最优阈值的源码及讲解

    图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 图像处理开发资料.图像处理技术交流请加QQ群,群号 271891601 这个功能的实现其实很简单,主要是要明白creat ...

  5. cv2 inrange灰度图_基于openCV,PIL的深色多背景复杂验证码图像转灰度二值化,并去噪降噪处理分析...

    title: [python]基于openCV,PIL的深色多背景复杂验证码图像转灰度二值化,并去噪降噪处理分析 type: categories copyright: true categories ...

  6. opencv 轮廓放大_基于openCV,PIL的深色多背景复杂验证码图像转灰度二值化,并去噪降噪处理分析...

    title: [python]基于openCV,PIL的深色多背景复杂验证码图像转灰度二值化,并去噪降噪处理分析 type: categories copyright: true categories ...

  7. MATLAB图像二值化函数im2bw与imbinarize对给定阈值的内部处理细节

    MATLAB图像处理工具包中的im2bw函数和imbinarize函数均可以实现灰度图像的二值化功能,且在MATLAB R2018a之后推荐使用imbinarize.但两者在内部处理图像的时候具有一些 ...

  8. linux 正点原子ov5640_【正点原子FPGA连载】第二十六章基于OV5640的二值化实验-摘自【正点原子】领航者 ZYNQ 之嵌入式开发指南 (amobbs.com 阿莫电子论坛)...

    本帖最后由 正点原子 于 2020-10-26 16:21 编辑 QQ群头像.png (1.78 KB) 2020-10-24 10:50 上传5)关注正点原子公众号,获取最新资料 100846rel ...

  9. 【Matlab验证码识别】遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别【含GUI源码 1694期】

    一.代码运行视频(哔哩哔哩) [Matlab验证码识别]遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别[含GUI源码 1694期] 二.matlab版本及参考文献 1 matlab ...

  10. 【Matlab人脸识别】BP神经网络人脸识别(含识别率)【含GUI源码 891期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]BP神经网络人脸识别(含识别率)[含GUI源码 891期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] ...

最新文章

  1. [技术文档] 一劳永逸,用USB设备制作多系统引导
  2. 独家|OpenCV 1.1 Mat - 基本图像容器(附链接)
  3. ios打包报错:User interaction is not allowed
  4. Ionic APP 热更新
  5. python数据结构与算法之排序
  6. Centos6.5部署大众点评CAT
  7. sql通用防注入程序php,sql通用防注入系统_PHP教程
  8. 如何通过http协议知道是从那个站点过来的请求_聊聊HTTP的那些事,以及在它背后的“勾当”...
  9. Linux的顶级十六进制编辑器
  10. PyQt4---QTextBrowser
  11. Safari 神器,手机平板都能用,我们找到了 14 个超实用浏览器插件
  12. 对于rpx的理解和使用
  13. 今日简报 每日精选12条新闻简报 每天一分钟 知晓天下事 4月13日
  14. docker多容器操作与强制删除容器的方法步骤
  15. Part 10:iOS的数据持久化(1),文件,归档
  16. 小谷围岛Landsat 8 OLI(陆地成像仪) 融合遥感影像彩色组合解译标志对比
  17. DBDataSource platformDataSource has not registed!的问题
  18. 剑灵省空间双开办法,附带批处理脚本
  19. 教您用公式编辑器快速输入公式
  20. scheduled一分钟执行一次_@Scheduled(cron = 0 0 * * * ?)实现定时任务

热门文章

  1. BZOJ 4802 欧拉函数(Pollard_Rho)
  2. 图片垂直居中 和 float
  3. 中国iOS和Android设备激活量将超美国
  4. clion解决中文乱码的问题
  5. 2018, CHI CHI2016汇总all CHI
  6. htc 常见错误和解决方案
  7. 外参矩阵转四元数,左右手坐标系转化1
  8. Atitit 融合战略的艺术 目录 1.1. Os方向 uwp ms。。 1 1.2. Sdk 方向 。ms netcore 1 2. Netcore优点 1 2.1. .NET应用程序可以
  9. Atitit 嵌入式tomcat 嵌入式服务器 attilax 你感觉艾提拉 总结 比起嵌入jetty ,文件可以自动刷新貌似还不错。。方便调试debug package com.attilax.
  10. Atiitt 技术部的基本发展战略 attilax总结 艾龙 著 1. 战略的解释,即是 大概纲领与方法 1 2. 技术的基本战略是,培训,吸收与改造,recos,预研,技术储备 1 2.1.