【交通标志识别】基于matlab GUI矩匹配算法路标识别【含Matlab源码 1175期】

时间:2021-08-24
本文章向大家介绍【交通标志识别】基于matlab GUI矩匹配算法路标识别【含Matlab源码 1175期】,主要包括【交通标志识别】基于matlab GUI矩匹配算法路标识别【含Matlab源码 1175期】使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、矩匹配算法简介

图像的矩是归一化的灰度级图像的二维随机变量的概率密度,是一个统计学特征。OpenCV中实现了这个矩的算子是Moments();其中分为零阶矩M00、一阶矩M10和M01、二阶矩M20,M02和M11;其中当图像为二值图时,M00是图像面积(白色区域)的总和,或者说连通域的面积;而这时M10和M01是图像白色区域上x和y坐标值的累计,所以图像的的重心(Xc,Yc)可以由:
Xc=M10/M00;
Yc=M01/M00;
图像的二阶矩一般用来求图像的方向,方法是:

二、部分源代码

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

% Last Modified by GUIDE v2.5 20-Jul-2010 09:42:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FeatureExtraction_New_OpeningFcn, ...
                   'gui_OutputFcn',  @FeatureExtraction_New_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 FeatureExtraction_New is made visible.
function FeatureExtraction_New_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 FeatureExtraction_New (see VARARGIN)
global Pic_num;
Pic_num=0;
% Choose default command line output for FeatureExtraction_New
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);



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


% --- Outputs from this function are returned to the command line.
function varargout = FeatureExtraction_New_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)
global Pic;
global Pic_gray;
global fname;
global Pic_num;

[fname, pname, index] = uigetfile({'*bmp;*.jpg';'*.gif'},'读取图片');
if index==1
    Pic_num=Pic_num+1;
    str = [pname fname];  
    Pic=imread(str);   
    set(handles.text1,'string',fname);
    axes(handles.axes1);     
    imshow(Pic);  
end
    axes(handles.axes2);     
    Pic_gray=rgb2gray(Pic);
    imshow(Pic_gray);  
    
[u,n2,e,K,energy,ENTROPY]=Pic_gray_count(Pic_gray);   % 计算灰度图像的种种特征并显示
set(handles.u,'string',num2str(u));     %均值
set(handles.n2,'string',num2str(n2));       %方差
set(handles.e,'string',num2str(e));         %偏度
set(handles.K,'string',num2str(K));         %峰度
set(handles.energy,'string',num2str(energy));%能量
set(handles.ENTROPY,'string',num2str(ENTROPY));%熵
score=25.0*ENTROPY/20+25.0*1000/n2+25.0*4/K+25.0*8/abs(u-128);  % 计算评分值,给出结果
score_result='优';
if score<60
    score_result='差';
elseif score<70
    score_result='中';
elseif score<80
    score_result='良';
else 
    score_result='优';
end
set(handles.good_or_bad,'string',score_result);


% --- 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)

% [R_G,R_G_gray,R_G_binary,R_G_binary_real,Pic_pattern,average]=Pic_Red_Outstand(Pic,sliderValue,Pic_R_rate)

% % 将图片 Pic 中的红色分量突出来,适用于对小的,完整的,占据整个图片的路标的处理,对大图片中的小路标的处理效果不好
% 如果阈值 sliderValue 为负数,则为利用计算出的默认average 作为阈值,调整突出的红色部分的多少
% 如果阈值 sliderValue 非负数,则根据 sliderValue 作为阈值,调整突出的红色部分的多少
% R_G 为  R_G=0.5*(2*Pic_double(:,:,1)-Pic_double(:,:,2)-Pic_double(:,:,3));
% R_G_gray 为 R_G 所构成的灰度图像
% R_G_binary 是与阈值  average 或 sliderValue 相关的R_G_gray 的二值化图像,
% 当红色分量太大,R_G_binary 比  R_G_binary_real 效果更好
% R_G_binary_real 是 R_G_binary 经过修正的 R_G_gray 的二值化图像
% average 为计算出的阈值
% Pic_pattern 描述图片 Pic 的分类情况
% Pic_R_rate  至关重要的变量! 
%                               当对小图片(路标的四周靠近图片的四周)处理时,路标(红色)的比例为0.37较好,默认为 0.4                 
%                              当对大图片(路标在图片中只占一个较小的区域)时,路标的比例很小,默认为 0(即启用修正)
    
%                              当 R_hao>Pic_R_rate 时,启用修正,否则不启用修正 
global Pic;
global Pic_pattern_new;

[R_G,R_G_gray,R_G_binary,R_G_binary_real,Pic_pattern,Pic_pattern_new,average]=Pic_Red_Outstand(Pic,-1,0.4);        % 0.4 突出红色分量
set(handles.text5,'string',num2str(average));
axes(handles.axes3);     
imshow(R_G_gray);  
axes(handles.axes4);     
imshow(R_G_binary);  
axes(handles.axes5);     
imshow(R_G_binary_real);  
% figure;
% surf(Pic_pattern_new);

Pic_pattern_temp=0;
[a,b]=size(Pic_pattern_new);
for i=1:a           % 将分类转变为彩色图片显示出来
    for j=1:b
        if Pic_pattern_new(i,j)==-1
            Pic_pattern_temp(i,j,1:3)=[0,0,0];
        end
        if Pic_pattern_new(i,j)==1
            Pic_pattern_temp(i,j,1:3)=[255,0,0];
        end
        if Pic_pattern_new(i,j)==2              % 第二类(大于平均阈值的一类)标为绿色
            Pic_pattern_temp(i,j,1:3)=[0,255,0];
        end
        if Pic_pattern_new(i,j)==3              % 第三类(小于平均阈值的一类)标为蓝色
            Pic_pattern_temp(i,j,1:3)=[0,0,255];
        end
    end
end
axes(handles.axes6);     
imshow(Pic_pattern_temp);  

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global Pic;
global Pic_pattern_new;
sliderValue = get(handles.slider1,'Value');
sliderValue =round(sliderValue);
set(handles.text3,'String', num2str(sliderValue));
num2str(sliderValue)

[R_G,R_G_gray,R_G_binary,R_G_binary_real,Pic_pattern,Pic_pattern_new,average]=Pic_Red_Outstand(Pic,sliderValue,0.4);        % 突出红色分量
set(handles.text5,'string',num2str(average));
axes(handles.axes3);     
imshow(R_G_gray);  
axes(handles.axes4);     
imshow(R_G_binary);  
axes(handles.axes5);     
imshow(R_G_binary_real);  

Pic_pattern_temp=0;
[a,b]=size(Pic_pattern_new);
for i=1:a           % 将分类转变为彩色图片显示出来
    for j=1:b
        if Pic_pattern_new(i,j)==-1
            Pic_pattern_temp(i,j,1:3)=[0,0,0];
        end
        if Pic_pattern_new(i,j)==1
            Pic_pattern_temp(i,j,1:3)=[255,0,0];
        end
        if Pic_pattern_new(i,j)==2
            Pic_pattern_temp(i,j,1:3)=[0,255,0];
        end
        if Pic_pattern_new(i,j)==3
            Pic_pattern_temp(i,j,1:3)=[0,0,255];
        end
    end
end
axes(handles.axes6);     
imshow(Pic_pattern_temp); 

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- 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)
global Pic;
global Pic_pattern_new;

slider_value=str2num(get(handles.text3,'string'));
average=str2num(get(handles.text5,'string'));
if slider_value>0
    [R_G,R_G_gray,R_G_binary,R_G_binary_real,Pic_pattern,Pic_pattern_new,average]=Pic_Red_Outstand(Pic,slider_value,1);
else
    [R_G,R_G_gray,R_G_binary,R_G_binary_real,Pic_pattern,Pic_pattern_new,average]=Pic_Red_Outstand(Pic,average,1);
end
figure;
surf(Pic_pattern_new);


% --- 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)
global Pic_pattern_new;
global fname;
global Pic_num;

[fname_a,fname_b]=size(fname);
fname_new=fname(1,1:fname_b-4);
load('fname_array.mat');                % 生成并保存图像名称构成的数组
if Pic_num==1   %清空数据库中的数据
    fname_array='   ';
end
fname_array(Pic_num,1:fname_b-4)=fname_new;
save 'fname_array.mat' fname_array;

load('signpost_data.mat');                % 生成并保存矩的相关结果构成的数组
if Pic_num==1   %清空数据库中的数据
    signpost_data=0;
end
Pic_binary_1=0;     % 由 Pic_pattern_new 生成不同三类的二值化图像,以便计算矩
Pic_binary_2=0;
Pic_binary_3=0;
[a,b]=size(Pic_pattern_new);
for i=1:a
    for j=1:b
        if Pic_pattern_new(i,j)==1
            Pic_binary_1(i,j)=0;
        else
            Pic_binary_1(i,j)=1;
        end
        if Pic_pattern_new(i,j)==2
            Pic_binary_2(i,j)=0;
        else
            Pic_binary_2(i,j)=1;
        end
        if Pic_pattern_new(i,j)==3
            Pic_binary_3(i,j)=0;
        else
            Pic_binary_3(i,j)=1;
        end
    end

三、运行结果


四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
[5]陈浩,方勇,朱大洲,王成,陈子龙.基于蚁群算法的玉米植株热红外图像边缘检测[J].农机化研究. 2015,37(06)

原文地址:https://www.cnblogs.com/QQ912100926/p/15181695.html