我正在做一个程序,用哈里斯方法检测角落方法的输出显示角点我想做的是对图像应用阈值这是我的代码:
% 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
的像素因此,所有较暗的像素不能是边缘。边框掩码确保只接受到图像边框的距离大于
radius
的像素。[r, c] = find(cimmx)
提供角点像素的行和列。