Matlab/OpenCV自己实现Harris特征点提取和匹配

本文介绍了如何使用Matlab和OpenCV自行实现Harris角点检测算法。Harris角点检测具有良好的抗旋转和仿射变换能力,但不适用于尺度变化。内容包括算法实现步骤及性能分析。
摘要由CSDN通过智能技术生成

一 实现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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值