canny边缘检测算法matlab代码
时间: 2023-10-02 18:09:35 浏览: 23
以下是Canny边缘检测算法的MATLAB代码:
```matlab
function [edgeImg,thresh] = myCannyEdgeDetection(img,sigma,threshLow,threshHigh)
% 高斯滤波
G = fspecial('gaussian', [5 5], sigma);
img = imfilter(img, G, 'same');
% 计算梯度幅值和方向
sobel_x = [-1 0 1;-2 0 2;-1 0 1];
sobel_y = [1 2 1;0 0 0;-1 -2 -1];
Gx = imfilter(img, sobel_x, 'same');
Gy = imfilter(img, sobel_y, 'same');
magnitude = sqrt(Gx.^2 + Gy.^2);
theta = atan2(Gy, Gx) * 180/pi;
% 非极大值抑制
[nRow,nCol] = size(magnitude);
edgeImg = zeros(nRow,nCol);
for i = 2:nRow-1
for j = 2:nCol-1
if (theta(i,j)<0)
theta(i,j) = theta(i,j)+360;
end
if ((theta(i,j)>=0) && (theta(i,j)<45)) || ((theta(i,j)>=180) && (theta(i,j)<225))
if (magnitude(i,j)>magnitude(i-1,j) && magnitude(i,j)>magnitude(i+1,j))
edgeImg(i,j) = magnitude(i,j);
end
elseif ((theta(i,j)>=45) && (theta(i,j)<90)) || ((theta(i,j)>=225) && (theta(i,j)<270))
if (magnitude(i,j)>magnitude(i-1,j-1) && magnitude(i,j)>magnitude(i+1,j+1))
edgeImg(i,j) = magnitude(i,j);
end
elseif ((theta(i,j)>=90) && (theta(i,j)<135)) || ((theta(i,j)>=270) && (theta(i,j)<315))
if (magnitude(i,j)>magnitude(i,j-1) && magnitude(i,j)>magnitude(i,j+1))
edgeImg(i,j) = magnitude(i,j);
end
elseif ((theta(i,j)>=135) && (theta(i,j)<180)) || ((theta(i,j)>=315) && (theta(i,j)<360))
if (magnitude(i,j)>magnitude(i-1,j+1) && magnitude(i,j)>magnitude(i+1,j-1))
edgeImg(i,j) = magnitude(i,j);
end
end
end
end
% 双阈值处理
maxMagnitude = max(max(edgeImg));
thresh = (threshLow/maxMagnitude) * 100;
edgeImg(edgeImg<threshLow) = 0;
edgeImg(edgeImg>threshHigh) = 255;
edgeImg((edgeImg>=threshLow) & (edgeImg<=threshHigh)) = 128;
% 双阈值连接
for i = 2:nRow-1
for j = 2:nCol-1
if (edgeImg(i,j)==128)
if (edgeImg(i-1,j-1)==255 || edgeImg(i-1,j)==255 || edgeImg(i-1,j+1)==255 || edgeImg(i,j-1)==255 || edgeImg(i,j+1)==255 || edgeImg(i+1,j-1)==255 || edgeImg(i+1,j)==255 || edgeImg(i+1,j+1)==255)
edgeImg(i,j) = 255;
else
edgeImg(i,j) = 0;
end
end
end
end
% 显示图像
figure;
subplot(1,2,1);
imshow(img);
title('原图像');
subplot(1,2,2);
imshow(edgeImg);
title('Canny边缘检测结果');
end
```
其中,`img`是输入的图像,`sigma`是高斯滤波器的标准差,`threshLow`和`threshHigh`是双阈值处理的两个阈值,`edgeImg`是输出的边缘图像,`thresh`是计算得到的使用的阈值。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)