以下代码是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代表中心右上的最大值,中心左下的最大值,这个方向根据式子是怎么来的啊?