Friday, March 4, 2011

opcv hsv mask

#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, lowthresh=0, highthresh=0;
 int Hlow=0, Slow=0, Vlow=0, Hhigh=0, Shigh=0, Vhigh=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* filtered_frame = cvCreateImage(size,IPL_DEPTH_8U,1);
 IplImage* edge = 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( "color_frame", CV_WINDOW_AUTOSIZE );
 cvNamedWindow( "filtered_frame", CV_WINDOW_AUTOSIZE );
 cvNamedWindow( "edge", 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("lowthresh","edge",&lowthresh,1000,onTrackbarSlide);
 cvCreateTrackbar("highthresh","edge",&highthresh,1000,onTrackbarSlide);
 cvCreateTrackbar("Hlow","filtered_frame",&Hlow,200,onTrackbarSlide);
 cvCreateTrackbar("Slow","filtered_frame",&Slow,200,onTrackbarSlide);
 cvCreateTrackbar("Vlow","filtered_frame",&Vlow,200,onTrackbarSlide);
 cvCreateTrackbar("Hhigh","filtered_frame",&Hhigh,200,onTrackbarSlide);
 cvCreateTrackbar("Shigh","filtered_frame",&Shigh,200,onTrackbarSlide);
 cvCreateTrackbar("Vhigh","filtered_frame",&Vhigh,200,onTrackbarSlide);
 
 IplImage *  hsv_frame    = cvCreateImage(size, IPL_DEPTH_8U, 3);
    IplImage*  thresholded   = cvCreateImage(size, IPL_DEPTH_8U, 1);
 while(1)
 {
    CvScalar hsv_min = cvScalar(0.01*Hlow*256, 0.01*Slow*256, 0.01*Vlow*256, 0);
    CvScalar hsv_max = cvScalar(0.01*Hhigh*256, 0.01*Shigh*256, 0.01*Vhigh*256, 0);
     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, hsv_frame, CV_BGR2HSV);
 cvSmooth(hsv_frame, hsv_frame, CV_GAUSSIAN, 5, 5 );
  cvShowImage( "color_frame", hsv_frame);
  cvInRangeS(hsv_frame, hsv_min, hsv_max, filtered_frame);
  cvShowImage( "filtered_frame", filtered_frame);
 cvCanny(filtered_frame, edge, lowthresh, highthresh);
 cvShowImage( "edge", edge);
cvWaitKey(1);
 cvLaplace(edge, laplace16, 7);
 cvConvertScale(laplace16,image);
 cvShowImage( "laplace", image);
 CvMemStorage* storage = cvCreateMemStorage(0);
cvWaitKey(1);
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
1,
image->width/10,100,50,
min_radius,max_radius
);
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(&hsv_frame);
cvReleaseImage(&filtered_frame);
cvReleaseImage(&edge);
cvReleaseImage(&laplace16);
cvReleaseImage(&image);
}

No comments:

Post a Comment