【颜色识别】基于matlab GUI机器视觉RGB识别系统【含Matlab源码 951期】

举报
海神之光 发表于 2022/05/29 02:55:54 2022/05/29
【摘要】 一、机器视觉RGB识别简介 颜色是物体表面的固有特征, 在目标识别和图像分割中有着无法替代的作用。机器视觉是利用光电成像系统和图像处理模块对物体进行尺寸、形状、颜色等的识别。这样, 就把计算机的快速性、...

一、机器视觉RGB识别简介

颜色是物体表面的固有特征, 在目标识别和图像分割中有着无法替代的作用。机器视觉是利用光电成像系统和图像处理模块对物体进行尺寸、形状、颜色等的识别。这样, 就把计算机的快速性、可重复性, 与人眼视觉的高度智能化和抽象能力相结合, 用机器代替人眼来作各种测量和判断, 大大提高了生产的柔性和自动化程度。图像处理中最适合显示系统的颜色空间是RGB颜色空间, 但其R、G、B3个分量高度相关, 阈值选择困难, 本文提出了一种稳定颜色空间的图像分割和识别方法, 并应用于自动生产线, 取得良好效果。

1 系统结构
机器视觉系统结构如图1所示。
在这里插入图片描述
机器视觉系统中, 从获得图像数据到最后获得处理结果, 通常要经过很多种算法。同时, 不同目的的机器视觉系统要求对图像作的运算也大为不同。机器视觉系统的输入是图像, 而最后的输出则是一些符号或者数值, 这些符号或数值, 有可能表达了物体的特性 (正品/次品, 阿拉伯数字等) 和位置 (集成电路引脚的位置等等) 。

2.1 图像平滑
采集的图像常有噪声干扰, 图像平滑处理有利于改善图像质量[, 本文采用自适应中值滤波算法。设Sx, y表示中心点 (x, y) 在给定时间的掩模窗口。设Zmin为Sx, y中灰度级最值;Zmed为Sx, y中灰度级中值;Zmax为Sx, y中灰度级最大值;Zx, y为像素 (x, y) 的灰度级;Smax为Sx, y允许最大的尺寸。自适应中值滤波器算法为:

A层算法
在这里插入图片描述
如果A1>0&A2>0转到B层。否则, 增大窗口尺寸。
如果窗口尺寸≤Smax重复A层。否则输出Zx, y。
B层算法
在这里插入图片描述
如果A1>0&A2>0, 输出Zx, y。否则输出Zmed。

2.2 颜色模型转换
装置采集的图像为RGB模型, 为避免计算机对图像进行处理时因三个量相互影响产生色差或者颜色失真而导致系统误判, 需要转换为HSI模型, 本文采用几何推导法进行RGB-HSI转换, 将RGB中的亮度因素进行圆柱极坐标的双椎体转换, 将三维变为二维, 在平面中求出HSI模型的色调分量值。

转换公式如下:
在这里插入图片描述
其中:H定义颜色的波长, 称为色调;S表示颜色的深浅程度, 称为饱和度;I表示强度或亮度。

2.3 物体识别
阈值彩色图像分割
生产线机器视觉系统中拍摄的物体受光照、噪声等环境影响, 采集到的物体图像颜色会与实际颜色存在差异, 物体之间也会存在一定颜色的差异, 同一个物体不同部分也可能存在颜色差异, 要准确识别物体, 需要通过图像分割来判断相邻区域颜色的相似度[7]。阈值法图像分割需要选取合适的阈值, 将计算机获取的经过颜色模型变换的图像色彩特征与设定的阈值进行比较, 以区分工件和背景。
在这里插入图片描述
f (x, y) 为计算机采集的图像色彩特征;T为设定的阈值。如果将图像二值化, 则b0为0, b1为1。这种方法对被识别物体为单一颜色时较适合。

二、部分源代码

function varargout = Color_Detect(varargin)
% COLOR_DETECT MATLAB code for Color_Detect.fig
%      COLOR_DETECT, by itself, creates a new COLOR_DETECT or raises the existing
%      singleton*.
%
%      H = COLOR_DETECT returns the handle to a new COLOR_DETECT or the handle to
%      the existing singleton*.
%
%      COLOR_DETECT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COLOR_DETECT.M with the given input arguments.
%
%      COLOR_DETECT('Property','Value',...) creates a new COLOR_DETECT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Color_Detect_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Color_Detect_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 Color_Detect

% Last Modified by GUIDE v2.5 22-mAY-2021 12:54:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Color_Detect_OpeningFcn, ...
                   'gui_OutputFcn',  @Color_Detect_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 Color_Detect is made visible.
function Color_Detect_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 Color_Detect (see VARARGIN)

% Choose default command line output for Color_Detect
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


set(handles.axes1,'Visible','off');
set(handles.axes2,'Visible','off');
set(handles.uipanel1,'visible','off');
set(handles.uipanel2,'visible','off');
set(handles.start,'Enable','off');
set(handles.snap,'Enable','off');
set(handles.oi,'visible','off');
set(handles.dc,'visible','off');
set(handles.uipanel3,'visible','off');
set(handles.startv,'enable','off');
set(handles.text,'enable','off');
set(handles.set,'enable','off');
set(handles.pop,'enable','off');
set(handles.text5,'visible','off');

% UIWAIT makes Color_Detect wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Color_Detect_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 image.
function image_Callback(hObject, eventdata, handles)
% hObject    handle to image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.uipanel1,'visible','on');
set(handles.video,'Enable','off');


% --- Executes on button press in video.
function video_Callback(hObject, eventdata, handles)
% hObject    handle to video (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.image,'Enable','off');
set(handles.text,'enable','on');
set(handles.pop,'enable','on');

% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
im=handles.im; x=handles.x;
switch x
    case 1
        axes(handles.axes2); cla;
        im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(r,rgb2gray(im));
 bw=im2bw(diff,0.18);
 area=bwareaopen(bw,300);
 rm=immultiply(area,r);  gm=g.*0;  bm=b.*0;
 image=cat(3,rm,gm,bm);
 imshow(image);
 % Transparcy part
  %[m n p]=size(im);
    % mask=ones(m,n);
     %i=find(image(:,:,1)==0);
    % mask(i)=.9; % Change Transparacy 
     %imshow(im);
     %hold on
     %h=imshow(image);
     %set(h,'AlphaData',mask);
 case 2
        axes(handles.axes2); cla;
        im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(g,rgb2gray(im));
 bw=im2bw(diff,0.18);
 area=bwareaopen(bw,300);
 gm=immultiply(area,g);  rm=r.*0;  bm=b.*0;
 image=cat(3,rm,gm,bm);
 imshow(image);
 case 3
        axes(handles.axes2); cla;
        im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(b,rgb2gray(im));
 bw=im2bw(diff,0.18);
 area=bwareaopen(bw,300);
 bm=immultiply(area,b);  gm=g.*0;  rm=r.*0;
 image=cat(3,rm,gm,bm);
 imshow(image);
 end
 set(handles.dc,'visible','on');


% --- Executes on button press in red.
function red_Callback(hObject, eventdata, handles)
% hObject    handle to red (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.x=1;
guidata(hObject,handles);
set(handles.start,'enable','on');

% --- Executes on button press in green.
function green_Callback(hObject, eventdata, handles)
% hObject    handle to green (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.x=2;
guidata(hObject,handles);
set(handles.start,'enable','on');
% --- Executes on button press in blue.
function blue_Callback(hObject, eventdata, handles)
% hObject    handle to blue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.x=3;
set(handles.start,'enable','on');
guidata(hObject,handles);

% --- Executes on button press in camera.
function camera_Callback(hObject, eventdata, handles)
% hObject    handle to camera (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
imaqreset
info= imaqhwinfo;
name = char(info.InstalledAdaptors(end));
c_info = imaqhwinfo(name);
id = c_info.DeviceInfo.DeviceID(end);
format = char(c_info.DeviceInfo.SupportedFormats(end));
handles.vid= videoinput(name, id, format);
axes(handles.axes1);cla;
h = waitbar(0,'Please wait...');
steps = 200;
for step = 1:steps
    % computations take place here
    waitbar(step / steps)
end
close(h) 

preview(handles.vid);
set(handles.snap,'Enable','on');
guidata(hObject,handles);
set(handles.uipanel2,'visible','on');
set(handles.oi,'visible','on');

% --- Executes on button press in browse.
function browse_Callback(hObject, eventdata, handles)
% hObject    handle to browse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Browse the file from user
[file path]=uigetfile({'*.jpg';'*.bmp';'*.jpeg';'*.png'}, 'Load Image File within Avilable Extensions');
image=[path file];
handles.file=image;
if (file==0)
    warndlg('You did not selected any file ') ; % fille is not selected
end
[fpath, fname, fext]=fileparts(file);
 validex=({'.bmp','.jpg','.jpeg','.png'});
 found=0;
 for (x=1:length(validex))
 if (strcmpi(fext,validex{x}))
     found=1;
handles.im=imread(image);
axes(handles.axes1); cla; 
h = waitbar(0,'Please wait...');
steps = 100;
for step = 1:steps
    % computations take place here
    waitbar(step / steps)
end
close(h) 
  end
 end
 imshow(handles.im);
if (found==0)
     errordlg('Selected file does not match available extensions. Please select file from available extensions [ .jpg, .jpeg, .bmp, .png] ','Image Format Error');
end
guidata(hObject, handles);
set(handles.uipanel2,'visible','on');
set(handles.oi,'visible','on');


三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、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/117415002

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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