赞
踩
9种边缘算子
Canny算子
Sobel算子
Susan算子
Prewitt算子
Laplace算子
LOG算子
Roberts算子
Kirsch算子
普通梯度方法
其中,
roberts,canny,sobel,log,prewitt在matlab中都可直接用一行代码来实现:
BW = edge(I,method,threshold)
main.m
clc; clear; close all;
Img = imread('CXK.png');
Img=rgb2gray(Img);
Img_canny = edge(Img,'canny'); %canny
Img_roberts = edge(Img,'roberts'); %reberts
Img_prewitt = edge(Img,'prewitt'); %prewitt
Img_sobel=edge(Img,'sobel'); %sobel
Img_log=edge(Img,'log'); %log
Img_lap=laplace(Img,0.05); %laplace
Img_susan=susan(Img,0.2); %susan
Img_kir=kirsch(Img); %kirsch
[Img_x,Img_y,Img_t]=grd(Img); %普通梯度法
figure();
subplot(231);imshow(Img_lap);title('laplace');
subplot(232);imshow(Img_log);title('log');
subplot(233);imshow(Img_canny);title('canny');
subplot(234);imshow(Img_prewitt);title('prewitt');
subplot(235);imshow(Img_sobel);title('sobel');
subplot(236);imshow(Img_roberts);title('roberts');
figure();
subplot(1,2,1);imshow(Img);title('原图');
subplot(1,2,2);imshow(Img_kir);title('kirsch');
figure();
subplot(1,2,1);imshow(Img);title('原图');
subplot(1,2,2);imshow(Img_susan);title('susan');
figure()
subplot(1,3,1);imshow(Img_x);title('gradient-x');
subplot(1,3,2);imshow(Img_y);title('gradient-y');
subplot(1,3,3);imshow(Img_t);title('gradient-t');
susan.m
function image_out = susan(im,threshold)
% 功能:实现运用SUNSAN算子进行边缘检测
% 输入:image_in-输入的待检测的图像
% threshold-阈值
% 输出:image_out-检测边缘出的二值图像
% 将输入的图像矩阵转换成double型
d = length(size(im));
if d==3
image=double(rgb2gray(im));
elseif d==2
image=double(im);
end
% 建立SUSAN模板
mask = ([ 0 0 1 1 1 0 0 ;0 1 1 1 1 1 0;1 1 1 1 1 1 1;1 1 1 1 1 1 1;1 1 1 1 1 1 1;0 1 1 1 1 1 0;0 0 1 1 1 0 0]);
R=zeros(size(image));
% 定义USAN 区域
nmax = 3*37/4;
[a b]=size(image);
new=zeros(a+7,b+7);
[c d]=size(new);
new(4:c-4,4:d-4)=image;
for i=4:c-4
for j=4:d-4
current_image = new(i-3:i+3,j-3:j+3);
current_masked_image = mask.*current_image;
% 调用susan_threshold函数进行阈值比较处理
current_thresholded = susan_threshold(current_masked_image,threshold);
g=sum(current_thresholded(:));
if nmax
R(i,j) = g-nmax;
else
R(i,j) = 0;
end
end
end
image_out=R(4:c-4,4:d-4);
susan_threshold.m
function thresholded = susan_threshold(image,threshold)
% 功能:设定SUSAN算法的阈值
[a b]=size(image);
intensity_center = image((a+1)/2,(b+1)/2);
temp1 = (image-intensity_center)/threshold;
temp2 = temp1.^6;
thresholded = exp(-1*temp2);
kirsch.m
function Img_kir=kirsch(Img)
%---------------------------------------------------------------
%对图象进行预处理
%对图象进行均值滤波
Img2=filter2(fspecial('average',3),Img);
%对图象进行高斯滤波
Img3=filter2(fspecial('gaussian'),Img2);
%利用小波变换对图象进行降噪处理
[thr,sorh,keepapp]=ddencmp('den','wv',Img3); %获得除噪的缺省参数
Img4=wdencmp('gbl',Img3,'sym4',2,thr,sorh,keepapp);%图象进行降噪处理
%---------------------------------------------------------------------
%提取图象边缘
t=[0.8 1.0 1.5 2.0 2.5].*10^5 ; %设定阈值
Img5=double(Img4);
[m,n]=size(Img5);
g=zeros(m,n);
d=zeros(1,8);
%利用Kirsch算子进行边缘提取
for i=2:m-1
for j=2:n-1
d(1) =(5*Img5(i-1,j-1)+5*Img5(i-1,j)+5*Img5(i-1,j+1)-3*Img5(i,j-1)-3*Img5(i,j+1)-3*Img5(i+1,j-1)-3*Img5(i+1,j)-3*Img5(i+1,j+1))^2;
d(2) =((-3)*Img5(i-1,j-1)+5*Img5(i-1,j)+5*Img5(i-1,j+1)-3*Img5(i,j-1)+5*Img5(i,j+1)-3*Img5(i+1,j-1)-3*Img5(i+1,j)-3*Img5(i+1,j+1))^2;
d(3) =((-3)*Img5(i-1,j-1)-3*Img5(i-1,j)+5*Img5(i-1,j+1)-3*Img5(i,j-1)+5*Img5(i,j+1)-3*Img5(i+1,j-1)-3*Img5(i+1,j)+5*Img5(i+1,j+1))^2;
d(4) =((-3)*Img5(i-1,j-1)-3*Img5(i-1,j)-3*Img5(i-1,j+1)-3*Img5(i,j-1)+5*Img5(i,j+1)-3*Img5(i+1,j-1)+5*Img5(i+1,j)+5*Img5(i+1,j+1))^2;
d(5) =((-3)*Img5(i-1,j-1)-3*Img5(i-1,j)-3*Img5(i-1,j+1)-3*Img5(i,j-1)-3*Img5(i,j+1)+5*Img5(i+1,j-1)+5*Img5(i+1,j)+5*Img5(i+1,j+1))^2;
d(6) =((-3)*Img5(i-1,j-1)-3*Img5(i-1,j)-3*Img5(i-1,j+1)+5*Img5(i,j-1)-3*Img5(i,j+1)+5*Img5(i+1,j-1)+5*Img5(i+1,j)-3*Img5(i+1,j+1))^2;
d(7) =(5*Img5(i-1,j-1)-3*Img5(i-1,j)-3*Img5(i-1,j+1)+5*Img5(i,j-1)-3*Img5(i,j+1)+5*Img5(i+1,j-1)-3*Img5(i+1,j)-3*Img5(i+1,j+1))^2;
d(8) =(5*Img5(i-1,j-1)+5*Img5(i-1,j)-3*Img5(i-1,j+1)+5*Img5(i,j-1)-3*Img5(i,j+1)-3*Img5(i+1,j-1)-3*Img5(i+1,j)-3*Img5(i+1,j+1))^2;
g(i,j) = max(d);
end
end
%显示边缘提取后的图象
for k=1:5
for i=1:m
for j=1:n
if g(i,j)>t(k)
Img_kir(i,j)=255;
else
Img_kir(i,j)=0;
end
end
end
end
laplace.m
function Img_lap=laplace(Img,threshold)
lenna_3=mat2gray(Img); %图像矩阵的归一化
[m,n]=size(lenna_3);
lenna_4=lenna_3; %保留图像的边缘一个像素
L=0;
t=threshold; %设定阈值
%Laplace算子
for j=2:m-1
for k=2:n-1
L=abs(4*lenna_3(j,k)-lenna_3(j-1,k)-lenna_3(j+1,k)-lenna_3(j,k+1)-lenna_3(j,k-1));
if(L > t)
Img_lap(j,k)=255; %白
else
Img_lap(j,k)=0; %黑
end
end
end
grd.m
function [Img_x,Img_y,Img_t]=grd(Img)
I=double(Img);
[x, y]=gradient(I);
t=sqrt(x.^2+y.^2);
Img_x=I;Img_y=I;Img_t=I;
Img_t(t>10)=255;
Img_t(t<10)=0;
%figure,imshow(G);title('gradient-t');
Img_x(x>2)=255;
Img_x(x<2)=0;
%figure,imshow(G);title('gradient-x');
Img_y(y>2)=255;
Img_y(y<2)=0;
%figure,imshow(G);title('gradient-y');
使用的原图如下:
CXK.png
结果如下:
laplace,log,canny,prewitt,sobel,roberts
kirsch
普通梯度法
另外附上彩蛋:GIF逐帧检测识别
CXK_dt2.gif
gif_detect.m
path = 'CXK2.gif';
for i=1:80 %取其中1-80帧
frame = imread(path,'gif','frame',i);
frame_edge = edge(frame,'roberts');
[I,map]=gray2ind(frame_edge,256);
if(i==1)
imwrite(I,map,'CXK_dt2.gif','DelayTime',0.1,'LoopCount',Inf)
else
imwrite(I,map,'CXK_dt2.gif','WriteMode','append','DelayTime',0.1)
end
end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。