一 实现Harris特征点提取
使用Matlab自己实现Harris特征点提取
function [ result,cnt] = Harris( img )
%R=det(M)-k.*tr(M).^2 k=0.04~0.06 we set k=0.04
[m,n]=size(img);
ori_img=img;
img=double(img);
h=[-2 -1 0 1 2];
Ix=filter2(h,img); % gradient on x
Iy=filter2(h',img); % gradient on y
Ixx=Ix.^2;
Iyy=Ix.^2;
Ixy=Ix.*Iy;
Gauss=fspecial('gaussian',[7 7],2); %window 7*7 ;sigma=2
Ixx=filter2(Gauss,Ixx);
Iyy=filter2(Gauss,Iyy);
Ixy=filter2(Gauss,Ixy);
% matrix instead of loop to compute matrix R
k=0.06;
Mdet=Ixx.*Iyy-Ixy.^2;
Mtr=Ixx+Iyy;
R=Mdet-k.*Mtr.^2;
% k=0.06;
% R=zeros(m,n);
% for i=1:m
% for j=1:n
% M=[Ixx(i,j) Ixy(i,j);Ixy(i,j) Iyy(i,j)];
% R(i,j)=det(M)-k*(trace(M))^2;
% end;
% end;
Rmax=max(R(:));
T=0.01*Rmax;
result=zeros(m,n);
%searh the matrix R, if R(i,j)>T as well as the surrounding points,R(i,j) is
%a corner point
cnt=0;
for i=2:m-1
for j=2:n-1
if (R(i, j) > T && R(i, j) > R(i-1, j-1) && R(i, j) > R(i-1, j) && R(i, j) > R(i-1, j+1) && R(i, j) > R(i, j-1) && ...
R(i, j) > R(i, j+1) && R(i, j) > R(i+1, j-1) && R(i, j) > R(i+1, j) && R(i, j) > R(i+1, j+1))
result(i,j)=255;
cnt=cnt+1;
end;
end;
end;
%plot the corner points
[r,c]=find(result==255);
figure;
imshow(ori_img);
hold on;
plot(c,r,'r+');
end
将以上代码改写成OpenCV+C++
#include<opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
Mat img=imread("E:/corner.jpg",0); // read as uchar 1 channel the directory name /
//cout<<img1.at<uchar>(1,1)<<endl;
namedWindow("image",WINDOW_AUTOSIZE