我正在使用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;
答案 0 :(得分:2)
首先,图像cim
的每个像素都被其最大邻居的值替换。
邻居引擎被定义为大小为sze
的正方形。这使图像扩张,即明亮的图像区域变厚。见matlab doc
mx = ordfilt2(cim, sze^2, ones(sze)); % Grey-scale dilate
cim==mx
表示您只接受原始图像和拨号图像中相同的像素。这仅包括其大小为sze
的最大值的像素。
cim>thresh
表示您只考虑值大于thresh
的像素。因此,所有较暗的像素都不能是边缘。
边框蒙版确保您只接受距图像边界大于radius
的像素。
[r, c] = find(cimmx)
为您提供角点像素的行和列。