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

举报
海神之光 发表于 2022/05/29 03:56:49 2022/05/29
【摘要】 一、矩匹配算法简介 图像的矩是归一化的灰度级图像的二维随机变量的概率密度,是一个统计学特征。OpenCV中实现了这个矩的算子是Moments();其中分为零阶矩M00、一阶矩M10和M01、二阶矩M20...

一、矩匹配算法简介

图像的矩是归一化的灰度级图像的二维随机变量的概率密度,是一个统计学特征。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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300

三、运行结果

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

四、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)

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/119896295

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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