Friday, March 4, 2011

opcv eye tracking

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
using namespace cv;

void onTrackbarSlide(int pos) {}
int main(int argc, char** argv) {
 int g_slider_position = 0,min_radius=0, max_radius=0, mindist=0, resolution=0;
 CvCapture* capture;
    capture = cvCreateCameraCapture(0);
  CvSize size = cvSize(
  (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
  (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
  );
    IplImage* img = cvCreateImage(size,IPL_DEPTH_8U,3);
 IplImage* grey = cvCreateImage(size,IPL_DEPTH_8U,1);
 IplImage* image = cvCreateImage(size,IPL_DEPTH_8U,1);
 IplImage* laplace16 = cvCreateImage(size, IPL_DEPTH_16S, 1);

 cvNamedWindow( "cvHoughCircles", CV_WINDOW_AUTOSIZE );
 cvNamedWindow( "laplace", CV_WINDOW_AUTOSIZE );
/* cvCreateTrackbar("min_radius","cvHoughCircles",&min_radius,1000,onTrackbarSlide);
 cvCreateTrackbar("max_radius","cvHoughCircles",&max_radius,1000,onTrackbarSlide);
 cvCreateTrackbar("resolution","cvHoughCircles",&resolution,20,onTrackbarSlide);
 cvCreateTrackbar("min_dist","cvHoughCircles",&mindist,1000,onTrackbarSlide);*/
 
 while(1)
 {
     IplImage* img = cvQueryFrame(capture);
  // invert the image
  int height,width,step,channels;
  uchar *data;
  int i,j,k;
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
    data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
//-----------------------------------------------
 cvCvtColor(img, grey, CV_RGB2GRAY);
 cvSmooth(grey, grey, CV_BLUR, 5, 5 );
 cvEqualizeHist(grey, grey);

 cvWaitKey(1);
 cvLaplace(grey, laplace16, 5);
 cvConvertScale(laplace16,image);
cvWaitKey(1);
 cvThreshold( image, image, 100, 100, CV_THRESH_TOZERO );
 cvSmooth(image, image, CV_BLUR, 5, 5 );
/* cvSmooth(image, image, CV_BLUR, 3, 3 );
 cvSmooth(image, image, CV_BLUR, 3, 3 );
 cvSmooth(image, image, CV_BLUR, 3, 3 );
 cvSmooth(image, image, CV_BLUR, 3, 3 );*/
 cvShowImage( "laplace", image);
 CvMemStorage* storage = cvCreateMemStorage(0);
cvWaitKey(1);
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
2,
width/7,100,50,
0,19
);
for( int i = 0; i < results->total; i++ ) {
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
cvCircle(
img,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0xff,0xff)
);
}
cvReleaseMemStorage(&storage);
cvShowImage( "cvHoughCircles", img);
 }
cvWaitKey(0);
 cvReleaseCapture( &capture );
cvReleaseImage(&img);
cvReleaseImage(&grey);
cvReleaseImage(&laplace16);
cvReleaseImage(&image);
}

No comments:

Post a Comment