【图像识别】基于不变矩的数字验证码识别含GUI界面

图像的几何不变矩

矩特征主要表征了图像区域的几何特征,又称为几何矩, 由于其具有旋转、平移、尺度等特性的不变特征,所以又称其为不变矩。在图像处理中,几何不变矩可以作为一个重要的特征来表示物体,可以据此特征来对图像进行分类等操作。如果想详细了解不变矩的概念、公式等请参考下面的论文:
不变矩方法研究
一种实用的不变矩计算方法

1.     HU矩

几何矩是由Hu(Visual pattern recognition by moment invariants)在1962年提出的,图像f(x,y)的(p+q)阶几何矩定义为

        Mpq =∫∫(x^p)*(y^q)f(x,y)dxdy(p,q = 0,1,……∞)

        矩在统计学中被用来反映随机变量的分布情况,推广到力学中,它被用作刻画空间物体的质量分布。同样的道理,如果我们将图像的灰度值看作是一个二维或三维的密度分布函数,那么矩方法即可用于图像分析领域并用作图像特征的提取。最常用的,物体的零阶矩表示了图像的“质量”:

                   Moo= ∫∫f(x,y )dxdy

        一阶矩(M01,M10)用于确定图像质心( Xc,Yc):

              Xc = M10/M00;Yc = M01/M00;

         若将坐标原点移至 Xc和 Yc处,就得到了对于图像位移不变的中心矩。如

            Upq =∫∫[(x-Xc)^p]*[(y-Yc)^q]f(x,y)dxdy。

        Hu在文中提出了7个几何矩的不变量,这些不变量满足于图像平移、伸缩和旋转不变。如果定义

         Zpq=Upq/(U20 + U02)^(p+q+2),

      Hu 的7种矩为:

      H1=Z20+Z02;H1=(Z20+Z02)^2+4Z11^2;......

矩是描述图像特征的算子,它在模式识别与图像分析领域中有重要的应用.迄今为止,常见的矩描述子可以分为以下几种:几何矩、正交矩、复数矩和旋转矩.其中几何矩提出的时间最早且形式简单,对它的研究最为充分。几何矩对简单图像有一定的描述能力,他虽然在区分度上不如其他三种矩,但与其他几种算子比较起来,他极其的简单,一般只需用一个数字就可表达。所以,一般我们是用来做大粒度的区分,用来过滤显然不相关的文档。

比如在图形库中,可能有100万幅图,也许只有200幅图是我们想要的。使用一维的几何矩的话,就可以对几何矩进行排序,建立索引,然后选出与目标图的几何矩最近的2000幅图作比较就好了。而对于其他的矩来说,由于一般是多维的关系,一般不好排序,只能顺序查找,自然速度有巨大的差别.所以。虽然几何矩不太能选出最像的,但可以快速排除不像的,提高搜索效率。

 

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

% Last Modified by GUIDE v2.5 23-Oct-2020 21:52:34

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

% Choose default command line output for daunxujian1
handles.output = hObject;
handles.Result = [];
handles.File = [];
% Update handles structure
guidata(hObject, handles);
clc; warning off all;
InitAxes(handles);
% UIWAIT makes daunxujian1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = daunxujian1_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)
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'载入图像',...
    fullfile(pwd, 'images'));
I = imread(fullfile(pathname, filename));
Result = Process_Main(I);
handles.File = fullfile(pathname, filename);
handles.Result = Result;
guidata(hObject, handles);
InitAxes(handles)
axes(handles.axes1); imshow(handles.Result.Image); title('原图像');

% --- 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)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
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)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
end

% --- 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)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
end

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
    axes(handles.axes6); imshow(handles.Result.Thin); title('细化处理');
    axes(handles.axes5); imshow(handles.Result.Bw); title('特征提取');
    hold on;
    h = [];
    for i = 1 : length(handles.Result.hs) %绘制水平线
        h = [h plot([1 handles.Result.sz(2)], [handles.Result.hs(i) handles.Result.hs(i)], 'r-')];
    end
    for i = 1 : length(handles.Result.vs)%绘制竖直线
        h = [h plot([handles.Result.vs(i) handles.Result.vs(i)], [1 handles.Result.sz(1)], 'g-')];
    end
    h = [h plot(handles.Result.x1, handles.Result.y1, 'y-')];%绘制左对角线
    h = [h plot(handles.Result.x2, handles.Result.y2, 'm-')];%绘制右对角线
end

% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'Save Image',...
    fullfile(pwd, 'Result/result.png'));
if ~isequal(filename, 0)
    imwrite(handles.Result.label, fullfile(pathname, filename));
    msgbox('保存图像成功!', '信息提示框');
end


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
choice = questdlg('确定退出?', ...
    '退出', ...
    '是','否','否');
switch choice
    case '是'
        close;
    otherwise
        return;
end


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'string',handles.Result.label);


% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
    axes(handles.axes6); imshow(handles.Result.Thin); title('细化处理');
end

【图像识别】基于不变矩的数字验证码识别含GUI界面_第1张图片

完整代码或者代写添加QQ1575304183

你可能感兴趣的:(matlab,图像处理)