【图像边缘检测】基于matlab GUI神经网络算法边缘检测【含Matlab源码 372期】

一、简介

神经网络是所谓深度学习的一个基础,也是必备的知识点,他是以人脑中的神经网络作为启发,最著名的算法就是backpropagation算法,这里就简单的整理一下神经网络相关参数,和计算方法。

1 多层向前神经网络(Multilayer Feed-Forward Neural Network)
多层向前神经网络由一下几个部分组成:
输入层(input layer),隐藏层(Hidden layer),输出层(output layer)
【图像边缘检测】基于matlab GUI神经网络算法边缘检测【含Matlab源码 372期】_第1张图片
特点如下:
(1)每层由单元(units)组成
(2)输入层是有训练集的实例特征向量传入
(3)经过连接接点的权重(weight)传入下一层,一层的输出是下一层的输入
(4)隐藏层的个数可以是任意的,输入层有一层,输出层有一层
(5)每个单元也可以称之为神经结点,根据生物学来源定义
(6)以上成为两层的神经网络,输入层是不算在里面的
(7)一层中加权求和,然后根据非线性方程转化输出
(8)作为多层向前神经网络,理论上,如果有足够的隐藏层,和足够的训练集,可以模拟出任何方程

2 设计神经网络结构
(1)使用神经网络训练数据之前,必须确定神经网络的层数,以及每层单元的个数
(2)特征向量在被传入输入层时通常要先标准化到0-1之间(为了加速学习过程)
(3)离散型变量可以被编码成每一个输入单元对应一个特征值可能赋的值
比如:特征值A可能取三个值(a0, a1, a2), 可以使用3个输入单元来代表A。
如果A=a0, 那么代表a0的单元值就取1, 其他取0;
如果A=a1, 那么代表a1de单元值就取1,其他取0,以此类推
(4)神经网络即可以用来做分类(classification)问题,也可以解决回归(regression)问题
①对于分类问题,如果是2类,可以用一个输出单元表示(0和1分别代表2类),如果多余2类,则每一个类别用一个输出单元表示
②没有明确的规则来设计最好有多少个隐藏层,可以根据实验测试和误差以及精准度来实验并改进

3 交叉验证方法(cross-Validation)
这里有一堆数据,我们把他切成3个部分(当然还可以分的更多)
第一部分做测试集,二三部分做训练集,算出准确度;
第二部分做测试集,一三部分做训练集,算出准确度;
第三部分做测试集,一二部分做训练集,算出准确度;
之后算出三个准确度的平局值,作为最后的准确度,如下图:
【图像边缘检测】基于matlab GUI神经网络算法边缘检测【含Matlab源码 372期】_第2张图片

二、源代码

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

% Last Modified by GUIDE v2.5 16-Jul-2011 14:54:47

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

% Choose default command line output for fuzzyedge
handles.output = hObject;

a = ones(256,256);
axes(handles.axes1);
imshow(a);
axes(handles.axes2);
imshow(a);
axes(handles.axes3);
imshow(a);

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = fuzzyedge_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 clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = ones(256,256);
axes(handles.axes1);
imshow(a);
axes(handles.axes2);
imshow(a);
axes(handles.axes3);
imshow(a);


% --- Executes on button press in browse.
function browse_Callback(hObject, eventdata, handles)
% hObject    handle to browse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file path] = uigetfile('*.bmp;*.jpg;*.png','选择一幅图片');
if file==0
    warndlg('用户必须选择一个输入的图片');
else
    a = imread(fullfile(path,file));
    axes(handles.axes1);
    imshow(a);
    handles.a = a;
end
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in direction.
function direction_Callback(hObject, eventdata, handles)
% hObject    handle to direction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = handles.a;
I = double(a);
[R C P] = size(a);
%%%%%%%%%%%%% Direction Find Calculation........
for i = 2:R-1
    for j = 2:C-1
        D1(i-1,j-1) = abs(I(i-1,j-1) - I(i,j)) + abs(I(i+1,j+1) - I(i,j));
        D2(i-1,j-1) = abs(I(i-1,j) - I(i,j)) + abs(I(i+1,j) - I(i,j));
        D3(i-1,j-1) = abs(I(i-1,j+1) - I(i,j)) + abs(I(i+1,j-1) - I(i,j));
        D4(i-1,j-1) = abs(I(i,j-1) - I(i,j)) + abs(I(i,j+1) - I(i,j));
        
    end
end

handles.D1 = D1;
handles.D2 = D2;
handles.D3 = D3;
handles.D4 = D4;
handles.I = I;
% Update handles structure
guidata(hObject, handles);

warndlg('方向检测完成');


% --- Executes on button press in edgeclassfy.
function edgeclassfy_Callback(hObject, eventdata, handles)
% hObject    handle to edgeclassfy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
D1 = handles.D1;
D2 = handles.D2;
D3 = handles.D3;
D4 = handles.D4;

I = handles.I;

[R C P] = size(D1);

for i = 1:R
    for j = 1:C
        if ((D1(i,j)<=35) && (D2(i,j)<=35) && (D3(i,j)<=35) && (D4(i,j)<=35))
            New_im(i,j) = 0;  % BACKGROUND CLASS
        elseif ((D1(i,j)<=35) && (D2(i,j)>35) && (D3(i,j)>35) && (D4(i,j)>35))
            New_im(i,j) = 1;
        elseif ((D1(i,j)>35) && (D2(i,j)<=35) && (D3(i,j)>35) && (D4(i,j)>35))
            New_im(i,j) = 2;
        elseif ((D1(i,j)>35) && (D2(i,j)>35) && (D3(i,j)<=35) && (D4(i,j)>35))
            New_im(i,j) = 3;
        elseif ((D1(i,j)>35) && (D2(i,j)>35) && (D3(i,j)>35) && (D4(i,j)<=35))
            New_im(i,j) = 4;
        elseif ((D1(i,j)>35) && (D2(i,j)>35) && (D3(i,j)>35) && (D4(i,j)>35))
            New_im(i,j) = 5;  % SPECKLE CLASS
        end
    end
end
handles.New_im = New_im;
% Update handles structure
guidata(hObject, handles);
warndlg('完成');


% --- Executes on button press in competitiverule.
function competitiverule_Callback(hObject, eventdata, handles)
% hObject    handle to competitiverule (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
New_im = handles.New_im;
D1 = handles.D1;
D2 = handles.D2;
D3 = handles.D3;
D4 = handles.D4;

[r c] = size(New_im);

%%%%%%%%%%%% Finding Stronger Edges.........

for i = 2:r-1
    for j = 2:c-1
        
        switch New_im(i,j)
            case 0           %%%%%%%%%%%%%% Change to Black
                New_edge(i-1,j-1) = 0;
            case 1           %%%%%%%%%%%%% Checking the Status
                if (D3(i,j)>=D3(i+1,j-1))&&(D3(i,j)>=D3(i-1,j+1))     %%%%%%%% change to White
                    New_edge(i-1,j-1) = 1;
                else
                    New_edge(i-1,j-1) = 0;
                end
            case 2
                if (D4(i,j)>=D4(i+1,j-1))&&(D4(i,j)>=D4(i-1,j+1))     %%%%%%%% change to White
                    New_edge(i-1,j-1) = 1;
                else
                    New_edge(i-1,j-1) = 0;
                end
            case 3
                if (D1(i,j)>=D1(i+1,j-1))&&(D1(i,j)>=D1(i-1,j+1))     %%%%%%%% change to White
                    New_edge(i-1,j-1) = 1;
                else
                    New_edge(i-1,j-1) = 0;
                end
            case 4
                if (D2(i,j)>=D2(i+1,j-1))&&(D2(i,j)>=D2(i-1,j+1))     %%%%%%%% change to White
                    New_edge(i-1,j-1) = 1;
                else
                    New_edge(i-1,j-1) = 0;
                end
            case 5
                
                  New_edge(i-1,j-1) = 1;
        end
    end
end
axes(handles.axes3);
imshow(New_edge);

三、运行结果

【图像边缘检测】基于matlab GUI神经网络算法边缘检测【含Matlab源码 372期】_第3张图片

四、备注

完整代码或者代写添加QQ 1564658423

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