基于Matlab的彩色特征提取-边缘检测/质点检测

这里写自定义目录标题

  • 基于Matlab的彩色特征提取-边缘检测/质点检测-图像处理
    • 边缘检测
    • 角点检测
    • 部分代码
    • 运行结果

基于Matlab的彩色特征提取-边缘检测/质点检测-图像处理

本期介绍一个小软件的制作----《基于maltab的彩色图像特征提取》。
边沿检测分别用三种方式:Sobel、Roberts、Prewitt。角点检测用:Harris、Susan。

边缘检测

边缘检测 是为了将其周围像素灰度有阶跃变化的像素检测出来,这些像素组成的集合就是该图像的边缘。比较常用的边缘检测方法就是考察每个像素在某个领域内灰度的变化,然后利用边缘临近一阶或二阶方向导数变化规律检测边缘,即边缘检测局部算法。来源网络)

角点检测

一般的角点检测 都是对有具体定义的、或者是能够具体检测出来的兴趣点的检测。这意味着兴趣点可以是角点,也可以是在某些属性上强度最大或者最小的孤立点、线段的终点,或者是曲线上局部曲率最大的点。在实践中,通常大部分称为角点检测的方法检测的都是兴趣点,而不是独有的角点。因此,如果只要检测角点的话,需要对检测出来的兴趣点进行局部检测,以确定出哪些是真正的角点。(来源网络)

部分代码

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

% Last Modified by GUIDE v2.5 06-May-2022 23:44:19

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = untitled_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)
[fname,pname,index] = uigetfile({'*.jpg';'*.bmp'},'选择图片');
str = [pname fname];
global a
a= imread(str);
axes(handles.axes1)
set(handles.axes1,'Visible','on')
imshow(a);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)

a1=get(handles.radiobutton1,'Value');
a2=get(handles.radiobutton2,'Value');
a3=get(handles.radiobutton3,'Value');
b1=get(handles.radiobutton4,'Value');
b2=get(handles.radiobutton5,'Value');
if a1==1
    f=getframe(handles.axes3);
    
imwrite(f.cdata,['image\','Sobel边缘检测图.jpg'])
elseif a2==1 
    f=getframe(handles.axes3);
imwrite(f.cdata,['image\','Roberts边缘检测图.jpg'])
elseif a3==1
      f=getframe(handles.axes3);
imwrite(f.cdata,['image\','Prewitt边缘检测图.jpg']) 
elseif b1==1
      f=getframe(handles.axes3);
imwrite(f.cdata,['image\','Harris角点检测图.jpg']) 
elseif b2==1
      f=getframe(handles.axes3);
imwrite(f.cdata,['image\','Susan角点检测图.jpg']) 
end 
   

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% sourcePic=imread('bianyuan.jpg');      %读取原图像
global a
grayPic=mat2gray(a);         %实现图像矩阵的归一化操作
[m,n]=size(grayPic);
newGrayPic=grayPic;                %为保留图像的边缘一个像素
sobelNum=0;                       %经sobel算子计算得到的每个像素的值
sobelThreshold=0.2;                 %设定阈值
for j=2:m-1                        %进行边界提取
    for k=2:n-1
sobelNum=abs(grayPic(j-1,k+1)+2*grayPic(j,k+1)+grayPic(j+1,k+1)-grayPic(j-1,k-1)-2*grayPic(j,k-1)-grayPic(j+1,k-1))+abs(grayPic(j-1,k-1)+2*grayPic(j-1,k)+grayPic(j-1,k+1)-grayPic(j+1,k-1)-2*grayPic(j+1,k)-grayPic(j+1,k+1));
        if(sobelNum > sobelThreshold)
            newGrayPic(j,k)=255;
        else
            newGrayPic(j,k)=0;
        end
    end
end
axes(handles.axes3)
imshow(newGrayPic);


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
global a
% sourcePic=imread('bianyuan.jpg');        %读取原图像
grayPic=mat2gray(a);           %实现图像矩阵的归一化操作
[m,n]=size(grayPic);
newGrayPic=grayPic;                  %为保留图像的边缘一个像素
robertsNum=0;                        %经Roberts算子计算得到的每个像素的值
robertThreshold=0.2;                   %设定阈值
for j=1:m-1                           %进行边界提取
    for k=1:n-1
      robertsNum=abs(grayPic(j,k)-grayPic(j+1,k+1))+abs(grayPic(j+1,k)-grayPic(j,k+1));
      if(robertsNum > robertThreshold)
            newGrayPic(j,k)=255;
       else
            newGrayPic(j,k)=0;
      end
    end
end
axes(handles.axes3)
imshow(newGrayPic);


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
global a
% sourcePic=imread('1.jpg');       %读取原图像
grayPic=mat2gray(a);    %实现图像矩阵的归一化操作
[m,n]=size(grayPic);
newGrayPic=grayPic;           %为保留图像的边缘一个像素
PrewittNum=0;                %经Prewitt算子计算得到的每个像素的值
PrewittThreshold=0.8;          %设定阈值
for j=2:m-1                   %进行边界提取
for k=2:n-1
PrewittNum=abs(grayPic(j-1,k+1)-grayPic(j+1,k+1)+grayPic(j-1,k)-grayPic(j+1,k)+grayPic(j-1,k-1)-grayPic(j+1,k-1))+abs(grayPic(j-1,k+1)+grayPic(j,k+1)+grayPic(j+1,k+1)-grayPic(j-1,k-1)-grayPic(j,k-1)-grayPic(j+1,k-1));
        if(PrewittNum > PrewittThreshold)
            newGrayPic(j,k)=255;
        else
            newGrayPic(j,k)=0;
        end
    end
end
axes(handles.axes3)
imshow(newGrayPic);
% imwrite(newGrayPic,'Prewitt0.8.jpg')%保存


% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
global a
ori_im2=rgb2gray(a);    
 %ori_im2=imresize(ori_im2',0.50,'bicubic');  %加上这句图就变成竖着的了  
 
fx = [5 0 -5;8 0 -8;5 0 -5];          % % la gaucienne,ver axe x
Ix = filter2(fx,ori_im2);              % la convolution vers axe x
fy = [5 8 5;0 0 0;-5 -8 -5];          % la gaucienne,ver axe y
Iy = filter2(fy,ori_im2);              % la convolution vers axe y
Ix2 = Ix.^2;
Iy2 = Iy.^2;
Ixy = Ix.*Iy;
clear Ix;
clear Iy;
 
h= fspecial('gaussian',[3 3],2);      % générer une fonction gaussienne,sigma=2
 
Ix2 = filter2(h,Ix2);
Iy2 = filter2(h,Iy2);
Ixy = filter2(h,Ixy);
 
height = size(ori_im2,1);
width = size(ori_im2,2);
result = zeros(height,width);         % enregistrer la position du coin
 
R = zeros(height,width);
 
K=0.04;
Rmax = 0;                              % chercher la valeur maximale de R
for i = 1:height
    for j = 1:width
        M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)];         
        R(i,j) = det(M)-K*(trace(M))^2;                     % % calcule R
        if R(i,j) > Rmax
           Rmax = R(i,j);
        end
    end
end
 
cnt = 0;
for i = 2:height-1
    for j = 2:width-1
        % réduire des valuers minimales ,la taille de fenetre 3*3
        if R(i,j) > 0.01*Rmax && R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) && R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) && R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1)
            result(i,j) = 1;
            cnt = cnt+1;
        end
    end
end
 
[posr2, posc2] = find(result == 1);
cnt;                                      % compter des coins
axes(handles.axes3)
imshow(ori_im2);
hold on;
plot(posc2,posr2,'w*');



运行结果

1.边缘检测运行结果图
Sobel算子边缘检测结果:
基于Matlab的彩色特征提取-边缘检测/质点检测_第1张图片
Roberts算子边缘检测结果:
基于Matlab的彩色特征提取-边缘检测/质点检测_第2张图片
Prewitt算子边缘检测结果:
基于Matlab的彩色特征提取-边缘检测/质点检测_第3张图片
2.角点检测运行结果图
Harris算子角点检测结果:
基于Matlab的彩色特征提取-边缘检测/质点检测_第4张图片
Susan算子角点检测结果:
基于Matlab的彩色特征提取-边缘检测/质点检测_第5张图片
2.保存图片运行结果图
基于Matlab的彩色特征提取-边缘检测/质点检测_第6张图片
喜欢的话戳下面,可私信咨询。
下载链接:https://download.csdn.net/download/m0_61505845/86834829

你可能感兴趣的:(matlab,开发语言,算法)