本文介绍了试图找到/理解Harris Corners的正确实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试了解如何计算Harris Corner M,如中所定义,他们在你的帖子中写了原始函数: %哈里斯 - 哈里斯角落检测器%%用法:[cim,r,c] = harris(im,sigma,thresh ,radius,disp)%%参数:%im - 要处理的图像。 %sigma - 平滑高斯的标准偏差。要使用的典型%值可能是1-3。 %thresh - threshold(可选)。尝试一个值~1000。 %radius - 在非最大%抑制中考虑的区域半径(可选)。使用的典型值可能是%为1-3。 %disp - 可选标志(0或1),表示您是否希望%显示覆盖在原始%图像上的角落。这对参数调整很有用。 %%返回:%cim - 二进制图像标记角。 %r - 角点的行坐标。 %c - 角点的列坐标。 %%如果参数列表中省略了thresh和radius,则cim返回%作为原始角点图像,r和c返回为空。 %参考:%C.G。哈里斯和M.J. Stephens。 一个组合的角落和边缘探测器,%Proceedings Fourth Alvey Vision Conference,Manchester。 %pp 147-151,1988。%%作者:%Peter Kovesi %计算机科学与技术系软件工程%西澳大利亚大学%[email protected] www.cs.uwa.edu.au/~pk %%2002年3月 函数[cim,r,c] = harris(im,sigma,thresh,radius,disp) error(nargchk(2,5,nargin)); dx = [-1 0 1; -1 0 1; -1 0 1]; %Derivative mask dy = dx'; %' Ix = conv2(im,dx,'same'); %图像衍生物 Iy = conv2(im,dy,'same'); %生成大小为6 * sigma(+/- 3sigma)和%最小大小1x1的高斯滤波器。 g = fspecial('gaussian',max(1,fix(6 * sigma)),sigma); Ix2 = conv2(Ix。^ 2,g,'相同'); %平滑平方图像导数 Iy2 = conv2(Iy。^ 2,g,'相同'); Ixy = conv2(Ix。* Iy,g,'same'); cim =(Ix2。* Iy2 - Ixy。^ 2)./(Ix2 + Iy2 + eps); %Harris角测量 %Alternate Harris角度测量用于某些人。建议%k = 0.04 - 我发现这有点任意且不令人满意。 %cim =(Ix2。* Iy2 - Ixy。^ 2) - k *(Ix2 + Iy2)。^ 2; if nargin> 2%我们应该执行非最大抑制和阈值 %通过执行灰度形态%扩张来提取局部最大值,然后在角点强度图像中找到%与之匹配的点扩张的图像也大于阈值。 sze = 2 * radius + 1; %面具的大小。 mx = ordfilt2(cim,sze ^ 2,ones(sze)); %灰度扩张。 cim =(cim == mx)&(cim> thresh); %查找最大值。 [r,c] = find(cim); %查找行,col coords。 if nargin == 5& disp%覆盖原始图像上的角 figure,imagesc(im),轴图像,colormap(灰色),保持 plot(c,r,'ys'),title('corner detected') ; 结束 else%将cim作为角力图像并使r和c为空。 r = []; c = []; 结束 cim 是计算的相关矩阵,或者每个像素位置的 M(x,y) (x,y)图片。我可以看到你的混乱来源。在此代码中,计算 M(x,y)的窗口假定为1 x 1.在您引用我的链接中,窗口实际上是5 x 5对于您在代码中查看的每个点。如果你想扩展它以使相关矩阵包含5 x 5像素,可以想到这样的事情: %/ / ......... %//从之前 - 需要修改以适应窗口大小%使用sigma值$ b生成大小为5 x 5的高斯滤波器$ bg = fspecial('gaussian',5,sigma); Ix2 = conv2(Ix。^ 2,g,'相同'); %平滑平方图像导数 Iy2 = conv2(Iy。^ 2,g,'相同'); Ixy = conv2(Ix。* Iy,g,'same'); %//新增 - 在计算cim kernel =之前添加这个(5,5); Ix2 = conv2(Ix2,内核,'相同'); %要包含5 x 5个补丁 Iy2 = conv2(Iy2,内核,'相同'); Ixy = conv2(Ixy,kernel,'same'); %//继续使用原始代码.... cim =(Ix2。* Iy2 - Ixy。^ 2)./(Ix2 + Iy2 + eps); %Harris角测量 %// ..... conv2 在输入和 kernel ,这是本地补丁和内核的加权和。在这种情况下,我们需要总结每个 Ix2 , Iy2 和 Ixy 以尊重 M 中的符号。如果我们在5 x 5窗口中将内核指定为全1,则实际上这是将所有值一起添加并为图像中的每个位置输出此总和。现在,在链接中它表示内核是高斯。该文档说你可以用高斯预过滤你的图像,然后只是累积窗口,而代码目前正在这样做。但是,您需要确保Gaussian的窗口大小与文档所说的相同,因此我们也将其更改为5 x 5. 您现在可以计算 cim 或相关矩阵 M(x,y)正常现在应该包含像素总和一个5 x 5窗口的 Ix2 , Iy2 和 Ixy 仅使用元素操作。我们更改后, Ix2 , Iy2 和 Ixy 的每个元素该代码计算5 x 5窗口内的导数值的总和,其中每个变量中的每个像素标记该位置作为中心的总和。在此之后,一旦你计算 cim ,这将为你提供每个像素的 M(x,y) im 。 现在,其余代码执行所谓的非最大抑制。这可确保您删除可能存在误报的角点。这意味着您要查看图像补丁并确定此补丁中的最大值。如果此修补程序中的此最大值等于此修补程序的中心 和,如果此最大值超过阈值,则保留此点。这正是代码的这一部分: sze = 2 * radius + 1; %面具的大小。 mx = ordfilt2(cim,sze ^ 2,ones(sze)); %灰度扩张。 cim =(cim == mx)&(cim> thresh); %查找最大值。 ordfilt2 是一个订单统计过滤器,其中第一个输入是您想要的图像,第二个输入是订单统计你正在寻找,第三个输入是你想要处理的像素的邻域。这告诉我们您需要最大订单统计或 sze ^ 2 ,这与最大值相对应包含在 sze x sze = ones(sze)邻域中。 代码的下一部分: [r,c] = find(cim); %查找行,col coords。 if nargin == 5& disp%覆盖原始图像上的角 figure,imagesc(im),轴图像,colormap(灰色),保持 plot(c,r,'ys'),title('corner detected') ; 结束 ...找到通过非的那些点的确切行和列坐标 - 最大抑制,如果需要,在图像上显示这些点。 这简单地解释了Harris Corner Detection ...希望有所帮助! I'm trying to understand how to compute the Harris Corner M, as defined inhttps://courses.cs.washington.edu/courses/cse455/07wi/homework/hw3/It seems that you need to sum over a bunch of patches.However, I've seen lots of implementations that do something like this: R = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;from: http://web.engr.illinois.edu/~slazebni/spring14/harris.mThere is no summing and you never look at a patch.These don't look equivalent to me. For example, the "R" value for pixel 5,5 is just Ix2, Iy2, and Ixy value squared just for that pixel. However, the math seems to suggest you sum over a patch over, say, pixel 5,5. Which implementation is correct? Both? Are they equivalent?Note: Ix2 = squared gradient of Image I in x directionIy2 is the same except in y directionIxy = Ix.*IyAlso, the .* or .^ is matlab notation indicating point-wise multiplication or exponentiation. 解决方案 For self-containment, the calculation of the Harris Corners is based on the calculation of the correlation matrix M:For each pixel in the image, you want to collect a N x N pixel window weighted by Gaussian weights and compute the response value R at the location (x,y) in the image by:Values of R that surpass a threshold are deemed to be interest points. Ix and Iy are the horizontal and vertical derivatives respectively. Now, the code you're concerned about I'm going to put it into this post for self-containment. BTW, this should be credited to Peter Kovesi who wrote the original function as linked in your post:% HARRIS - Harris corner detector%% Usage: [cim, r, c] = harris(im, sigma, thresh, radius, disp)%% Arguments:% im - image to be processed.% sigma - standard deviation of smoothing Gaussian. Typical% values to use might be 1-3.% thresh - threshold (optional). Try a value ~1000.% radius - radius of region considered in non-maximal% suppression (optional). Typical values to use might% be 1-3.% disp - optional flag (0 or 1) indicating whether you want% to display corners overlayed on the original% image. This can be useful for parameter tuning.%% Returns:% cim - binary image marking corners.% r - row coordinates of corner points.% c - column coordinates of corner points.%% If thresh and radius are omitted from the argument list 'cim' is returned% as a raw corner strength image and r and c are returned empty.% Reference:% C.G. Harris and M.J. Stephens. "A combined corner and edge detector",% Proceedings Fourth Alvey Vision Conference, Manchester.% pp 147-151, 1988.%% Author:% Peter Kovesi% Department of Computer Science & Software Engineering% The University of Western Australia% [email protected] www.cs.uwa.edu.au/~pk%% March 2002function [cim, r, c] = harris(im, sigma, thresh, radius, disp)error(nargchk(2,5,nargin));dx = [-1 0 1; -1 0 1; -1 0 1]; % Derivative masksdy = dx'; %'Ix = conv2(im, dx, 'same'); % Image derivativesIy = conv2(im, dy, 'same');% Generate Gaussian filter of size 6*sigma (+/- 3sigma) and of% minimum size 1x1.g = fspecial('gaussian',max(1,fix(6*sigma)), sigma);Ix2 = conv2(Ix.^2, g, 'same'); % Smoothed squared image derivativesIy2 = conv2(Iy.^2, g, 'same');Ixy = conv2(Ix.*Iy, g, 'same');cim = (Ix2.*Iy2 - Ixy.^2)./(Ix2 + Iy2 + eps); % Harris corner measure% Alternate Harris corner measure used by some. Suggested that% k=0.04 - I find this a bit arbitrary and unsatisfactory.% cim = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;if nargin > 2 % We should perform nonmaximal suppression and threshold% Extract local maxima by performing a grey scale morphological% dilation and then finding points in the corner strength image that% match the dilated image and are also greater than the threshold.sze = 2*radius+1; % Size of mask.mx = ordfilt2(cim,sze^2,ones(sze)); % Grey-scale dilate.cim = (cim==mx)&(cim>thresh); % Find maxima.[r,c] = find(cim); % Find row,col coords.if nargin==5 & disp % overlay corners on original image figure, imagesc(im), axis image, colormap(gray), hold on plot(c,r,'ys'), title('corners detected');endelse % leave cim as a corner strength image and make r and c empty.r = []; c = [];endcim is the correlation matrix that is computed, or M(x,y) for each pixel location (x,y) in the image. I can see where your source of confusion is. In this code, the windows to compute M(x,y) are assumed to be 1 x 1. In the link you referenced me to, the windows are actually 5 x 5 for each point you're looking at in the code. If you want to extend this so that the correlation matrix incorporates 5 x 5 pixels, something like this comes to mind:%//.........%// From before - Need to modify to accommodate for window size% Generate Gaussian filter of size 5 x 5 with sigma valueg = fspecial('gaussian', 5, sigma);Ix2 = conv2(Ix.^2, g, 'same'); % Smoothed squared image derivativesIy2 = conv2(Iy.^2, g, 'same');Ixy = conv2(Ix.*Iy, g, 'same');%// New - add this before the computation of cimkernel = ones(5,5);Ix2 = conv2(Ix2, kernel, 'same'); % To incorporate 5 x 5 patchesIy2 = conv2(Iy2, kernel, 'same');Ixy = conv2(Ixy, kernel, 'same');%// Continue with original code....cim = (Ix2.*Iy2 - Ixy.^2)./(Ix2 + Iy2 + eps); % Harris corner measure%//.....conv2 performs convolution between an input and kernel, which is a weighted sum of local patches and the kernel. In this case, we need to sum over each of Ix2, Iy2 and Ixy to respect the notation in M. If we specify the kernel to be all 1s in a 5 x 5 window, this is essentially adding all of the values together and outputting this sum for each location in the image. Now, in the link it says that the kernel is Gaussian. The document says you can pre-filter your images with a Gaussian and just accumulate the windows after that, and the code currently is doing that. However, you need to make sure that the window size of the Gaussian is the same as what the document says, so we change this to 5 x 5 as well.You can now compute cim or the correlation matrix M(x,y) as normal which should now incorporate the summation of pixels of a 5 x 5 window for Ix2, Iy2 and Ixy using just element-wise operations. Each element of Ix2, Iy2 and Ixy once we change the code computes a summation of derivative values within a 5 x 5 window where each pixel in each variable marks the summation where that location served as the centre. After this point, once you compute cim, this would thus give you M(x,y) for each pixel in im.Now, the rest of the code performs what is called non-maximum suppression. This ensures that you remove corner points that are potentially false positives. What that means is that you take a look at image patches and determine what the maximum value is in this patch. If this maximum value in this patch is equal to the centre of this patch and if this maximum value surpasses a threshold, you keep this point. This is exactly what this part of the code does:sze = 2*radius+1; % Size of mask.mx = ordfilt2(cim,sze^2,ones(sze)); % Grey-scale dilate.cim = (cim==mx)&(cim>thresh); % Find maxima.ordfilt2 is an order-statistics filter where the first input is the image you want, the second input is the order-statistic you are looking for and the third input is the neighbourhood of pixels you want to process. This is telling us that you want the largest order-statistics or sze^2, which corresponds to the maximum value contained in a sze x sze = ones(sze) neighbourhood.The next part of the code:[r,c] = find(cim); % Find row,col coords.if nargin==5 & disp % overlay corners on original image figure, imagesc(im), axis image, colormap(gray), hold on plot(c,r,'ys'), title('corners detected');end... finds the exact row and column coordinates of those points that passed non-maximum suppression and shows these points on the image if so desired.That explains Harris Corner Detection in a nutshell... hope that helps! 这篇关于试图找到/理解Harris Corners的正确实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 22:10