MATLAB实现canny边缘检测算法的代码
时间: 2023-10-02 08:09:43 浏览: 78
以下是MATLAB实现canny边缘检测算法的代码,仅供参考:
%% Canny Edge Detection Algorithm
% Define input image
I = imread('test_image.jpg');
% Convert image to grayscale
gray_I = rgb2gray(I);
% Define parameters for edge detection
sigma = 1.5; % Gaussian filter sigma value
Th = 0.1; % Low threshold value
Th_H = 0.2; % High threshold value
% Apply Gaussian filter to the input image
g = fspecial('gaussian',[3 3],sigma);
smooth_I = imfilter(gray_I,g,'same');
% Compute gradient magnitude and direction
[Ix, Iy] = imgradientxy(smooth_I,'sobel');
% Compute gradient direction
theta = atan2(Iy,Ix);
% Perform non-maximum suppression
[suppressed_I, suppressed_theta] = nonmaxsupp(Ix,Iy,theta);
% Thresholding
edge_map = double(suppressed_I > Th_H);
% Hysteresis thresholding
edge_map = hystthresh(edge_map,Th,Th_H);
% Display the final edge map
imshow(edge_map);
function [suppressed_I, suppressed_theta] = nonmaxsupp(Ix, Iy, theta)
% Obtain the input image size
[rows,cols] = size(Ix);
% Initialize the output image
suppressed_I = zeros(rows,cols);
% Compute non-maximum suppression
for i = 2:rows-1
for j = 2:cols-1
% Check the gradient direction and interpolate the gradient magnitude
% along the direction perpendicular to the direction
if (theta(i,j) >= -pi/8 && theta(i,j) < pi/8) || (theta(i,j) >= 7*pi/8 && theta(i,j) <= pi) || (theta(i,j) >= -pi && theta(i,j) < -7*pi/8)
if Ix(i,j) > Ix(i,j-1) && Ix(i,j) > Ix(i,j+1)
suppressed_I(i,j) = Ix(i,j);
suppressed_theta(i,j) = theta(i,j);
end
elseif (theta(i,j) >= pi/8 && theta(i,j) < 3*pi/8) || (theta(i,j) >= -7*pi/8 && theta(i,j) < -5*pi/8)
if Ix(i,j) > Ix(i-1,j+1) && Ix(i,j) > Ix(i+1,j-1)
suppressed_I(i,j) = Ix(i,j);
suppressed_theta(i,j) = theta(i,j);
end
elseif (theta(i,j) >= 3*pi/8 && theta(i,j) < 5*pi/8) || (theta(i,j) >= -5*pi/8 && theta(i,j) < -3*pi/8)
if Ix(i,j) > Iy(i-1,j) && Ix(i,j) > Iy(i+1,j)
suppressed_I(i,j) = Ix(i,j);
suppressed_theta(i,j) = theta(i,j);
end
elseif (theta(i,j) >= 5*pi/8 && theta(i,j) < 7*pi/8) || (theta(i,j) >= -3*pi/8 && theta(i,j) < -pi/8)
if Ix(i,j) > Ix(i-1,j-1) && Ix(i,j) > Ix(i+1,j+1)
suppressed_I(i,j) = Ix(i,j);
suppressed_theta(i,j) = theta(i,j);
end
end
end
end
end
function edge_map = hystthresh(edge_map,Th, Th_H)
% Obtain the input image size
[rows,cols] = size(edge_map);
% Initialize the output image
edge_map = double(edge_map);
% Perform hysteresis thresholding
for i = 2:rows-1
for j = 2:cols-1
if edge_map(i,j) > Th_H % Strong edge pixel
edge_map(i,j) = 1;
elseif edge_map(i,j) < Th % Weak edge pixel
edge_map(i,j) = 0;
elseif edge_map(i-1,j-1) > Th_H || edge_map(i-1,j) > Th_H || edge_map(i-1,j+1) > Th_H || edge_map(i,j-1) > Th_H || edge_map(i,j+1) > Th_H || edge_map(i+1,j-1) > Th_H || edge_map(i+1,j) > Th_H || edge_map(i+1,j+1) > Th_H
edge_map(i,j) = 1;
end
end
end
end
相关推荐
![](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)