我正在编写一个使用 Harris 方法检测角点的程序。该方法的输出显示角点。我想要做的是对图像应用阈值处理。这是我的代码:
% Create a mask for deteting the vertical edges
verticalMask = [-1 0 1;
-2 0 2;
-1 0 1]* 0.25;
% Create a mask for deteting the horizontal edges
horisontalMask = [-1 -2 -1;
0 0 0;
1 2 1]* 0.25;
% Create a mask for Gaussian filter(is used to improve the result)
gaussianFilter= [1 4 1;
4 7 4;
1 4 1].*(1/27);
K = 0.04; % The sensitivity factor used in the Harris detection algorithm (Used to detect
sharp corners).
% Get the gradient of the image [Ix,Iy], using the convulation function
Ix = conv2(grayImage,verticalMask);
Iy = conv2(grayImage,horisontalMask);
% get the input arguments of the harris formula
Ix2 = Ix.* Ix; % get Ix to the power of two
Iy2 = Iy.* Iy; % get Iy to the power of two
Ixy = Ix .* Iy; %get the Ixy by multiply Ix and Iy
% Apply the gaussian filter to the the arguments
Ix2 = conv2(Ix2,gaussianFilter);
Iy2 = conv2(Iy2,gaussianFilter);
Ixy = conv2(Ixy,gaussianFilter);
% Enetr the arguments into the formula
C = (Ix2 .* Iy2) - (Ixy.^2) - K * ( Ix2 + Iy2 ).^ 2;
现在,我想将阈值应用于公式的输出 C。 我找到了一个我试过的代码,它工作得很好,但如果有人能解释一下,我想先理解它。(对于 thresh 和 radius 变量,我改变了它们的值以便它可以与我的图像一起工作)。
thresh = 0.000999;
radius = 1;
sze = 2*radius + 1; % Size of dilation mask
mx = ordfilt2(cim, sze^2, ones(sze)); % Grey-scale dilate
% Make mask to exclude points on borders
bordermask = zeros(size(cim));
bordermask(radius+1:end-radius, radius+1:end-radius) = 1;
% Find maxima, threshold, and apply bordermask
cimmx = (cim==mx) & (cim>thresh) & bordermask;
[r, c] = find(cimmx); % Return coordinates of corners
figure, imshow(im),
hold on;
plot(c, r, '+');
hold off;
最佳答案
首先,图像 cim
的每个像素都被替换为其最大邻居的值。
邻居罩被定义为大小为 sze
的正方形。这会扩大图像,即明亮的图像区域变厚。见matlab doc
mx = ordfilt2(cim, sze^2, ones(sze)); % Grey-scale dilate
cim==mx
表示您只接受原始图像和拨号图像中相同的像素。这仅包括其邻域大小为 sze
的最大值的像素。
cim>thresh
意味着您只考虑值大于 thresh
的像素。因此,所有较暗的像素都不可能是边缘。
边框 mask 确保您只接受到图像边框的距离大于 radius
的像素。
[r, c] = find(cimmx)
为您提供角像素的行和列。
关于matlab - 哈里斯角点检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562284/