通过非极大值抑制改进Canny算子的Matlab代码
时间: 2024-05-10 07:18:38 浏览: 126
以下是通过非极大值抑制改进Canny算子的Matlab代码:
```matlab
function [edgeImage] = canny_nms(image, sigma, lowThreshold, highThreshold)
% Step 1: Apply Gaussian filter to the image
kernelSize = ceil(sigma*3)*2 + 1; % Calculate kernel size based on sigma
gaussianFilter = fspecial('gaussian', kernelSize, sigma); % Create Gaussian filter
filteredImage = imfilter(image, gaussianFilter, 'replicate'); % Apply Gaussian filter
% Step 2: Calculate image gradients
[Gx, Gy] = gradient(filteredImage);
magnitude = sqrt(Gx.^2 + Gy.^2);
orientation = atan2(Gy, Gx) * 180 / pi; % Convert to degrees
% Step 3: Non-maximum suppression
nmsImage = zeros(size(filteredImage));
for i = 2:size(filteredImage,1)-1
for j = 2:size(filteredImage,2)-1
if (orientation(i,j) < 0) % Convert negative angles to positive angles
orientation(i,j) = orientation(i,j) + 180;
end
if ((orientation(i,j) >= 0 && orientation(i,j) < 22.5) || (orientation(i,j) >= 157.5 && orientation(i,j) < 180))
if (magnitude(i,j) >= magnitude(i,j-1) && magnitude(i,j) >= magnitude(i,j+1))
nmsImage(i,j) = magnitude(i,j);
end
elseif ((orientation(i,j) >= 22.5 && orientation(i,j) < 67.5) || (orientation(i,j) >= 112.5 && orientation(i,j) < 157.5))
if (magnitude(i,j) >= magnitude(i-1,j-1) && magnitude(i,j) >= magnitude(i+1,j+1))
nmsImage(i,j) = magnitude(i,j);
end
elseif ((orientation(i,j) >= 67.5 && orientation(i,j) < 112.5))
if (magnitude(i,j) >= magnitude(i-1,j) && magnitude(i,j) >= magnitude(i+1,j))
nmsImage(i,j) = magnitude(i,j);
end
end
end
end
% Step 4: Apply double thresholding
highThreshold = max(nmsImage(:)) * highThreshold;
lowThreshold = highThreshold * lowThreshold;
edgeImage = zeros(size(nmsImage));
strongEdgesRow = [];
strongEdgesCol = [];
for i = 2:size(nmsImage,1)-1
for j = 2:size(nmsImage,2)-1
if (nmsImage(i,j) >= highThreshold)
edgeImage(i,j) = 1;
strongEdgesRow = [strongEdgesRow i];
strongEdgesCol = [strongEdgesCol j];
elseif (nmsImage(i,j) >= lowThreshold && nmsImage(i,j) < highThreshold)
if (nmsImage(i-1,j-1) >= highThreshold || nmsImage(i-1,j) >= highThreshold || nmsImage(i-1,j+1) >= highThreshold || nmsImage(i,j-1) >= highThreshold || nmsImage(i,j+1) >= highThreshold || nmsImage(i+1,j-1) >= highThreshold || nmsImage(i+1,j) >= highThreshold || nmsImage(i+1,j+1) >= highThreshold)
edgeImage(i,j) = 1;
strongEdgesRow = [strongEdgesRow i];
strongEdgesCol = [strongEdgesCol j];
end
end
end
end
% Step 5: Hysteresis thresholding
while ~isempty(strongEdgesRow)
currentRow = strongEdgesRow(1);
currentCol = strongEdgesCol(1);
strongEdgesRow(1) = [];
strongEdgesCol(1) = [];
if (currentRow > 1 && currentRow < size(edgeImage,1) && currentCol > 1 && currentCol < size(edgeImage,2))
for i = -1:1
for j = -1:1
if (edgeImage(currentRow+i, currentCol+j) == 0 && nmsImage(currentRow+i, currentCol+j) >= lowThreshold)
edgeImage(currentRow+i, currentCol+j) = 1;
strongEdgesRow = [strongEdgesRow currentRow+i];
strongEdgesCol = [strongEdgesCol currentCol+j];
end
end
end
end
end
end
```
此代码中,我们首先应用高斯滤波器对图像进行平滑处理,然后计算图像梯度和方向。接下来,我们执行非极大值抑制来获得边缘强度图像。然后,我们应用双阈值法来确定边缘像素的强度。最后,我们执行滞后阈值化来消除弱边缘像素。
阅读全文