Canny算子代码matlab代码
时间: 2023-12-18 11:03:53 浏览: 95
以下是Canny算子的Matlab代码示例:
```matlab
% 读取图像
img = imread('image.jpg');
% 转换为灰度图像
gray_img = rgb2gray(img);
% 应用高斯滤波器
gauss_img = imgaussfilt(gray_img, 1.5);
% 计算梯度幅值和方向
[grad_mag, grad_dir] = imgradient(gauss_img);
% 应用非极大值抑制
nms_img = imhmax(grad_mag, 0);
% 应用双阈值边缘检测
low_thresh = 0.1 * max(nms_img(:));
high_thresh = 0.2 * max(nms_img(:));
edge_img = edge(nms_img, 'canny', [low_thresh high_thresh]);
% 显示结果
imshow(edge_img);
```
注意,这只是一个简单的示例代码,您可能需要根据您的具体需求进行调整和修改。
相关问题
通过非极大值抑制改进Canny算子的Matlab代码
以下是通过非极大值抑制改进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
```
此代码中,我们首先应用高斯滤波器对图像进行平滑处理,然后计算图像梯度和方向。接下来,我们执行非极大值抑制来获得边缘强度图像。然后,我们应用双阈值法来确定边缘像素的强度。最后,我们执行滞后阈值化来消除弱边缘像素。
Canny算子锐化matlab的程序
以下是使用Canny算子进行图像锐化的MATLAB代码示例:
```matlab
% 读取图像
img = imread('image.jpg');
% 将图像转换为灰度图像
gray_img = rgb2gray(img);
% 使用Canny算子进行边缘检测
canny_img = edge(gray_img, 'canny');
% 将边缘图像与原始图像进行加权融合,得到锐化后的图像
sharp_img = imadd(im2double(img), canny_img);
% 显示结果
subplot(1,2,1), imshow(img), title('原始图像');
subplot(1,2,2), imshow(sharp_img), title('锐化后的图像');
```
在上述代码中,首先读取原始图像,并将其转换为灰度图像。然后使用MATLAB内置的`edge`函数,选择Canny算子进行边缘检测。接着将边缘图像与原始图像进行加权融合,得到锐化后的图像。最后使用`subplot`函数将原始图像和锐化后的图像显示在同一窗口中。
阅读全文