Tuesday, March 8, 2011

opcv goodfeaturetotrack 1

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
#include <conio.h>
using namespace cv;
const int MAX_CORNERS = 500;
int main()
{
 IplImage* img;
 IplImage* temp;
 IplImage* gray;
 const int MAX_COUNT = 500;
 int corner_count = MAX_CORNERS;
 int Gdim = 21;
 CvPoint2D32f* corners=new CvPoint2D32f[MAX_CORNERS];
 int W = 300;  //width of image
 int H = 200;  // height of image
 int Wr = 100;  //width of rectangle
 int Hr = 50;  // height of rectangle
 int xr = 20;  //x offset of rectangle
 int yr = 20;  //y offset of rectangle
 int i,j;
 uchar* ptr;
// create an RGB image that is blank
 img =  cvCreateImage(cvSize(W,H),IPL_DEPTH_8U,3);
 for(i=0;i<H;i++){
  ptr = (uchar*)(img->imageData+i*img->widthStep);
  for(j=0;j<W;j++){
   *(ptr++) = 255;*(ptr++) = 255;*(ptr++) = 255;}}
 // add in a red square
 for(i=0;i<Hr;i++){
  ptr = (uchar*)(img->imageData+(yr+i)*img->widthStep + 3*xr);
  for(j=0;j<Wr;j++){
   *(ptr++) = 0;*(ptr++) = 0;*(ptr++) = 255;}}
 // blurring with gaussian
 temp= cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
 cvSmooth(img,temp,CV_GAUSSIAN,Gdim,Gdim);
 cvCopy(temp,img,NULL);
 //create a gray scale image based in img
gray =  cvCreateImage(cvSize(W,H),IPL_DEPTH_8U,1);
cvCvtColor(img,gray,CV_RGB2GRAY);
// output the images
cvNamedWindow( "image", CV_WINDOW_AUTOSIZE );
cvShowImage( "image", img );
cvNamedWindow( "gray", CV_WINDOW_AUTOSIZE );
cvShowImage( "gray", gray );
// allocate additional image scratchpad for good features algorithm
IplImage* eig = cvCreateImage( cvGetSize(gray), 32, 1 );
temp = cvCreateImage( cvGetSize(gray), 32, 1 );
double quality = 0.01;
double min_distance = 10;
cvGoodFeaturesToTrack(gray, eig, temp, corners, &corner_count,quality, min_distance, 0, 3, 0, 0.04 );

// print out the good features and highlight the features in the image with green dots
int iimax = 100;
 if(corner_count < 100)
  iimax = corner_count;
  printf("corner count %d\n\n",iimax);
  for(int ii=0;ii<iimax;ii++){
   printf("%d  %d  %d\n",ii,cvRound(corners[ii].x),cvRound(corners[ii].y));
   cvSetImageROI(img,cvRect(cvRound(corners[ii].x),cvRound(corners[ii].y),3,3) );
   cvAddS(img,cvScalar(-255,255,-255),img);
   cvResetImageROI(img);
   cvShowImage( "image", img );
  }
 
cvWaitKey(0);
cvSaveImage( "C:\\encm503\\openCV_basics\\corner_det_square.jpg",img );
return(0);
}
--------------------------------------------------------------------------------

No comments:

Post a Comment