Harris及Shi-Tomasi原理及源码解析

来源:互联网 发布:centos 安装选项 编辑:程序博客网 时间:2024/05/19 22:03

本文采用的是opencv2.4.3中的源码。

转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/48244255

CSDN-勿在浮沙筑高台

Harris角点检测

   人眼对角点的识别通常是通过一个局部的小窗口内完成的,如果在各个方向上移动这个小窗口,窗口内的灰度发生了较大的变化,那么说明窗口内存在角点。

  如果在各个方向移动,灰度几乎不变,说明是平坦区域;

  如果只沿着某一个方向移动,灰度几乎不变,说明是直线;

  如果沿各个方向移动,灰度均发生变化,说明是角点。

 

                                                      平坦区域                              直线                               角点              

图像I(x,y),在点(x,y)处平移(u,v)后的自相似性,可以用灰度变化函数E(u,v)表示

  

                  

泰勒展开:

代入得到:

                        

其中:

                         

二次项函数本质上就是一个椭圆函数,椭圆的扁平率和尺寸是由矩阵M的两个特征值决定的。

                                              

矩阵M的两个特征值与图像中的角点,边缘,平坦区域的关系:


Harris定义角点响应函数即,即R=Det(M)-k*trace(M)*trace(M)k为经验常数0.04~0.06 。

定义当R>threshold时且为局部极大值的点时,定义为角点。

Harris角点检测算子对图像亮度和对比度具有部分不变性,且具有旋转不变性,但不具有尺度不变性。

                           

opencv中调用cornerHarris函数检测角点:

blockSize:为邻域大小,对每个像素,考虑blockSize×blockSize大小的邻域S(p),在邻域上计算图像的差分的相关矩阵;


ksize: 为Soble算子核尺寸,如果小于0,采用3×3的Scharr滤波器;

k:为角点响应函数中的经验常数(0.04~0.06);

[cpp] view plain copy
 print?
  1. int blockSize = 2;  
  2. int apertureSize =3;  
  3. double k = 0.04;  
  4. /// Detecting corners  
  5. cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );   
[cpp] view plain copy
 print?
  1. void cv::cornerHarris( InputArray _src, OutputArray _dst, int blockSize, int ksize, double k, int borderType )  
  2. {  
  3.     Mat src = _src.getMat();  
  4.     _dst.create( src.size(), CV_32F );  
  5.     Mat dst = _dst.getMat();  
  6.     cornerEigenValsVecs( src, dst, blockSize, ksize, HARRIS, k, borderType );//调用函数计算图像块的特征值和特征向量  
  7. }  
[cpp] view plain copy
 print?
  1. static void  
  2. cornerEigenValsVecs( const Mat& src, Mat& eigenv, int block_size,  
  3.                      int aperture_size, int op_type, double k=0.,  
  4.                      int borderType=BORDER_DEFAULT )  
  5. {  
  6. #ifdef HAVE_TEGRA_OPTIMIZATION  
  7.     if (tegra::cornerEigenValsVecs(src, eigenv, block_size, aperture_size, op_type, k, borderType))  
  8.         return;  
  9. #endif  
  10.   
  11.   
  12.     int depth = src.depth();  
  13.     double scale = (double)(1 << ((aperture_size > 0 ? aperture_size : 3) - 1)) * block_size;  
  14.     if( aperture_size < 0 )  
  15.         scale *= 2.;  
  16.     if( depth == CV_8U )  
  17.         scale *= 255.;  
  18.     scale = 1./scale;  
  19.     CV_Assert( src.type() == CV_8UC1 || src.type() == CV_32FC1 );  
  20.   
  21.   
  22.     Mat Dx, Dy;   //保存每个像素点的水平方向和垂直方向的一阶差分  
  23.     if( aperture_size > 0 )//采用Sobel滤波器  
  24.     {  
  25.         Sobel( src, Dx, CV_32F, 1, 0, aperture_size, scale, 0, borderType );  
  26.         Sobel( src, Dy, CV_32F, 0, 1, aperture_size, scale, 0, borderType );  
  27.     }  
  28.     else    //采用3×3的Scharr滤波器,可以给出比3×3 Sobel滤波器更精确的结果  
  29.     {  
  30.         Scharr( src, Dx, CV_32F, 1, 0, scale, 0, borderType );  
  31.         Scharr( src, Dy, CV_32F, 0, 1, scale, 0, borderType );  
  32.     }  
  33.   
  34.   
  35.     Size size = src.size();  
  36.     Mat cov( size, CV_32FC3 );  
  37.     int i, j;  
  38.   
  39.   
  40.     for( i = 0; i < size.height; i++ )  
  41.     {  
  42.         float* cov_data = (float*)(cov.data + i*cov.step);  
  43.         const float* dxdata = (const float*)(Dx.data + i*Dx.step);  
  44.         const float* dydata = (const float*)(Dy.data + i*Dy.step);  
  45.   
  46.   
  47.         for( j = 0; j < size.width; j++ )  
  48.         {  
  49.             float dx = dxdata[j];  
  50.             float dy = dydata[j];  
  51.   
  52.   
  53.             cov_data[j*3] = dx*dx;  //第一个通道存dx*dx,即M矩阵左上角的元素  
  54.             cov_data[j*3+1] = dx*dy;//第二个通道存dx*dy,即M矩阵左下角和右上角的元素  
  55.             cov_data[j*3+2] = dy*dy;//第三个通道存dy*dy,即M矩阵右下角的元素  
  56.         }  
  57.     }  
  58.   
  59.   
  60.     boxFilter(cov, cov, cov.depth(), Size(block_size, block_size), //计算邻域上的差分相关矩阵(block_size×block_size)  
  61.         Point(-1,-1), false, borderType );  
  62.   
  63.   
  64.     if( op_type == MINEIGENVAL )   //计算M矩阵的最小的特征值  
  65.         calcMinEigenVal( cov, eigenv );  
  66.     else if( op_type == HARRIS )//计算Harris角点响应函数R  
  67.         calcHarris( cov, eigenv, k );  
  68.     else if( op_type == EIGENVALSVECS )//计算图像块的特征值和特征向量  
  69.         calcEigenValsVecs( cov, eigenv );  
  70. }  
[cpp] view plain copy
 print?
  1. static void  
  2. calcHarris( const Mat& _cov, Mat& _dst, double k )  
  3. {  
  4.     int i, j;  
  5.     Size size = _cov.size();  
  6.     if( _cov.isContinuous() && _dst.isContinuous() )  
  7.     {  
  8.         size.width *= size.height;  
  9.         size.height = 1;  
  10.     }  
  11.   
  12.     for( i = 0; i < size.height; i++ )  
  13.     {  
  14.         const float* cov = (const float*)(_cov.data + _cov.step*i);  
  15.         float* dst = (float*)(_dst.data + _dst.step*i);  
  16.         j = 0;  
  17.         for( ; j < size.width; j++ )  
  18.         {  
  19.             float a = cov[j*3];  
  20.             float b = cov[j*3+1];  
  21.             float c = cov[j*3+2];  
  22.             dst[j] = (float)(a*c - b*b - k*(a + c)*(a + c));  //计算每个像素对应角点响应函数R  
  23.         }  
  24.     }  
  25. }  

Shi-Tomasi角点检测

由于Harris算法的稳定性和k值有关,Shi-Tomasi发现,角点的稳定性和矩阵M的较小特征值有关,改进的Harris算法即直接计算出矩阵M的特征值,用较小的特征值与阈值比较,大于阈值的即为强特征点。
                       
opencv中对其实现算法在goodFeaturesToTrack()函数中:
[cpp] view plain copy
 print?
  1. CV_EXPORTS_W void goodFeaturesToTrack( InputArray image, OutputArray corners,  
  2.                                      int maxCorners, double qualityLevel, double minDistance,  
  3.                                      InputArray mask=noArray(), int blockSize=3,  
  4.                                      bool useHarrisDetector=falsedouble k=0.04 );  
image:输入图像
corners:输出图像数组
maxCorners:需要的角点数目
qualityLevel:最大,最小特征值的乘法因子。定义可接受图像角点的最小质量因子。
minDistance:容忍距离。角点之间的最小距离,采用欧氏距离。
mask:掩码
blockSize:邻域大小
useHarrisDetector:采用Harris角点检测
k:采用Harris角点检测时的经验常数k(0.04~0.06)
算法原理:调用cornerMinEigenVal()函数求出每个像素点自适应矩阵M的较小特征值,保存在矩阵eig中,然后找到矩阵eig中最大的像素值记为maxVal,然后阈值处理,小于qualityLevel*maxVal的特征值排除掉,最后函数确保所有发现的角点之间具有足够的距离。
[cpp] view plain copy
 print?
  1. void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,  
  2.                               int maxCorners, double qualityLevel, double minDistance,  
  3.                               InputArray _mask, int blockSize,  
  4.                               bool useHarrisDetector, double harrisK )  
  5. {  
  6.     Mat image = _image.getMat(), mask = _mask.getMat();  
  7.   
  8.     CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );  
  9.     CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size()) );  
  10.   
  11.     Mat eig, tmp;  
  12.     if( useHarrisDetector )       
  13.         cornerHarris( image, eig, blockSize, 3, harrisK );  //采用Harris角点检测  
  14.     else  
  15.         cornerMinEigenVal( image, eig, blockSize, 3 );  //采用Harris改进算法,eig保存矩阵M较小的特征值。见下面算法实现  
  16.   
  17.     double maxVal = 0;  
  18.     minMaxLoc( eig, 0, &maxVal, 0, 0, mask );//保存eig中最大的值maxVal  
  19.     threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );//阈值处理,小于maxVal*qualityLevel的像素值归为0。  
  20.     dilate( eig, tmp, Mat());//膨胀,3×3的核,为了取局部极大值  
  21.   
  22.     Size imgsize = image.size();  
  23.   
  24.     vector<const float*> tmpCorners;  
  25.   
  26.     // collect list of pointers to features - put them into temporary image  
  27.     forint y = 1; y < imgsize.height - 1; y++ )  
  28.     {  
  29.         const float* eig_data = (const float*)eig.ptr(y);  
  30.         const float* tmp_data = (const float*)tmp.ptr(y);  
  31.         const uchar* mask_data = mask.data ? mask.ptr(y) : 0;  
  32.   
  33.         forint x = 1; x < imgsize.width - 1; x++ )  
  34.         {  
  35.             float val = eig_data[x];  
  36.             if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )//局部极大值  
  37.                 tmpCorners.push_back(eig_data + x);  
  38.         }  
  39.     }  
  40.   
  41.     sort( tmpCorners, greaterThanPtr<float>() );  //按值从大到小排序  
  42.     vector<Point2f> corners;  
  43.     size_t i, j, total = tmpCorners.size(), ncorners = 0;  
  44.  /*   
  45.   网格处理,即把图像划分成正方形网格,每个网格边长为容忍距离minDistance 
  46.   以一个角点位置为中心,minDistance为半径的区域内部不允许出现第二个角点 
  47.  */  
  48.     if(minDistance >= 1)  
  49.     {  
  50.          // Partition the image into larger grids  
  51.         int w = image.cols;  
  52.         int h = image.rows;  
  53.           
  54.         const int cell_size = cvRound(minDistance);//划分成网格,网格边长为容忍距离  
  55.         const int grid_width = (w + cell_size - 1) / cell_size;  
  56.         const int grid_height = (h + cell_size - 1) / cell_size;  
  57.   
  58.         std::vector<std::vector<Point2f> > grid(grid_width*grid_height);  
  59.   
  60.         minDistance *= minDistance;  
  61.   
  62.         for( i = 0; i < total; i++ )  //按从大到小的顺序,遍历所有角点  
  63.         {  
  64.             int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);  
  65.             int y = (int)(ofs / eig.step);  
  66.             int x = (int)((ofs - y*eig.step)/sizeof(float));  
  67.   
  68.             bool good = true;  
  69.   
  70.             int x_cell = x / cell_size;  
  71.             int y_cell = y / cell_size;  
  72.   
  73.             int x1 = x_cell - 1;  
  74.             int y1 = y_cell - 1;  
  75.             int x2 = x_cell + 1;  
  76.             int y2 = y_cell + 1;  
  77.   
  78.             // boundary check  
  79.             x1 = std::max(0, x1);  
  80.             y1 = std::max(0, y1);  
  81.             x2 = std::min(grid_width-1, x2);  
  82.             y2 = std::min(grid_height-1, y2);  
  83.   
  84.             forint yy = y1; yy <= y2; yy++ )//检测角点,minDistance半径邻域内,有没有其他角点出现  
  85.             {  
  86.                 forint xx = x1; xx <= x2; xx++ )  
  87.                 {  
  88.                     vector <Point2f> &m = grid[yy*grid_width + xx];  
  89.   
  90.                     if( m.size() )  
  91.                     {  
  92.                         for(j = 0; j < m.size(); j++)  
  93.                         {  
  94.                             float dx = x - m[j].x;  
  95.                             float dy = y - m[j].y;  
  96.                             if( dx*dx + dy*dy < minDistance )//有其他角点,丢弃当前角点  
  97.                             {  
  98.                                 good = false;  
  99.                                 goto break_out;  
  100.                             }  
  101.                         }  
  102.                     }  
  103.                 }  
  104.             }  
  105.   
  106.             break_out:  
  107.   
  108.             if(good)  
  109.             {  
  110.                 // printf("%d: %d %d -> %d %d, %d, %d -- %d %d %d %d, %d %d, c=%d\n",  
  111.                 //    i,x, y, x_cell, y_cell, (int)minDistance, cell_size,x1,y1,x2,y2, grid_width,grid_height,c);  
  112.                 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));  
  113.   
  114.                 corners.push_back(Point2f((float)x, (float)y));//满足条件的存入corners  
  115.                 ++ncorners;  
  116.   
  117.                 if( maxCorners > 0 && (int)ncorners == maxCorners )  
  118.                     break;  
  119.             }  
  120.         }  
  121.     }  
  122.     else   //不设置容忍距离  
  123.     {  
  124.         for( i = 0; i < total; i++ )  
  125.         {  
  126.             int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);  
  127.             int y = (int)(ofs / eig.step);  
  128.             int x = (int)((ofs - y*eig.step)/sizeof(float));  
  129.   
  130.             corners.push_back(Point2f((float)x, (float)y));  
  131.             ++ncorners;  
  132.             if( maxCorners > 0 && (int)ncorners == maxCorners )  
  133.                 break;  
  134.         }  
  135.     }  
  136.   
  137.     Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);  
  138.   
  139. }  

求矩阵M最小的特征值


[cpp] view plain copy
 print?
  1. static void  
  2. calcMinEigenVal( const Mat& _cov, Mat& _dst )  
  3. {  
  4.     int i, j;  
  5.     Size size = _cov.size();  
  6.     if( _cov.isContinuous() && _dst.isContinuous() )  
  7.     {  
  8.         size.width *= size.height;  
  9.         size.height = 1;  
  10.     }  
  11.   
  12.     for( i = 0; i < size.height; i++ )//遍历所有像素点  
  13.     {  
  14.         const float* cov = (const float*)(_cov.data + _cov.step*i);  
  15.         float* dst = (float*)(_dst.data + _dst.step*i);  
  16.         j = 0;  
  17.         for( ; j < size.width; j++ )  
  18.         {  
  19.             float a = cov[j*3]*0.5f;//cov[j*3]保存矩阵M左上角元素  
  20.             float b = cov[j*3+1];   //cov[j*3+1]保存左下角和右上角元素  
  21.             float c = cov[j*3+2]*0.5f;//cov[j*3+2]右下角元素  
  22.             dst[j] = (float)((a + c) - std::sqrt((a - c)*(a - c) + b*b));//求最小特征值,一元二次方程求根公式  
  23.         }  
  24.     }  
  25. }  

参考:http://blog.csdn.net/xw20084898/article/details/21180729

         http://wenku.baidu.com/view/f61bc369561252d380eb6ef0.html

         http://blog.csdn.net/crzy_sparrow/article/details/7391511     

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 格力空调出现u8怎么办 双肺多发斑点影怎么办 外文翻译没5000字怎么办 睡出永久睡痕怎么办 英语不好学学英文软件怎么办 遥控汽车只能原地打转怎么办 铝合金门上的胶带纸撕不掉怎么办 纸胶带撕不下来怎么办 拼多多卖不出去怎么办 联想键盘被锁了怎么办 台式电脑打不开机怎么办 文件名中不能用特殊符号怎么办 高铁喷雾扣留后怎么办 高铁没收的东西怎么办 安检被收的东西怎么办 我的律师骗我怎么办 没婆婆生了小孩怎么办 没人帮你带孩子怎么办 亲戚在家里不走怎么办 穷人家的孩子该怎么办 空腹吃李子胃疼怎么办 情侣空间农场谷仓空间不够怎么办 王者荣耀情侣解除对方不同意怎么办 oppo手机进了水怎么办 淘宝卖号被骗了怎么办 淘宝买号被骗了怎么办 后脑偏头疼怎么办最快最有效 脸两边的骨头大怎么办 做b超胎儿老盘腿怎么办 裤子白边染色了怎么办 异地恋要分手了怎么办 异地恋没话题了怎么办 陪婆婆聊天心情超级郁闷怎么办? 他不想理你了怎么办 陌陌看到信息不回怎么办 qq的文档看不了怎么办 怀孕了分手了怎么办啊 qq节日祝福关了怎么办 微信欠款不还怎么办 qq文件记录删除了怎么办 qq漫游记录删了怎么办