opencv: 角点检测源码分析;
以下6个函数是opencv有关角点检测的函数 ConerHarris, cornoerMinEigenVal,CornorEigenValsAndVecs, preConerDetect, conerSubPix, goodFeaturesToTracks, 其中, 前三个都调用静态函数cornerEigenValsVecs
1、静态函数cornerEigenValsVecs;
static void
cornerEigenValsVecs( const Mat& src, Mat& eigenv, int block_size,
int aperture_size, int op_type, double k=.,
int borderType=BORDER_DEFAULT )
{
#ifdef HAVE_TEGRA_OPTIMIZATION
if (tegra::useTegra() && tegra::cornerEigenValsVecs(src, eigenv, block_size, aperture_size, op_type, k, borderType))
return;
#endif
#if CV_TRY_AVX
bool haveAvx = CV_CPU_HAS_SUPPORT_AVX;
#endif
#if CV_SIMD128
bool haveSimd = hasSIMD128();
#endif int depth = src.depth();
double scale = (double)( << ((aperture_size > ? aperture_size : ) - )) * block_size;
if( aperture_size < )
scale *= 2.0;
if( depth == CV_8U )
scale *= 255.0;
scale = 1.0/scale; CV_Assert( src.type() == CV_8UC1 || src.type() == CV_32FC1 ); Mat Dx, Dy;
if( aperture_size > )
{
Sobel( src, Dx, CV_32F, , , aperture_size, scale, , borderType );
Sobel( src, Dy, CV_32F, , , aperture_size, scale, , borderType );
}
else
{
Scharr( src, Dx, CV_32F, , , scale, , borderType );
Scharr( src, Dy, CV_32F, , , scale, , borderType );
} Size size = src.size();
Mat cov( size, CV_32FC3 );
int i, j; for( i = ; i < size.height; i++ )
{
float* cov_data = cov.ptr<float>(i);
const float* dxdata = Dx.ptr<float>(i);
const float* dydata = Dy.ptr<float>(i); #if CV_TRY_AVX
if( haveAvx )
j = cornerEigenValsVecsLine_AVX(dxdata, dydata, cov_data, size.width);
else
#endif // CV_TRY_AVX
j = ; #if CV_SIMD128
if( haveSimd )
{
for( ; j <= size.width - v_float32x4::nlanes; j += v_float32x4::nlanes )
{
v_float32x4 v_dx = v_load(dxdata + j);
v_float32x4 v_dy = v_load(dydata + j); v_float32x4 v_dst0, v_dst1, v_dst2;
v_dst0 = v_dx * v_dx;
v_dst1 = v_dx * v_dy;
v_dst2 = v_dy * v_dy; v_store_interleave(cov_data + j * , v_dst0, v_dst1, v_dst2);
}
}
#endif // CV_SIMD128 for( ; j < size.width; j++ )
{
float dx = dxdata[j];
float dy = dydata[j]; cov_data[j*] = dx*dx;
cov_data[j*+] = dx*dy;
cov_data[j*+] = dy*dy;
}
} //盒式均值滤波;
boxFilter(cov, cov, cov.depth(), Size(block_size, block_size),
Point(-,-), false, borderType ); if( op_type == MINEIGENVAL )
calcMinEigenVal( cov, eigenv ); //最小特征值; 如果最小的特征值都满足角点的要求,那么说明是角点,并且是强角点;
else if( op_type == HARRIS )
calcHarris( cov, eigenv, k );
else if( op_type == EIGENVALSVECS )
calcEigenValsVecs( cov, eigenv );
}
2、preConerDetect函数分析;
preConerDetect: 角点检测的特征图, 利用二阶导数求解角点,二阶导数为零表示角点;
计算Dx^2Dyy + Dy^2Dxx - 2DxDyDxy
void cv::preCornerDetect( InputArray _src, OutputArray _dst, int ksize, int borderType )
{
CV_INSTRUMENT_REGION() int type = _src.type();
CV_Assert( type == CV_8UC1 || type == CV_32FC1 ); CV_OCL_RUN( _src.dims() <= && _dst.isUMat(),
ocl_preCornerDetect(_src, _dst, ksize, borderType, CV_MAT_DEPTH(type))) Mat Dx, Dy, D2x, D2y, Dxy, src = _src.getMat();
_dst.create( src.size(), CV_32FC1 );
Mat dst = _dst.getMat(); Sobel( src, Dx, CV_32F, , , ksize, , , borderType );
Sobel( src, Dy, CV_32F, , , ksize, , , borderType );
Sobel( src, D2x, CV_32F, , , ksize, , , borderType );
Sobel( src, D2y, CV_32F, , , ksize, , , borderType );
Sobel( src, Dxy, CV_32F, , , ksize, , , borderType ); double factor = << (ksize - );
if( src.depth() == CV_8U )
factor *= ;
factor = ./(factor * factor * factor);
#if CV_SIMD128
float factor_f = (float)factor;
bool haveSimd = hasSIMD128();
v_float32x4 v_factor = v_setall_f32(factor_f), v_m2 = v_setall_f32(-2.0f);
#endif Size size = src.size();
int i, j;
for( i = ; i < size.height; i++ )
{
float* dstdata = dst.ptr<float>(i);
const float* dxdata = Dx.ptr<float>(i);
const float* dydata = Dy.ptr<float>(i);
const float* d2xdata = D2x.ptr<float>(i);
const float* d2ydata = D2y.ptr<float>(i);
const float* dxydata = Dxy.ptr<float>(i); j = ; #if CV_SIMD128
if (haveSimd)
{
for( ; j <= size.width - v_float32x4::nlanes; j += v_float32x4::nlanes )
{
v_float32x4 v_dx = v_load(dxdata + j);
v_float32x4 v_dy = v_load(dydata + j); v_float32x4 v_s1 = (v_dx * v_dx) * v_load(d2ydata + j);
v_float32x4 v_s2 = v_muladd((v_dy * v_dy), v_load(d2xdata + j), v_s1);
v_float32x4 v_s3 = v_muladd((v_dy * v_dx) * v_load(dxydata + j), v_m2, v_s2); v_store(dstdata + j, v_s3 * v_factor);
}
}
#endif for( ; j < size.width; j++ )
{
float dx = dxdata[j];
float dy = dydata[j];
dstdata[j] = (float)(factor*(dx*dx*d2ydata[j] + dy*dy*d2xdata[j] - *dx*dy*dxydata[j])); //计算特征图;
}
}
}
3、cornorSubPix函数分析;
角点的真实位置并不一定在整数像素位置,因而为了获取更为精确的角点位置坐标,需要角点坐标达到亚像素级精度;
原理: https://xueyayang.github.io/pdf_posts/%E4%BA%9A%E5%83%8F%E7%B4%A0%E8%A7%92%E7%82%B9%E7%9A%84%E6%B1%82%E6%B3%95.pdf
//亚像素级角点检测;
void cv::cornerSubPix( InputArray _image, InputOutputArray _corners,
Size win, Size zeroZone, TermCriteria criteria )
{
CV_INSTRUMENT_REGION() const int MAX_ITERS = ;
int win_w = win.width * + , win_h = win.height * + ;
int i, j, k;
int max_iters = (criteria.type & CV_TERMCRIT_ITER) ? MIN(MAX(criteria.maxCount, ), MAX_ITERS) : MAX_ITERS;
double eps = (criteria.type & CV_TERMCRIT_EPS) ? MAX(criteria.epsilon, .) : ;
eps *= eps; // use square of error in comparsion operations cv::Mat src = _image.getMat(), cornersmat = _corners.getMat();
int count = cornersmat.checkVector(, CV_32F);
CV_Assert( count >= );
Point2f* corners = cornersmat.ptr<Point2f>(); if( count == )
return; CV_Assert( win.width > && win.height > );
CV_Assert( src.cols >= win.width* + && src.rows >= win.height* + );
CV_Assert( src.channels() == ); Mat maskm(win_h, win_w, CV_32F), subpix_buf(win_h+, win_w+, CV_32F);
float* mask = maskm.ptr<float>(); for( i = ; i < win_h; i++ )
{
float y = (float)(i - win.height)/win.height;
float vy = std::exp(-y*y);
for( j = ; j < win_w; j++ )
{
float x = (float)(j - win.width)/win.width;
mask[i * win_w + j] = (float)(vy*std::exp(-x*x));
}
} // make zero_zone
if( zeroZone.width >= && zeroZone.height >= &&
zeroZone.width * + < win_w && zeroZone.height * + < win_h )
{
for( i = win.height - zeroZone.height; i <= win.height + zeroZone.height; i++ )
{
for( j = win.width - zeroZone.width; j <= win.width + zeroZone.width; j++ )
{
mask[i * win_w + j] = ;
}
}
} // do optimization loop for all the points
for( int pt_i = ; pt_i < count; pt_i++ )
{
Point2f cT = corners[pt_i], cI = cT;
int iter = ;
double err = ; do
{
Point2f cI2;
double a = , b = , c = , bb1 = , bb2 = ; getRectSubPix(src, Size(win_w+, win_h+), cI, subpix_buf, subpix_buf.type());
const float* subpix = &subpix_buf.at<float>(,); // process gradient
for( i = , k = ; i < win_h; i++, subpix += win_w + )
{
double py = i - win.height; for( j = ; j < win_w; j++, k++ )
{
double m = mask[k];
double tgx = subpix[j+] - subpix[j-];
double tgy = subpix[j+win_w+] - subpix[j-win_w-];
double gxx = tgx * tgx * m;
double gxy = tgx * tgy * m;
double gyy = tgy * tgy * m;
double px = j - win.width; a += gxx;
b += gxy;
c += gyy; bb1 += gxx * px + gxy * py;
bb2 += gxy * px + gyy * py;
}
} double det=a*c-b*b;
if( fabs( det ) <= DBL_EPSILON*DBL_EPSILON )
break; // 2x2 matrix inversion
double scale=1.0/det;
cI2.x = (float)(cI.x + c*scale*bb1 - b*scale*bb2);
cI2.y = (float)(cI.y - b*scale*bb1 + a*scale*bb2);
err = (cI2.x - cI.x) * (cI2.x - cI.x) + (cI2.y - cI.y) * (cI2.y - cI.y);
cI = cI2;
if( cI.x < || cI.x >= src.cols || cI.y < || cI.y >= src.rows )
break;
}
while( ++iter < max_iters && err > eps ); // if new point is too far from initial, it means poor convergence.
// leave initial point as the result
if( fabs( cI.x - cT.x ) > win.width || fabs( cI.y - cT.y ) > win.height )
cI = cT; corners[pt_i] = cI;
}
}
4、goodFeaturesToTrack函数分析;
goodFeaturesToTrack是对hariis的一种改进算法---Shi-Tomasi角点检测算子。
void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize,
bool useHarrisDetector, double harrisK )
{
CV_INSTRUMENT_REGION() CV_Assert( qualityLevel > && minDistance >= && maxCorners >= );
CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) ); CV_OCL_RUN(_image.dims() <= && _image.isUMat(),
ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
_mask, blockSize, useHarrisDetector, harrisK)) Mat image = _image.getMat(), eig, tmp;
if (image.empty())
{
_corners.release();
return;
} // Disabled due to bad accuracy
CV_OVX_RUN(false && useHarrisDetector && _mask.empty() &&
!ovx::skipSmallImages<VX_KERNEL_HARRIS_CORNERS>(image.cols, image.rows),
openvx_harris(image, _corners, maxCorners, qualityLevel, minDistance, blockSize, harrisK)) if( useHarrisDetector )
cornerHarris( image, eig, blockSize, , harrisK );
else
cornerMinEigenVal( image, eig, blockSize, ); double maxVal = ;
minMaxLoc( eig, , &maxVal, , , _mask );
threshold( eig, eig, maxVal*qualityLevel, , THRESH_TOZERO );
dilate( eig, tmp, Mat()); Size imgsize = image.size();
std::vector<const float*> tmpCorners; // collect list of pointers to features - put them into temporary image
Mat mask = _mask.getMat();
for( int y = ; y < imgsize.height - ; y++ )
{
const float* eig_data = (const float*)eig.ptr(y);
const float* tmp_data = (const float*)tmp.ptr(y);
const uchar* mask_data = mask.data ? mask.ptr(y) : ; for( int x = ; x < imgsize.width - ; x++ )
{
float val = eig_data[x];
if( val != && val == tmp_data[x] && (!mask_data || mask_data[x]) )
tmpCorners.push_back(eig_data + x);
}
} std::vector<Point2f> corners;
size_t i, j, total = tmpCorners.size(), ncorners = ; if (total == )
{
_corners.release();
return;
} std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() ); if (minDistance >= ) //邻域处理;
{
// Partition the image into larger grids
int w = image.cols;
int h = image.rows; const int cell_size = cvRound(minDistance);
const int grid_width = (w + cell_size - ) / cell_size;
const int grid_height = (h + cell_size - ) / cell_size; std::vector<std::vector<Point2f> > grid(grid_width*grid_height); minDistance *= minDistance; for( i = ; i < total; i++ )
{
int ofs = (int)((const uchar*)tmpCorners[i] - eig.ptr());
int y = (int)(ofs / eig.step);
int x = (int)((ofs - y*eig.step)/sizeof(float)); bool good = true; int x_cell = x / cell_size;
int y_cell = y / cell_size; int x1 = x_cell - ;
int y1 = y_cell - ;
int x2 = x_cell + ;
int y2 = y_cell + ; // boundary check
x1 = std::max(, x1);
y1 = std::max(, y1);
x2 = std::min(grid_width-, x2);
y2 = std::min(grid_height-, y2); for( int yy = y1; yy <= y2; yy++ ) //移动;
{
for( int xx = x1; xx <= x2; xx++ )
{
std::vector <Point2f> &m = grid[yy*grid_width + xx]; if( m.size() )
{
for(j = ; j < m.size(); j++)
{
float dx = x - m[j].x;
float dy = y - m[j].y; if( dx*dx + dy*dy < minDistance ) //邻域内有角点的话, 就剔除;
{
good = false;
goto break_out;
}
}
}
}
} break_out: if (good)
{
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y)); corners.push_back(Point2f((float)x, (float)y));
++ncorners; if( maxCorners > && (int)ncorners == maxCorners )
break;
}
}
}
else
{
for( i = ; i < total; i++ )
{
int ofs = (int)((const uchar*)tmpCorners[i] - eig.ptr());
int y = (int)(ofs / eig.step);
int x = (int)((ofs - y*eig.step)/sizeof(float)); corners.push_back(Point2f((float)x, (float)y));
++ncorners;
if( maxCorners > && (int)ncorners == maxCorners )
break;
}
} Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
}
注: 该博文为扩展型;
opencv: 角点检测源码分析;的更多相关文章
- OpenCV SIFT原理与源码分析
http://blog.csdn.net/xiaowei_cqu/article/details/8069548 SIFT简介 Scale Invariant Feature Transform,尺度 ...
- 【OpenCV】SIFT原理与源码分析:DoG尺度空间构造
原文地址:http://blog.csdn.net/xiaowei_cqu/article/details/8067881 尺度空间理论 自然界中的物体随着观测尺度不同有不同的表现形态.例如我们形 ...
- 【OpenCV】SIFT原理与源码分析:方向赋值
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<关键点搜索与定位>,我们已经找到 ...
- 【OpenCV】SIFT原理与源码分析:关键点搜索与定位
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一步<DoG尺度空间构造>,我们得到了 ...
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...
- 【OpenCV】SIFT原理与源码分析:关键点描述
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<方向赋值>,为找到的关键点即SI ...
- HDFS源码分析数据块汇报之损坏数据块检测checkReplicaCorrupt()
无论是第一次,还是之后的每次数据块汇报,名字名字节点都会对汇报上来的数据块进行检测,看看其是否为损坏的数据块.那么,损坏数据块是如何被检测的呢?本文,我们将研究下损坏数据块检测的checkReplic ...
- SURF算法与源码分析、下
上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV中的源码分析,这篇文章接着上篇文章对已经定位到的SURF特征点进行特征描述.这一步至关重要,这是SU ...
- 阅读《RobHess的SIFT源码分析:综述》笔记
今天总算是机缘巧合的找到了照样一篇纲要性质的文章. 如是能早一些找到就好了.不过“在你认为为时已晚的时候,其实还为时未晚”倒是也能聊以自慰,不过不能经常这样迷惑自己,毕竟我需要开始跑了! 就照着这个大 ...
随机推荐
- GA-H61M-DS2 BIOS SETTING
Boot Option #1,UEFI:Sandisk SDSSDHP128G Boot Option #2, Boot Option #3, Boot Option #4, Bootup Numbe ...
- codeforces630C
Lucky Numbers CodeForces - 630C 小希称只含7和8的数是幸运数,那么不超过n位的幸运数有多少个? Input 一个整数 n (1 ≤ n ≤ 55) Output 输出幸 ...
- 洛谷 P3953 逛公园
题目链接 思路 首先没有0边,且k为0的情况就是最短路计数. 如果k不为0,看到k<=50,想到dp. 设f[u][i]表示到达u点比最短路多走i的路径数,转移到v点. f[u][i]+=f[v ...
- js 异步代码
这段时间一直在用node.js做毕设的后台,所以需要一些异步代码操作,主要的异步方式有:Promise.Generator 和 async / await,但下面主要讲 Promise 和 async ...
- C#开发轻松入门--笔记
第一章 1-1 .NET简介 (02:11) 1-2 Visual Studio简介及安装 (03:23) 1-3 创建C#控制台程序 (04:14) 1-4 练习题 1-5 程序界面各部分介绍 (0 ...
- ubuntu系统安装mysql(deb-bundle包)
由于某些原因,又要在ubuntu系统中安装mysql了,之前曾经安装过好多次.都没记下来 以前一直动用源码包来安装,基于两个原因:1.一直用Python写代码.2.想使用文件来安装,而不是通过api ...
- Idea中JavaWeb项目部署
1. 添加应用服务器tomcat 2. 将tomcat配置添加到项目中 artifacts配置:添加deploy, 添加artifacts,选择Web Application: Exploded &g ...
- BZOJ3275Number——二分图最大权独立集
题目描述 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c*c2:gcd(a,b)=1 输入 第一行一个正 ...
- BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆
题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13 根据 ...
- Get Luffy Out * HDU - 1816(2 - sat 妈的 智障)
题意: 英语限制了我的行动力....就是两个钥匙不能同时用,两个锁至少开一个 建个图 二分就好了...emm....dfs 开头low 写成sccno 然后生活失去希望... #include & ...