10

我正在实施哈里斯角落探测器的教育目的,但我卡在哈里斯响应部分。基本上,我在做什么,是:实施哈里斯角落探测器

  1. 计算在x方向和y方向的图像强度梯度
  2. (1)以上的(2)
  3. 输出
  4. 计算哈里斯响应的
  5. 模糊输出在3x3邻域中抑制(3)的输出中的非极大值,并且阈值输出

1和2似乎工作正常;然而,作为哈里斯响应,我得到的值非常小,没有一点达到阈值。输入是标准的户外摄影。

[...] 
[Ix, Iy] = intensityGradients(img); 
g = fspecial('gaussian'); 
Ix = imfilter(Ix, g); 
Iy = imfilter(Iy, g); 
H = harrisResponse(Ix, Iy); 
[...] 

function K = harrisResponse(Ix, Iy) 
    max = 0; 
    [sy, sx] = size(Ix); 
    K = zeros(sy, sx); 
    for i = 1:sx, 
     for j = 1:sy, 
      H = [Ix(j,i) * Ix(j,i), Ix(j,i) * Iy(j,i) 
       Ix(j,i) * Iy(j,i), Iy(j,i) * Iy(j,i)]; 
      K(j,i) = det(H)/trace(H); 
      if K(j,i) > max, 
       max = K(j,i); 
      end 
     end 
    end 
    max 
end 

对于样品图片,最大结束是6.4163e-018这似乎太低了。

回答

7

Harris角点检测中的一个角被定义为“区域中最高值的像素”(通常为3X35x5),所以您关于没有达到“阈值”的点的评论对我来说似乎很陌生。只需要收集比周围邻居5x5附近的所有像素值都高的像素。

除此之外: 我不是100%肯定,但我想你应该有:

K(j,i) = det(H) - lambda*(trace(H)^2) 哪里拉姆达的是,在你的情况下,工作的积极的常数(哈里斯建议值是0.04)。

一般来说唯一明智的时刻来过滤输入了这一点之前:

[Ix, Iy] = intensityGradients(img);

过滤Ix2Iy2Ixy没有多大意义了我。

而且,我觉得你的代码示例是这里错了(不函数harrisResponse有两个或三个输入变量):

H = harrisResponse(Ix2, Ixy, Iy2); 
[...] 

function K = harrisResponse(Ix, Iy) 
+0

我已经恢复到不再过滤Ix2等,因此在stackoverflow的副本中留下了一些错误。 – Etan 2010-10-05 12:50:44

+0

问题是我没有总结3x3方块中的所有像素来找出Ix2等;相反,我刚刚使用了相应的像素。在改变H之后,它总结了所有9个像素的所有Ix2,Ixy和Iy2,看起来非常好。 – Etan 2010-10-05 12:52:16

+1

det(H)/ trace(H)是一种在没有lambda的情况下使用的近似值。 – Etan 2010-10-05 12:52:41

3

建议的实施是非常低效的。 让我们计算梯度(其可以也被优化)之后开始:

A = Ix.^2; 
B = Iy.^2; 
C = (Ix.*Iy).^4; 
lambda = 0.04; 

H = (A.*B - C) - lambda*(A+B).^2; 

% if you really need max: 
max(H(:)) 

不需要循环,因为Matlab的讨厌环路。

+2

但是为什么要计算'C =(Ix。* Iy)。^ 4'而不是简单的'C =(Ix。* Iy)'? – 2015-03-31 09:37:22

0

在计算机视觉系统工具箱中有一个称为detectHarrisFeatures的功能。

3

基本上,Harris角点检测将有5个步骤:

  1. 梯度计算
  2. 高斯平滑
  3. 哈里斯量度计算
  4. 非最大抑制
  5. 阈值

如果你正在执行MATLAB,将会很容易理解算法并获得结果。

MATLAB的下面的代码可以帮助你解决你的疑惑:

% Step 1: Compute derivatives of image 
Ix = conv2(im, dx, 'same'); 
Iy = conv2(im, dy, 'same'); 

% Step 2: Smooth space image derivatives (gaussian filtering) 
Ix2 = conv2(Ix .^ 2, g, 'same'); 
Iy2 = conv2(Iy .^ 2, g, 'same'); 
Ixy = conv2(Ix .* Iy, g, 'same'); 

% Step 3: Harris corner measure 
harris = (Ix2 .* Iy2 - Ixy .^ 2) ./ (Ix2 + Iy2); 

% Step 4: Find local maxima (non maximum suppression) 
mx = ordfilt2(harris, size .^ 2, ones(size)); 

% Step 5: Thresholding 
harris = (harris == mx) & (harris > threshold); 
1

,我与Python实现的解决方案,它为我工作,我希望你找到你在找什么

import numpy as np 
import matplotlib.pyplot as plt 
from PIL.Image import * 
from scipy import ndimage 

def imap1(im): 
    print('testing the picture . . .') 
    a = Image.getpixel(im, (0, 0)) 
    if type(a) == int: 
     return im 
    else: 
     c, l = im.size 
     imarr = np.asarray(im) 
     neim = np.zeros((l, c)) 
     for i in range(l): 
      for j in range(c): 
       t = imarr[i, j] 
       ts = sum(t)/len(t) 
       neim[i, j] = ts 
     return neim 

def Harris(im): 
    neim = imap1(im) 
    imarr = np.asarray(neim, dtype=np.float64) 
    ix = ndimage.sobel(imarr, 0) 
    iy = ndimage.sobel(imarr, 1) 
    ix2 = ix * ix 
    iy2 = iy * iy 
    ixy = ix * iy 
    ix2 = ndimage.gaussian_filter(ix2, sigma=2) 
    iy2 = ndimage.gaussian_filter(iy2, sigma=2) 
    ixy = ndimage.gaussian_filter(ixy, sigma=2) 
    c, l = imarr.shape 
    result = np.zeros((c, l)) 
    r = np.zeros((c, l)) 
    rmax = 0 
    for i in range(c): 
     print('loking for corner . . .') 
     for j in range(l): 
      print('test ',j) 
      m = np.array([[ix2[i, j], ixy[i, j]], [ixy[i, j], iy2[i, j]]], dtype=np.float64) 
      r[i, j] = np.linalg.det(m) - 0.04 * (np.power(np.trace(m), 2)) 
      if r[i, j] > rmax: 
       rmax = r[i, j] 
    for i in range(c - 1): 
     print(". .") 
     for j in range(l - 1): 
      print('loking') 
      if r[i, j] > 0.01 * rmax and r[i, j] > r[i-1, j-1] and r[i, j] > r[i-1, j+1]\ 
            and r[i, j] > r[i+1, j-1] and r[i, j] > r[i+1, j+1]: 
       result[i, j] = 1 

    pc, pr = np.where(result == 1) 
    plt.plot(pr, pc, 'r+') 
    plt.savefig('harris_test.png') 
    plt.imshow(im, 'gray') 
    plt.show() 
    # plt.imsave('harris_test.png', im, 'gray') 

im = open('chess.png') 
Harris(im)