# Harris Corner Detector 公式推导以及具体含义

其中w是窗函数，它可以是加权函数，也可以是高斯函数

所以其中一阶可以近似为

在本质上我们可以把二次项看成一个椭圆函数，我们对M进行特征值分析有λ1，λ2

1.λ1，λ2都很小且近似，E在所以方向接近于常数；

2.λ1>>λ2,或者λ2>>λ1, E将在某一方向上很大；

3.λ1，λ2都很大且近似，E将在所有方向上很大；

1.计算图像x,y方向的梯度Ix，Iy

2.计算每个像素点的梯度平方

3.计算梯度在每个像素点的和

4.定义在每个像素点的矩阵H，也就是前面的M

5.计算每个像素的角点响应

6.设置阈值找出可能点并进行非极大值抑制

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 close all clear all   I = imread('empire.jpg'); I = rgb2gray(I); I = imresize(I,[500,300]); imshow(I);   sigma = 1; halfwid = sigma * 3;   [xx, yy] = meshgrid(-halfwid:halfwid, -halfwid:halfwid);   Gxy = exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2)); Gx = xx .* exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2)); Gy = yy .* exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2));   %%apply sobel in herizontal direction and vertical direction compute the %%gradient %fx = [-1 0 1;-1 0 1;-1 0 1]; %fy = [1 1 1;0 0 0;-1 -1 -1]; Ix = conv2(I,Gx,'same'); Iy = conv2(I,Gy,'same'); %%compute Ix2, Iy2,Ixy Ix2 = Ix.*Ix; Iy2 = Iy.*Iy; Ixy = Ix.*Iy;   %%apply gaussian filter h = fspecial('gaussian',[6,6],1); Ix2 = conv2(Ix2,h,'same'); Iy2 = conv2(Iy2,h,'same'); Ixy = conv2(Ixy,h,'same'); height = size(I,1); width = size(I,2); result = zeros(height,width); R = zeros(height,width); Rmax = 0; %% compute M matrix and corner response for i = 1:height     for j =1:width         M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy(i,j)];         R(i,j) = det(M) - 0.04*(trace(M)^2);         if R(i,j)> Rmax             Rmax = R(i,j);         end     end end %% compare whith threshold count = 0; for i = 2:height-1     for j = 2:width-1         if R(i,j) > 0.01*Rmax             result(i,j) = 1;             count = count +1;         end     end end   %non-maxima suppression result = imdilate(result, [1 1 1; 1 0 1; 1 1 1]);   [posc,posr] = find(result == 1); imshow(I); hold on; plot(posr,posc,'r.');