canny算子 matlab代码6,求教canny算子中非极大值抑制算法的理解

以下代码是canny算子中的非极大值抑制算法

·····································································

%非极大值抑制

function Max = cannyMax(direction,ix,iy,M)

[m,n] = size(M);

% 根据梯度幅度确定各点梯度的方向,并找出四个方向可能存在边缘点的坐标。

switch direction

case 1

idx = find((iy<=0 & ix>-iy)  | (iy>=0 & ix|iy|

case 2

idx = find((ix>0 & -iy>=ix)  | (ix<0 & -iy<=ix));%|iy|>|ix|

case 3

idx = find((ix<=0 & ix>iy) | (ix>=0 & ix|ix|

case 4

idx = find((iy<0 & ix<=iy) | (iy>0 & ix>=iy));%|ix|>|iy|

end

%排除外部像素的干扰

if ~isempty(idx)%如果idx不为空

v = mod(idx,m);%求除法后的模数

extIdx = find(v==1 | v==0 | idx<=m | (idx>(n-1)*m));%找出外部像素

idx(extIdx) = [];%令外部像素为空

end

ixv = ix(idx); %得到此时方向下的ix

iyv = iy(idx); %得到此时方向下的iy

gradM = M(idx);%得到此时方向下的各点幅度,中心点

%计算4个方向的梯度幅度,得到局部最大值

switch direction

case 1

d = abs(iyv./ixv);%求绝对值,d<1

gradM1 = M(idx+m).*(1-d) + M(idx+m-1).*d; %中心右上的最大值

gradM2 = M(idx-m).*(1-d) + M(idx-m+1).*d; %中心左下的最大值,以下case中按方向同理

case 2

d = abs(ixv./iyv);

gradM1 = M(idx-1).*(1-d) + M(idx+m-1).*d;

gradM2 = M(idx+1).*(1-d) + M(idx-m+1).*d;

case 3

d = abs(ixv./iyv);

gradM1 = M(idx-1).*(1-d) + M(idx-m-1).*d;

gradM2 = M(idx+1).*(1-d) + M(idx+m+1).*d;

case 4

d = abs(iyv./ixv);

gradM1 = M(idx-m).*(1-d) + M(idx-m-1).*d;

gradM2 = M(idx+m).*(1-d) + M(idx+m+1).*d;

end

Max = idx(gradM>=gradM1 & gradM>=gradM2);%返回此时方向下局部的,即非极大值抑制的最大值

····························································································

上面的代码中,第二个switch语句case中

d = abs(iyv./ixv);

gradM1 = M(idx+m).*(1-d) + M(idx+m-1).*d; %中心右上的最大值

gradM2 = M(idx-m).*(1-d) + M(idx-m+1).*d; %中心左下的最大值,以下case中按方向同理

为什么gradM1、gradM1代表中心右上的最大值,中心左下的最大值,这个方向根据式子是怎么来的啊?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是通过非极大值抑制改进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 ``` 此代码,我们首先应用高斯滤波器对图像进行平滑处理,然后计算图像梯度和方向。接下来,我们执行非极大值抑制来获得边缘强度图像。然后,我们应用双阈值法来确定边缘像素的强度。最后,我们执行滞后阈值化来消除弱边缘像素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值