最近一个项目需要进行图像的角点检测,但是用opencv提供的各种接口效果并不是很理想,于是想探究一下它们的实现,所以看了其源码并且摘录下来,整理了一下,本想在源码的基础上再修改修改但无耐没有思路。以下使自己的整理:
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv\cv.h>
#include <iomanip>
#include <math.h>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace cv;
using namespace std;
#define SHI_TOMASI 0
#define HARRIS 1
template<typename T> struct greaterThanPtr
{
bool operator()(const T* a, const T* b) const { return *a > *b; }
};
void mycalcMinEigenVal( const Mat& _cov, Mat& _dst )
{
_dst=Mat(_cov.size(),CV_32FC1);
int i, j;
Size size = _cov.size();
if( _cov.isContinuous() && _dst.isContinuous() )
{
size.width *= size.height;
size.height = 1;
}
for( i = 0; i < size.height; i++ )
{
const float* cov = (const float*)( _cov.data + _cov.step*i);
float* dst = (float*)( _dst.data + _dst.step*i);
j = 0;
for( ; j < size.width; j++ )
{
float a = cov[j*3]*0.5f;
float b = cov[j*3+1];
float c = cov[j*3+2]*0.5f;
dst[j] = (float)((a + c) - std::sqrt((a - c)*(a - c) + b*b));
}
}
}
void mycalcHarris( const Mat& _cov, Mat& _dst, double k )
{
_dst=Mat(_cov.size(),CV_32FC1);
int i, j;
Size size = _cov.size();
if( _cov.isContinuous() && _dst.isContinuous() )
{
size.width *= size.height;
size.height = 1;
}
for( i = 0; i < size.height; i++ )
{
const float* cov = (const float*)(_cov.data + _cov.step*i);
float* dst = (float*)(_dst.data + _dst.step*i);
j = 0;
for( ; j < size.width; j++ )
{
float a = cov[j*3];
float b = cov[j*3+1];
float c = cov[j*3+2];
dst[j] = (float)(a*c - b*b - k*(a + c)*(a + c));
}
}
}
void mycornerDetection(const Mat& src, Mat& eigenv, int block_size,int aperture_size, int op_type, double k,int borderType=BORDER_DEFAULT )
{
Mat Dx, Dy;
if( aperture_size > 0 )
{
Sobel( src, Dx, C