2014年6月26日 星期四

使用OpenCV的cvGoodFeaturesToTrack及cvFindCornerSubPix做角點偵測 ( Corner Detection using cvGoodFeaturesToTrack and cvFindCornerSubPix in OpenCV )

介紹:
  • 如何使用cvGoodFeaturesToTrack、cvFindCornerSubPix
Introduction:
  • How to do corner detection using cvGoodFeaturesToTrack , cvFindCornerSubPix in OpenCV library. 

開發環境:
  • Visual Studio 2010
  • Windows 7
  • OpenCV 2.4.5

#include "stdafx.h"
#include "cvaux.h"
#include "highgui.h"

int main()
{
 IplImage *pSrcImg;
 IplImage *pGrayImg;
 IplImage *pEigImg;
 IplImage *pTempImg;
 IplImage *pShowResultImg;
 IplImage *pShowSubResultImg;
 CvCapture *capture = cvCaptureFromCAM(0);

 const int MAX_CORNERS  = 200 ;//特徵數量限制 
 CvPoint2D32f* corners  = new CvPoint2D32f[ MAX_CORNERS ];
 int corner_count  = MAX_CORNERS;

 //parameter of cvGoodFeaturesToTrack
 double quality_level  = 0.1;
 double min_distance  = 5;

 //parameter of cvFindCornerSubPix
 int half_win_size  = 5;
 int iteration   = 20;
 double epislon   = 0.1;

 while(1)
 {
  cvWaitKey(30);
  pSrcImg = cvQueryFrame(capture);

  pShowResultImg   = cvCloneImage(pSrcImg);
  pShowSubResultImg  = cvCloneImage(pSrcImg);
  pGrayImg  = cvCreateImage( cvGetSize(pSrcImg), 
          pSrcImg->depth, 
          1);
  pEigImg  = cvCreateImage( cvGetSize(pSrcImg), 
          IPL_DEPTH_32F, 
          1);
  pTempImg  = cvCreateImage( cvGetSize(pSrcImg), 
          IPL_DEPTH_32F, 
          1);

  cvCvtColor( pSrcImg, pGrayImg, CV_BGR2GRAY);
  cvGoodFeaturesToTrack( pGrayImg, 
        pEigImg, 
        pTempImg,
        corners, 
        &corner_count, 
        quality_level, 
        min_distance );

  for(int i = 0 ; i < corner_count ; ++i){
   cvLine( pShowResultImg, cvPoint(corners[i].x, corners[i].y),
   cvPoint(corners[i].x, corners[i].y), CV_RGB(255,0,0), 5);
  }


  cvFindCornerSubPix( pGrayImg, 
       corners, 
       corner_count, 
       cvSize(half_win_size, half_win_size),
       cvSize(-1,-1), 
       cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,
       iteration, 
       epislon));

  for(int i = 0 ; i < corner_count ; ++i){
   cvLine( pShowSubResultImg, cvPoint(corners[i].x, corners[i].y),
   cvPoint(corners[i].x, corners[i].y), CV_RGB(255,255,0), 5);
  }
  
  cvReleaseImage( &pGrayImg );
  cvReleaseImage( &pEigImg );
  cvReleaseImage( &pTempImg );  
     
  cvShowImage("Result", pShowResultImg);
  cvShowImage("Sub Result", pShowSubResultImg);    
 }
 cvReleaseImage( &pShowResultImg );
 cvReleaseImage( &pShowSubResultImg );   
}

紅色點表示cvGoodFeaturesToTrack所偵測的,黃色表示cvFindCornerSubPix校正後的。




相關文章:OpenCV學習日記

0 意見:

張貼留言