CAMSHIFT object tracking problem

asked 2013-03-18 12:58:20 -0500

hi, i have developed camshift algorithm by watching some tutorials..but unfortunately it didn't track the selected object....here is my code..i am a beginner to opencv and i need help..problem is the rectangle doesn't follow the object to track...

bool destroy=false;
 CvRect box;
 CvRect track_window;   
 bool drawing_box = false;
 cv::Mat imageROI;
 IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;   
 CvHistogram *hist = 0;   
 int hdims = 16;   
 float hranges_arr[] = {0,180};   
 float* hranges = hranges_arr;   
 int vmin = 10, vmax = 256, smin = 30;   
 IplImage *image2;
 CvScalar hsv2rgb( float hue );
 CvConnectedComp track_comp;   
 CvBox2D track_box;   

 void draw_box(Mat img, CvRect rect)
 {
   imageROI= img(cv::Rect(box.x,box.y,box.width,box.height));
cv::rectangle(img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height),
          cvScalar(0,0,255) ,2);

 }

  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);

}

     void my_mouse_callback( int event, int x, int y, int flags, void* param )
      {
       IplImage* frame = (IplImage*) param;

    switch( event )
       {
      case CV_EVENT_MOUSEMOVE: 
     {
      if( drawing_box )
      {
          box.width = x-box.x;
          box.height = y-box.y;
      }
  }
  break;

  case CV_EVENT_LBUTTONDOWN:
  {
      drawing_box = true;
      box = cvRect( x, y, 0, 0 );

  }
  break;

  case CV_EVENT_LBUTTONUP:
  {
      drawing_box = false;
      if( box.width < 0 )
      {
          box.x += box.width;
          box.width *= -1;
      }

      if( box.height < 0 )
      {
          box.y += box.height;
          box.height *= -1;
      }

      draw_box(frame, box);

  }
  break;

  case CV_EVENT_RBUTTONUP:
  {
      destroy=true;
  }
  break;

  default:
  break;

} }

    int _tmain(int argc, _TCHAR* argv[])
     {

VideoCapture cap(0);  
if(!cap.isOpened())   
    return -1;

  Mat image;
   Mat frame;
//cv::Mat image= cv::imread("1.jpg");
 cap>>image;
if (!image.data)
    return 0; 


// Display image
cv::namedWindow("Image");
cv::imshow("Image",image);

IplImage* img = new IplImage(image);
cvSmooth(img,img,CV_GAUSSIAN,3,0,0.0,0.0);
IplImage* temp = cvCloneImage(img);
cvSetMouseCallback("Image", my_mouse_callback, (void*) img);


while( 1 )
{
 if (destroy) 
{
  cvDestroyWindow("Image"); break;
}
cvCopyImage(img, temp);

if (drawing_box) 
    draw_box(temp, box);


cvShowImage("Image", temp);

if (cvWaitKey(15) == 27) 
    break;
}

cvReleaseImage(&temp);
cvDestroyWindow("Image");


    for(;;)
  {
      int i, bin_w, c;   

       cap >> frame;
        IplImage* frame_ipl = new IplImage(frame);
        hsv = cvCreateImage( cvGetSize(frame_ipl), 8, 3 ); 
        image2 = cvCreateImage( cvGetSize(frame_ipl), 8, 3 );
        hue = cvCreateImage( cvGetSize(frame_ipl), 8, 1 );   
        mask = cvCreateImage( cvGetSize(frame_ipl), 8, 1 );   
        backproject = cvCreateImage( cvGetSize(frame_ipl), 8, 1 );   
        hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );   
        histimg = cvCreateImage( cvSize(320,200), 8, 3 );   
        cvZero( histimg ); 

        cvCopy( frame_ipl, image2, 0 );   
        cvCvtColor( image2, hsv, CV_BGR2HSV );   

        int _vmin = vmin, _vmax = vmax;   

        cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0),   
                    cvScalar(180,256,MAX(_vmin,_vmax),0), mask );   
        cvSplit( hsv, hue, 0, 0, 0 );   

            float max_val = 0.f;   
            cvSetImageROI( hue, box );   
            cvSetImageROI( mask, box );   
            cvCalcHist( &hue, hist, 0, mask );   
            cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );   
            cvConvertScale( hist- ...
(more)
edit retag flag offensive close merge delete