一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【颜色识别】基于matlab机器视觉颜色识别系统【含Matlab源码 588期】

获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);

二、机器视觉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 = maingui(varargin)
% MAINGUI M-file for maingui.fig
%      MAINGUI, by itself, creates a new MAINGUI or raises the existing
%      singleton*.
%
%      H = MAINGUI returns the handle to a new MAINGUI or the handle to
%      the existing singleton*.
%
%      MAINGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAINGUI.M with the given input arguments.
%
%      MAINGUI('Property','Value',...) creates a new MAINGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before maingui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to maingui_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 maingui% Last Modified by GUIDE v2.5 03-Mar-2008 14:56:06% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @maingui_OpeningFcn, ...'gui_OutputFcn',  @maingui_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 maingui is made visible.
function maingui_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 maingui (see VARARGIN)% warning off;splash('thirdeye.kon','png');axes(handles.axes2); %initialise axes where color will be displayed
img = im2uint8(zeros([128 128 3])); %set it to black color initially
handles.h2 = image(img);
axis off;axes(handles.axes1);%initialise axes where preview will be displayed
axis off; drawnow;axes(handles.axes1);
handles.h1 = imshow(im2uint8(ones([240 320 3])));%intial white colored image on the axistry %intialise sound card for sound alert[y,Fs] = wavread('warning.wav');% read a warning wave filehandles.player = audioplayer(y, Fs);
catch %if sound card not present display following error messageerrstr = lasterr;h2 =  errordlg(['Sound Card Not Present OR ' errstr] ,'Sound Card Error');setWindowOnTop(h2,'true');
endhandles.guifig = gcf;tryif exist('startupdata.mat','file') %load startup data. This contains information about initial conditions set. If not found GUI will load its default valuesload startupdataset(handles.animation,'Checked',animation);set(handles.sound_alert,'Checked',sound_alert);set(handles.auto_hide,'Checked',auto_hide);set(handles.on_top,'Checked',on_top);if strcmp(get(handles.on_top,'Checked'),'on')setWindowOnTop(handles.guifig,'true');end        end
catcherrordlg(lasterr);
endhandles.displayperiod = 2; %pause for the amount specifiedhandles.tmr = timer('TimerFcn', {@TmrFcn,handles.guifig},'BusyMode','Queue',...'ExecutionMode','fixedRate','Period',0.1);%intialise a Timer Functionguidata(handles.guifig, handles);% Choose default command line output for maingui
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes maingui wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = maingui_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 Timer Event
function TmrFcn(src,event,handles)
handles = guidata(handles);pause(handles.displayperiod);%pause for the number of seconds intialised initiallystr = [pwd '\product images'];%product images stored here
names = dir([str '\*.jpg']);
rand_num = random_integer_generator(1,numel(names),1);%generates  a random number from the number of JPG files present in the product directory
handles.reference_image = imread([str '\' names(rand_num,1).name]);% reads any one random imagedrawnow;
set(handles.h1,'CData',handles.reference_image);% displays the randome image on the axisif strcmp(get(handles.auto_hide,'Checked'),'on')setWindowState(handles.guifig,'restore'); %sets the gui figure to restoredrawnow;
endif isfield(handles,'second') %the color from first variable is stored in variable second. Not true while startupif ~strcmp(handles.first,'NOCOLOR')handles.second = handles.first;end
end%%%%%%%%%%%%%%%%%%COLOR MATCHING FUNCTION
[output_color output_image] = validate_color_new(handles.reference_image,handles.data);%new main function for finding color match
%%%%%%%%%%%%%%%%%%%%
set(handles.color_text,'string',upper(output_color)); %sets the string value the name of the colorimg = im2uint8(zeros([128 128 3])); %create a img file which can display the color
val = output_image(round(size(output_image,1)/2),round(size(output_image,2)/2),:);
val = squeeze(val)';
img(:,:,1) = val(1);
img(:,:,2) = val(2);
img(:,:,3) = val(3);
set(handles.h2,'CData',img);drawnow; %outputs the color on this imghandles.first = output_color; %store the output color in first variableif ~strcmp(output_color,'NOCOLOR') %if color cannot be identified from the stored colors then dont give audiovisual indicationif ~isfield(handles,'second') %check condition in first run, if condition is not satisfied then second varaible = first variablehandles.second = output_color;endif ~strcmp(handles.first,handles.second) %if colors are different then do the following activitytry%necessary if sound card is not presentif strcmp(get(handles.sound_alert,'Checked'),'on')%give and alert only if sound_alert is set to onplay(handles.player);endcatcherrordlg(lasterr);endif strcmp(get(handles.animation,'Checked'),'on') %do the animation if animation is set to onmodel_color(output_color);%calls a model wearing the color.Default time duration of 3 secondsendend
endif strcmp(get(handles.auto_hide,'Checked'),'on')pause(handles.displayperiod);%pause for the number of seconds intialised initiallysetWindowState(handles.guifig,'minimize'); %sets the gui figure to minimize        endif strcmp(get(handles.auto_hide,'Checked'),'off')%This is useful to keep a pause of the same time irrespective of auto hide on or offpause(handles.displayperiod);%pause for the number of seconds intialised initially
end

四、运行结果



五、matlab版本及参考文献

1 matlab版本
2014a

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

【颜色识别】基于matlab机器视觉颜色识别系统【含Matlab源码 588期】相关推荐

  1. 【Matlab水果识别】苹果质量检测及分级系统(带面板)【含GUI源码 1613期】

    一.代码运行视频(哔哩哔哩) [Matlab水果识别]苹果质量检测及分级系统(带面板)[含GUI源码 1613期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1 ...

  2. 【Matlab指纹识别】指纹识别门禁系统【含GUI源码 1692期】

    一.代码运行视频(哔哩哔哩) [Matlab指纹识别]指纹识别门禁系统[含GUI源码 1692期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 包子阳,余继 ...

  3. 【Matlab指纹识别】指纹识别匹配门禁系统【含GUI源码 587期】

    一.代码运行视频(哔哩哔哩) [Matlab指纹识别]指纹识别匹配门禁系统[含GUI源码 587期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 包子阳,余 ...

  4. 【Matlab车牌识别】停车计费系统【含GUI源码 735期】

    一.代码运行视频(哔哩哔哩) [Matlab车牌识别]停车计费系统[含GUI源码 735期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅.MATLA ...

  5. 【Matlab破损识别】机器视觉+SVM玉米种子破损识别(带面板)【含GUI源码 1651期】

    一.代码运行视频(哔哩哔哩) [Matlab破损识别]机器视觉+SVM玉米种子破损识别(带面板)[含GUI源码 1651期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考 ...

  6. 【Matlab身份证识别】身份证号码识别【含GUI源码 014期】

    一.代码运行视频(哔哩哔哩) [Matlab身份证识别]身份证号码识别[含GUI源码 014期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅.MAT ...

  7. 【Matlab水果识别】自助水果超市【含GUI源码 594期】

    一.代码运行视频(哔哩哔哩) [Matlab水果识别]自助水果超市[含GUI源码 594期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]倪云峰,叶健,樊娇娇 ...

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

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

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

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

  10. 【Matlab人脸识别】形态学教室人数统计(带面板)【含GUI源码 1703期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]形态学教室人数统计(带面板)[含GUI源码 1703期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]孟 ...

最新文章

  1. 关于「无人驾驶」,一位投资大佬给出了四大预判
  2. Linux Shell脚本攻略学习总结:二
  3. java知识点7——面向过程和面向对象、面向对象的内存分析、构造方法
  4. spring框架学习笔记3:使用注解代替配置文件
  5. idea 写html js 热部署
  6. eclipse maven 项目发布到tomcat 报错 Failed to scan JAR [file:/C:/xxxxx.jar] from WEB-INF/lib
  7. 简述osi参考模型各层主要功能_OSI网络模型
  8. android手机 代码下载,android手机NFC 示例源码下载
  9. 【java】java 并发变成 阶段同步器 Phaser
  10. C++原子性实现无锁队列
  11. SqlServer2005海量数据存储过程分页(上)
  12. android以ini文件形式保存在内部存储_Android中将Bitmap对象以PNG格式保存在内部存储中...
  13. stm23f103 usart1串口中断可进入,而stm32f205 usart1的中断进不去解决办法
  14. html让方块往下移动,JS控制方块自动移动
  15. 如何利用RTK通过CORS进行测量?
  16. 这三个方法让你实现文字转语音在线转换
  17. 20多年的岁月里一路走来满是辜负与遗憾,从双非到阿里SP,收到offer的那天我哭惨了
  18. 全球及中国智能医疗产品行业发展态势及投资可行性研究报告2022-2027年
  19. OGNL表达式的基本语法和用法
  20. deepin安装瑞昱Realtek无线网卡8188GU驱动

热门文章

  1. 杭电多校第三次 Problem A. Ascending Rating(单调队列)
  2. idea展现目录清晰方式
  3. Appium实现的技巧
  4. Perforce client - p4常见用法
  5. sklearn,SVM 和文本分类
  6. 传智播客 sklearn数据集与机器学习组成
  7. 增强现实:原理算法与应用 第一章增强现实概论笔记
  8. Lost Found
  9. python学习笔记 程序执行过程 基本数据类型
  10. unity投影和视频画面的调整