Friday, February 1, 2013

How to add Slider or Trackbar to a cvWindow

    You might have seen my previous post regarding the HSV values extraction. That code used Sliders to vary the value of the parameter passed to the thresholding algorithm. In this post, we are going to learn ahow to add a Slider or Trackbar to a cvWindow.

the function below will create and add a trackbar to the window "Output"
  cvCreateTrackbar("LowHue", "Output", &lowHue, max, onLowHue);

you see here five arguments to the function.

"LowHue" - it is a string, used as caption on the cvWindow above the Trackbar. something like a name for the trackbar.

"Output" - name of the window, which must be created using cvNamedWindow("Output", CV_WINDOW_NORMAL ) before attaching a trackbar to it.

&lowHue - it is a variable, corresponding to the slider position.
 int lowHue = 10;

max - maximum value for the slider. the value corresponding to slider postion when it is at the rightmost end.
 int max = 255;

onLowHue - it is a function pointer, which will be called when, there is a change in slider position due to user interaction.

void onLowHue (int value )

     lowHue = value;

Read more ►

HSV trainer

    This code snippet extracts the lower and higher bound HSV values for the object of interest.

Code Listing


int max = 255;

int lowHue = 10 ;
int lowSat = 0 ;
int lowVal = 0 ;

int highHue = 100 ;
int highSat = 255 ;
int highVal = 255 ;

void onLowHue ( int value ) { lowHue = value ; }
void onLowSat ( int value ) { lowSat = value ; }
void onLowVal ( int value ) { lowVal = value ; }

void onHighHue ( int value ) { highHue = value ; }
void onHighSat ( int value ) {  highSat = value ; }
void onHighVal ( int value ) { highVal = value ; }

IplImage* hsv_from_rgb(IplImage* _src);
IplImage* hsv_threshold ( IplImage* _hsv_src,
     CvScalar _min,
     CvScalar _max );

void cvCreateShowWindow(char* name, IplImage *src)
 cvShowImage(name, src);

IplImage* resize_640x480(IplImage* _src)
    IplImage* res = cvCreateImage( cvSize(640,480) ,
    cvResize ( _src, res, CV_INTER_LINEAR );
 return res;

int main ( int  argc, char* argv[] )
 if ( argc < 2 ) {
     printf ("Error: no input image found\n" ) ;
     exit (1) ;

   IplImage* src = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
   cvCreateShowWindow("src", src);
   IplImage* cir = cvCloneImage (src) ;
   //IplImage* _src = cvCreateImage ( cvGetSize(src), IPL_DEPTH_8U, 1 ) ;
   //cvCvtColor (src, _src, CV_BGR2GRAY) ;
   IplImage* _src = resize_640x480 (src);
   //cvSmooth ( _src, _src, CV_GAUSSIAN, 15, 0, 0, 0);

   cvCreateShowWindow(" input", _src);

  cvCreateShowWindow("Output",_src  );

  cvCreateTrackbar("LowHue", "Output", &lowHue, max, onLowHue);
  cvCreateTrackbar("HighHue", "Output", &highHue, max, onHighHue);

  cvCreateTrackbar("LowSat", "Output", &lowSat, max, onLowSat);
  cvCreateTrackbar("HighSat", "Output", &highSat, max, onHighSat);

  cvCreateTrackbar("LowVal", "Output", &lowVal, max, onLowVal);
  cvCreateTrackbar("HighVal", "Output", &highVal, max, onHighVal);

  CvMemStorage* storage = cvCreateMemStorage(0);
  IplImage* hsved = hsv_from_rgb ( _src );
  IplImage* res ;
 while ( 1 )
   res = hsv_threshold( hsved, 
                        cvScalar( lowHue, lowSat, lowVal, 0),
                        cvScalar( highHue, highSat, highVal, 255) );
   cvShowImage("Output", res );
 } // End of while loop
  cvCreateShowWindow("res", res);

IplImage* hsv_from_rgb(IplImage* _src)
  IplImage* img = cvCloneImage ( _src ) ;
  cvCvtColor ( _src, img, CV_BGR2HSV );
  return img;

IplImage* hsv_threshold ( IplImage* _hsv_src,
     CvScalar _min,
     CvScalar _max )
    IplImage* thresholded = cvCreateImage(cvGetSize( _hsv_src ), 8, 1 );
    cvInRangeS( _hsv_src, _min, _max, thresholded );  
    return thresholded;  

    Run the program with any input image. Adjust the slider so that the object of interest is show white. Note the values. Thats it. You are done.
Read more ►

Second Review - Report

   Well, the second review went very well.We have showed the program that tracks the ball. Before the review, we tested the code with the images of same ball at different environments. WHITE is a major villain in HSV space. So we tried with different colored environments. Actually only three backgrounds. The Hue values almost remained the same. So it was easy to extract the position of the ball.

   We used this snippet, for extracting the HSV values for training(what we call it) the code for object detection. Just before the review started, we shot another video of the ball in the lab itself and trained the code to suit the environment. We just finished extracting concurrent values for HSV params, the reviewers entered the lab.

   Suriyadeepan first presented the control of bot using Android through Blue-tooth(BtBee) module. Then the object tracking code is demostrated. Thats it. Thank you for reading this boring story.

Second Review report can be downloaded here
Read more ►

Copyright © Project Athena Design by O Pregador | Blogger Theme by Blogger Template de luxo | Powered by Blogger