I. Brief introduction of moment matching algorithm

The moment of the image is the probability density of the two-dimensional random variable of the normalized grayscale image, which is a statistical feature. The operator that implements this moment in OpenCV is Moments(); It is divided into zero order moment M00, first order moment M10 and M01, second order moment M20, M02 and M11. Where, when the image is a binary graph, M00 is the sum of the image area (white area), or the area of the connected domain; In this case, M10 and M01 are the accumulative values of X and Y coordinates in the white area of the image, so the center of gravity (Xc,Yc) of the image can be written as: Xc=M10/M00; Yc=M01/M00; The second moment of the image is generally used to find the direction of the image. The method is as follows:

Two, some source code

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 anew 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 orinvalid 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'},'Read picture');
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); Calculation of gray image features and displayset(handles.u,'string',num2str(u)); The mean %set(handles.n2,'string',num2str(n2)); % varianceset(handles.e,'string',num2str(e)); The % of skewnessset(handles.K,'string',num2str(K)); % kurtosisset(handles.energy,'string',num2str(energy)); % energyset(handles.ENTROPY,'string',num2str(ENTROPY)); Entropy score = %25.0*ENTROPY/20+25.0*1000/n2+25.0*4/K+25.0*8/abs(u- 128.); % calculates the score value and gives the result score_result='best';
if score<60
    score_result='poor';
elseif score<70
    score_result='in';
elseif score<80
    score_result='good';
else 
    score_result='best';
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) % % To highlight the red component in the picture Pic, suitable for small, complete, occupy the whole picture of the road sign processing, % If the threshold sliderValue is negative, adjust the amount of the highlighted red portion using the calculated default Average as the threshold. % If the threshold sliderValue is non-negative, adjust the amount of the highlighted red portion according to the sliderValue as the threshold. Adjust how much % R_G is highlighted in red to R_G=0.5* (2*Pic_double(:,:,1)-Pic_double(:,:,2)-Pic_double(:,:,3)); % R_G_gray is a binary image of R_G_gray associated with the threshold average or sliderValue. % when the red component is too large, R_G_binary_real % R_G_binary_real is the corrected binaryimage of R_G_gray % average is the calculated threshold % Pic_pattern Pic_R_rate % Pic_R_rate % Pic_R_rate % When the small picture (the sides of the road sign are close to the sides of the picture) is processed, the proportion of the road sign (red) is0.37Good, default is0.4% For large images (landmarks occupy only a small area of the image), the proportion of landmarks is small and defaults to0% Enable fix when R_hao>Pic_R_rate, otherwise disable fix 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.4Salient red componentset(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 % Turns categories into color images for displayfor 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% The second type (the type greater than the average threshold) is marked green Pic_pattern_temp(I,j,1:3) = [0.255.0];
        end
        if Pic_pattern_new(i,j)= =3% The third class (less than the average threshold) is marked blue 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); % highlights the red componentset(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 % Turns categories into color images for displayfor 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_b4 -);
load('fname_array.mat'); % Generates and saves an array of image namesif Pic_num==1% Clear data from the database fname_array=' ';
end
fname_array(Pic_num,1:fname_b4 -)=fname_new;
save 'fname_array.mat' fname_array;

load('signpost_data.mat'); % generates and stores an array of related results of momentsif Pic_num==1% Clear data in the database signpost_data=0;
end
Pic_binary_1=0; % Binary images of different classes are generated by Pic_pattern_new to compute moment 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
Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

2 Reference [1] CAI Limei. MATLAB Image Processing — Theory, Algorithm and Case Analysis [M]. Tsinghua University Press, 2020. [2] Yang Dan, ZHAO Haibin, LONG Zhe. Examples of MATLAB Image Processing In detail [M]. Tsinghua University Press, 2013. [3] Zhou Pin. MATLAB Image Processing and Graphical User Interface Design [M]. Tsinghua University Press, 2013. [4] LIU Chenglong. Proficient in MATLAB Image Processing [M]. Tsinghua University Press, 2015. [5] Chen Hao, FANG Yong, Zhu Dazhou, WANG Cheng, Chen Zilong. Edge detection of thermal infrared image of maize plant based on ant colony algorithm [J]. Journal of agricultural mechanization research, 2015,37(06)