Sunday, April 17, 2011

opcv camshift

//This is a modify code from opencv2.1 sample code, camshiftdemo.cpp
//This program tracks a orange ping-pong ball
//LOG
//April 2, 2011:  work on cvHoughCircle implemetation
//Note houghcircle doesn't work for real time applications
#ifdef _CH_
#pragma package <opencv>
#endif
#define CV_NO_BACKWARD_COMPATIBILITY
#ifndef _EiC
#include <cv.h>   
#include <highgui.h>   
#include <cxcore.h>   
#include <stdio.h>   
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#endif
IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0, *dst=0;
CvHistogram *hist = 0;
//initialize for cvHoughCircles
CvScalar hsv_min;
CvScalar hsv_max;
CvMemStorage* storage;
CvSeq* circles;
int backproject_mode = 0;
int select_object = 0;
int track_object = 0;
int show_hist = 1;
CvPoint origin;
CvRect selection;
CvRect leftside_selection; //added april 8, 2011, if out of view activate this tracking window
CvRect rightside_selection;//april 8, 2011
CvRect topside_selection;  //april 8, 2011
CvRect bottomside_selection; //april 8, 2011
CvRect loutofviewTrackWin; //April 8, 2011 leftside out of view track window
CvRect routofviewTrackWin; //April 8, 2011
CvRect toutofviewTrackWin; //April 8, 2011
CvRect boutofviewTrackWin; //April 8, 2011
CvRect track_window;
CvBox2D track_box;
CvConnectedComp track_comp;
int hdims = 16;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;

//intialized here
//int vmin = 10, vmax = 256, smin = 30;
int vmin = 129, vmax = 256, smin = 86; //NOTE: select window in the ping-pong ball
void on_mouse( int event, int x, int y, int flags, void* param )
{
    if( !image )
        return;
    if( image->origin )
        y = image->height - y;
// do until CV_EVENT_LBUTTONUP if deactivated
    if( select_object )
    {
        selection.x = MIN(x,origin.x); // select x-positon of left side 
        selection.y = MIN(y,origin.y); // select y-positon of bottom line
        selection.width = selection.x + CV_IABS(x - origin.x); // width = x-position of left side + difference between original x and x   
        selection.height = selection.y + CV_IABS(y - origin.y); // height = y-position of bottom line + difference between original y and y 
        //selection.x = MAX( selection.x, 0 );
        //selection.y = MAX( selection.y, 0 );
        //selection.width = MIN( selection.width, image->width );
        //selection.height = MIN( selection.height, image->height );
  // if mouse cursor is at out-of-screen
  //credit: http://read.pudn.com/downloads113/sourcecode/graph/text_recognize/474257/5/camshift.cpp__.htm
        if( (selection.x < 0) || (selection.y < 0)){  
            selection.x = MAX( selection.x, 0 );  
            selection.y = MAX( selection.y, 0 );  
        }  
        // if width of height of selection box is larger than image's then
  //credit: http://read.pudn.com/downloads113/sourcecode/graph/text_recognize/474257/5/camshift.cpp__.htm
        if( (selection.width > image->width) || (selection.height > image->height) ){  
            selection.width = MIN( selection.width, image->width );  
            selection.height = MIN( selection.height, image->height );  
        }     

// measurement of width and height of selection box in above is not actually width and height.   
// because x and y position in above are started at (0,0), we just add it to difference between original x,y and x,y in above   
// we need following calculation 
 printf("selection.x = %d, selection.y= %d\n", selection.x, selection.y);
        selection.width -= selection.x;
        selection.height -= selection.y;
     printf("selection.width = %d, selection.height= %d\n", selection.width, selection.height);

 }
    switch( event )
    {
    case CV_EVENT_LBUTTONDOWN:
        origin = cvPoint(x,y); // save a position of mouse cursor during left button of mouse is down
        selection = cvRect(x,y,0,0);
        select_object = 1;
        break;
    case CV_EVENT_LBUTTONUP:
        select_object = 0;
        if( selection.width > 0 && selection.height > 0 )
            track_object = -1; // selection box is selected
        break;
    }
}

CvScalar hsv2rgb( float hue )
{
    int rgb[3], p, sector;
    static const int sector_data[][3]=
        {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
    hue *= 0.033333333333333333333333333333333f;
    sector = cvFloor(hue);
    p = cvRound(255*(hue - sector));
    p ^= sector & 1 ? 255 : 0;
    rgb[sector_data[sector][0]] = 255;
    rgb[sector_data[sector][1]] = 0;
    rgb[sector_data[sector][2]] = p;
    return cvScalar(rgb[2], rgb[1], rgb[0],0);
}
int main( int argc, char** argv )
{
    CvCapture* capture = 0;
 CvFont font; 
    CvPoint boxcenter;
 CvPoint boxcenter2;
 int radius;
    char* aText[36];// character array for output

 CvPoint2D32f  dcenter;// struct to local center of tracked object
 CvPoint2D32f  center;// struct to local center of of box
 CvSize2D32f  boxDim;// struct store the box height and width

 //select webcam or avi video file
    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 )
        capture = cvCaptureFromAVI( argv[1] );
    if( !capture )//if no webcam or avi video print error message
    {
        fprintf(stderr,"Could not initialize capturing...\n");
        return -1;
    }
    printf( "Hot keys: \n"
        "\tESC - quit the program\n"
        "\tc - stop the tracking\n"
        "\tb - switch to/from backprojection view\n"
        "\th - show/hide object histogram\n"
        "To initialize tracking, select the object with mouse\n" );

 //add window here
    cvNamedWindow( "Histogram", 1 );
    cvNamedWindow( "CamShiftDemo", 1 );
 //cvNamedWindow( "hsv", 1 );//added April 7, 2011
 //cvNamedWindow( "mask", 1 );//
 //cvNamedWindow( "hue", 1 );

    cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 );
    //printf("track_object = %d \n", track_object);
 //create trackbar here
    cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
    cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
    cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );

    for(;;)// very long for loop
    {
        IplImage* frame = 0;
        int i, bin_w, c;
 
        frame = cvQueryFrame( capture );
        if( !frame )
            break;
 
        if( !image )//basically intialized here
        {
            /* allocate all the buffers */
            image = cvCreateImage( cvGetSize(frame), 8, 3 );
            image->origin = frame->origin;
            hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
            hue = cvCreateImage( cvGetSize(frame), 8, 1 );
            mask = cvCreateImage( cvGetSize(frame), 8, 1 );
            backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
            hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
            histimg = cvCreateImage( cvSize(320,200), 8, 3 );
            cvZero( histimg );
   dst = cvCreateImage( cvGetSize(frame), 8, 1 );//added April 6, 2011 for cvCanny
   leftside_selection = cvRect(0,0,image->width/32, image->height);//April 8, 2011, specify tracking window
   rightside_selection = cvRect(620,0,image->width/32, image->height);//April 8, 2011
   topside_selection = cvRect(0,0,image->width, image->height/24);//April 8, 2011
   bottomside_selection = cvRect(0,460,image->width, image->height/24);//April 8, 2011
//checking the output of leftside_selection
//printf("leftside_selection.x = %d, leftside_selection.y = %d\n", leftside_selection.x, leftside_selection.y);
//printf("leftside_selection.width = %d, leftside_selection.height = %d\n", leftside_selection.width, leftside_selection.height);
 
  }//end if( !image )
 
        cvCopy( frame, image, 0 );
        cvCvtColor( image, hsv, CV_BGR2HSV );//conversion bgr color space to hsv
  //printf("track_object_1 = %d \n", track_object);
        if( track_object )
        {
            int _vmin = vmin, _vmax = vmax;
   //printf("_vmin= %d \n", vmin);
   //Checks that array elements lie between two scalars.
            cvInRangeS(
    hsv,
    cvScalar(0,smin,MIN(_vmin,_vmax),0),
    cvScalar(180,256,MAX(_vmin,_vmax),0),
    mask );
   //Divides multi-channel array into several single-channel arrays
   //or extracts a single channel from the array.
            cvSplit( hsv, hue, 0, 0, 0 );
  
   //printf("track_object_2 = %d \n", track_object);
            if( track_object < 0 )
            {
                float max_val = 0.f; //max_val = 0.000000
    float test=0.f;
    float a =0.f;
                cvSetImageROI( hue, selection );
                cvSetImageROI( mask, selection );
                cvCalcHist( &hue, hist, 0, mask );//Calculates the histogram of image(s).
                cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );//Finds the minimum and maximum histogram bins.
               
    //Converts one array to another with optional linear transformation.
    cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
                //a = 255./max_val; // uncomment to see result, added April 7,2011
    //test=max_val ? 255. / max_val : 0.;
    //printf("max_val = %f , a = %f\n",max_val, a );
   
    //Resets the image ROI to include the entire image and releases the ROI structure.
    cvResetImageROI( hue );
                cvResetImageROI( mask );
    track_window = selection;
    loutofviewTrackWin = leftside_selection;//april 8, 2011
    routofviewTrackWin = rightside_selection;//april 8, 2011
    toutofviewTrackWin = topside_selection;//april 8, 2011
    boutofviewTrackWin = bottomside_selection;//april 8, 2011
                track_object = 1;//By setting to 1 a loop is form
                cvZero( histimg );
                bin_w = histimg->width / hdims;
    //printf("bin_w = %d, histimg->width = %d,hdims=%d \n",bin_w,histimg->width, hdims );
    //bin_w = 20, histimg->width = 320,hdims=16
                for( i = 0; i < hdims; i++ )
                {
     //cvGetReal1D: Return a specific element of single-channel array.
                    int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 );//very fast float->int conversion
     CvScalar color = hsv2rgb(i*180.f/hdims);
                    //draw histograph
     cvRectangle(
      histimg,
      cvPoint(i*bin_w,histimg->height),
      cvPoint((i+1)*bin_w,histimg->height - val),
                        color, -1, 8, 0 );
                //printf("val(%d) = %d \n", i,val);
    //val(0) = 0
    //val(1) = 200 (orange)
    //val(2) = 0
    //val(3) = 0
    //val(4) = 0
    //val(5) = 0
    //val(6) = 0
    //val(7) = 0
    //val(8) = 0
    //val(9) = 0
    //val(10) = 0
    //val(11) = 0
    //val(12) = 0
    //val(13) = 0
    //val(14) = 0
    //val(15) = 0
    }// end for( i = 0; i < hdims; i++ )
            }// if( track_object < 0 )
            cvCalcBackProject( &hue, backproject, hist );//Calculates the back projection.
            cvAnd( backproject, mask, backproject, 0 );//Calculates per-element bit-wise conjunction of two arrays.
   //erode backproject, added april 7/11
   //cvErode(backproject,backproject,0,3);
   //smooth backproject, added april 7/11
   //cvSmooth(backproject,backproject,CV_GAUSSIAN,13,0,0,0);
            cvCamShift(
    backproject,
    track_window,
                cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1),
                &track_comp, &track_box );
   //track_window = track_comp.rect;
   //credit: http://read.pudn.com/downloads113/sourcecode/graph/text_recognize/474257/5/camshift.cpp__.htm
   center =track_box.center;
  
   //if object is out of view on the left side select ioutofviewTrackwin
   if( center.x >=0 && center.x <=20)//leftside
     track_window = loutofviewTrackWin;
   else if( center.x >=620 && center.x <=640)//rightside
     track_window = routofviewTrackWin;
   else if( center.y >=0 && center.y <=20)//topside
     track_window = toutofviewTrackWin;
   else if( center.y >=460 && center.y <=480)//bottomside
     track_window = boutofviewTrackWin;
   else
     track_window = track_comp.rect;
   //end if
   boxDim =track_box.size;
   boxcenter2=cvPointFrom32f(center);
   //draw a circle around tracked ping-pong ball
   radius = pow(pow((int)boxDim.height/3,2) +pow((int)boxDim.width/3,2), 0.5);
   cvCircle( image, boxcenter2,radius , CV_RGB(255,0,255), 4, 8,0);


            if( backproject_mode )
                cvCvtColor( backproject, image, CV_GRAY2BGR );
            if( !image->origin )
                track_box.angle = -track_box.angle;
            //cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 );
  
   //added April 2, 2011
   //cvCanny(backproject, dst, 10,100, 3);
  
   //cvNamedWindow( "cvCanny",CV_WINDOW_AUTOSIZE );
   //cvShowImage( "cvCanny"  , dst);
  
    
  }//end if( track_object )
  dcenter =track_box.center;
  boxcenter=cvPointFrom32f(dcenter);
  cvCircle( image, boxcenter, 3, CV_RGB(0,255,0), -1, 8,0);             
        //cvCircle( image, cvPoint(30,50), 3, CV_RGB(255,0,0), 30, 8,1);   
        cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, .65, .65, 0, 2, CV_AA);  
              
       //shows the location    
        sprintf(aText,"x=%d, y=%d, frame size =%d x %d",boxcenter.x,boxcenter.y,cvGetSize(frame) );
        cvPutText( image, aText, cvPoint(20,20), &font, CV_RGB(255,0,0));  
       //fprintf(output,"%d, %d\n", boxcenter.x, boxcenter.y);

        if( select_object && selection.width > 0 && selection.height > 0 )
        {
            cvSetImageROI( image, selection );
            cvXorS( image, cvScalarAll(255), image, 0 );
            cvResetImageROI( image );
        }
  //diplay window
        cvShowImage( "CamShiftDemo", image );
        cvShowImage( "Histogram", histimg );
  //cvShowImage( "hsv", hsv);//added April 7, 2011
  //cvShowImage( "mask", mask );
  //cvShowImage( "hue", hue);
        c = cvWaitKey(10);
        if( (char) c == 27 )
            break;
        switch( (char) c )
        {
        case 'b':
            backproject_mode ^= 1;
            break;
        case 'c':
            track_object = 0;
            cvZero( histimg );
            break;
        case 'h':
            show_hist ^= 1;
            if( !show_hist )
                cvDestroyWindow( "Histogram" );
            else
                cvNamedWindow( "Histogram", 1 );
            break;
        default:
            ;
        }//end  if( track_object )
    } //end for(;;)
    cvReleaseCapture( &capture );
    cvDestroyWindow("CamShiftDemo");
    return 0;
}
#ifdef _EiC
main(1,"camshiftdemo.c");
#endif
//For testing outputs
//checking the output of leftside_selection
//printf("loutofviewTrackWin.x = %d, loutofviewTrackWin.y = %d\n", loutofviewTrackWin.x, loutofviewTrackWin.y);
//printf("loutofviewTrackWin.width = %d, loutofviewTrackWin.height = %d\n", loutofviewTrackWin.width, loutofviewTrackWin.height);
 

No comments:

Post a Comment