Matlab-分割與邊緣檢測實驗-使用一階和二階檢測算子(導數)進行圖像的邊緣檢測
代碼鏈接:https://download.csdn.net/download/qq_43571150/12033266
使用一階和二階檢測算子(導數)進行圖像的邊緣檢測
問題1:編寫程序實現一階Sobel算子,進行圖像的邊緣提取;
問題2:編寫程序實現一階Prewitt算子,進行圖像的邊緣提取;
問題3:編寫程序實現一階Roberts算子,進行圖像的邊緣提取;
問題4:編寫程序實現二階Laplacian算子(3*3),進行圖像的邊緣提取。
圖像結果👇
Matlab代碼👇
clear all;
A=imread('01.jpg');
R=rgb2gray(A);
I=double(R);
hx1=[-1 -2 -1;
0 0 0;
1 2 1]; %生產垂直梯度模板
hy1=[-1 0 1; %生產水平梯度模板
-2 0 2;
-1 0 1];
hx2=[-1 -1 -1;
0 0 0;
1 1 1]; %生產垂直梯度模板
hy2=[-1 0 1; %生產水平梯度模板
-1 0 1;
-1 0 1];
hx3=[-1 0;
0 1]; %生產垂直梯度模板
hy3=[ 0 -1; %生產水平梯度模板
1 0];
hx4=[ 0 -1 0; %生產水平梯度模板
-1 4 -1;
0 -1 0];
hx5=[-1 -1 -1; %生產水平梯度模板
-1 8 -1;
-1 -1 -1];
grad1=Edge(I,hx1,hy1);
grad2=Edge(I,hx2,hy2);
grad3=Edge(I,hx3,hy3);
grad4=Laplacian(I,hx4);
grad5=Laplacian(I,hx5);
p1=uint8(grad1);
p2=uint8(grad2);
p3=uint8(grad3);
p4=uint8(grad4);
p5=uint8(grad5);
subplot(3,3,1);imshow(A);title('input');
subplot(3,3,2);imshow(R);title('灰度圖');
subplot(3,3,4);imshow(p1,[]);title('一階Sobel算子');
subplot(3,3,5);imshow(p2,[]);title('一階Prewitt算子');
subplot(3,3,6);imshow(p3,[]);title('一階Roberts算子');
subplot(3,3,7);imshow(p4,[]);title('二階Laplacian算子四鄰域');
subplot(3,3,8);imshow(p5,[]);title('二階Laplacian算子八鄰域');
imwrite(p1,'01 一階Sobel算子邊緣提取.jpg');
imwrite(p2,'01 一階Prewitt算子邊緣提取.jpg');
imwrite(p3,'01 一階Roberts算子.jpg');
imwrite(p4,'01 二階Laplacian算子四鄰域邊緣提取.jpg');
imwrite(p5,'01 二階Laplacian算子八鄰域邊緣提取.jpg');
function p = Edge(I,hx,hy)
gradx=filter2(hx,I,'same');
gradx=abs(gradx); %計算圖像的垂直梯度
grady=filter2(hy,I,'same');
grady=abs(grady); %計算圖像的水平梯度
p=gradx+grady; %得到圖像的梯度
end
function p = Laplacian(I,hx)
gradx=filter2(hx,I,'same');
gradx=abs(gradx); %計算圖像的垂直梯度
p=gradx; %得到圖像的梯度
end