관리 메뉴

코코야이야기

[c++] OpenCV 특징점검출 본문

프로그래밍/OpenCV

[c++] OpenCV 특징점검출

코코야 2015. 9. 24. 16:00
반응형

소스코드

//cvExtractSURF 함수로 특징점 및 묘사자 검출

#include <nonfree\nonfree.hpp>
#include "cv.h"
#include "highgui.h"
#include <stdio.h>

int main()
{
 IplImage *srcImage;
 if((srcImage = cvLoadImage("Lena.jpg",CV_LOAD_IMAGE_GRAYSCALE)) ==NULL)
  return -1;
 IplImage* dstImage = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,3);
 //IplImage* corners = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_32F,1);
 cv::initModule_nonfree();
 CvMemStorage* storage = cvCreateMemStorage(0);
 CvSeq *imageKeypoints = NULL, *imageDescriptors = NULL;
 CvSURFParams params = cvSURFParams(3000,0);
 cvExtractSURF(srcImage, NULL, &imageKeypoints, &imageDescriptors, storage, params);
 printf("Image imageKeypoints: %d\n", imageKeypoints->total);
 printf("Image imageKeypoints: %d\n", imageDescriptors->total);
 cvCvtColor(srcImage, dstImage, CV_GRAY2BGR);

 int x,y,r,i;
 int x1,y1,x2,y2;
 CvPoint2D32f pt[4];
 CvBox2D box;
 CvScalar color;

 for(i=0; i<imageKeypoints->total;i++)
 {
  CvSURFPoint* surf=(CvSURFPoint*)cvGetSeqElem(imageKeypoints, i);
  x = cvRound(surf->pt.x);
  y = cvRound(surf->pt.y);
  r = cvRound(surf->size*1.2/9.*2);

  //draw a box using surf->dir
  box.angle = surf->dir;
  box.center = cvPoint2D32f(x,y);
  box.size = cvSize2D32f(2*r,2*r);
  cvBoxPoints(box,pt);

  if(surf->laplacian < 0)
   color = CV_RGB(0,0,255);
  else
   color = CV_RGB(255,0,0);

  for(int j=0; j<4; j++)
  {
   x1 = cvRound(pt[j].x);
   y1 = cvRound(pt[j].y);
   x2 = cvRound(pt[(j+1)%4].x);
   y2 = cvRound(pt[(j+1)%4].y);

   CvPoint r1 = cvPoint(x1,y1);
   CvPoint r2 = cvPoint(x2,y2);
   cvLine(dstImage, r1, r2, color, 2);
  }

  //draw circle
  cvCircle(dstImage, cvPoint(x,y), r, color, 2);
 }

 int descriptor_size = (params.extended)? 128 : 64;

 for(i=0; i<1; i++) //print just one image descriptor
 {
  float* aD= (float*)cvGetSeqElem(imageDescriptors,i);
  printf("i = %d : \n", i);
  for (int j=0; j <descriptor_size; j++)
  {
   if(j%4==3)
    printf("%.4f\n",aD[j]);
   else
    printf("%.4f",aD[j]);
  }
  printf("\n");
 }

 cvNamedWindow("dstImage",CV_WINDOW_AUTOSIZE);
 cvShowImage("dstImage",dstImage);
 
 cvWaitKey(0);
 cvDestroyAllWindows();
 cvReleaseMemStorage(&storage);
 cvReleaseImage(&srcImage);
 cvReleaseImage(&dstImage);

 return 0;
}

 

 

 

출력영상

 

반응형

'프로그래밍 > OpenCV' 카테고리의 다른 글

[c++] OpenCV 카메라영상  (0) 2015.09.25
[c++] OpenCV 템플릿매칭1  (0) 2015.09.25
[c++] OpenCV 원검출  (0) 2015.09.24
[c++] OpenCV 직선검출  (0) 2015.09.23
[c++] OpenCV 영상엣지검출  (0) 2015.09.23
Comments