Tracking Specific Color / Tracking Object

Tracking Specific Color

  1.         // Define the range of "blue" color in HSV colorspace
  2.         Scalar lowerLimit = Scalar(60,100,100);
  3.         Scalar upperLimit = Scalar(180,255,255);
  4.  
  5.         // Threshold the HSV image to get only blue color
  6.         inRange(hsvImage, lowerLimit, upperLimit, mask);
  7.  
  8.         // Compute bitwise-AND of input image and mask
  9.         bitwise_and(frame, frame, outputImage, mask=mask);
  10.  
  11.         // Run median filter on the output to smoothen it
  12.         medianBlur(outputImage, outputImage, 5);

Tracking Object

  1.         if(trackingFlag)
  2.         {
  3.             // Check for all the values in 'hsvimage' that are within the specified range
  4.             // and put the result in 'mask'
  5.             inRange(hsvImage, Scalar(0, minSaturation, minValue), Scalar(180, 256, maxValue), mask);
  6.  
  7.             // Mix the specified channels
  8.             int channels[] = {0, 0};
  9.             hueImage.create(hsvImage.size(), hsvImage.depth());
  10.             mixChannels(&hsvImage, 1, &hueImage, 1, channels, 1);
  11.  
  12.             if(trackingFlag < 0)
  13.             {
  14.                 // Create images based on selected regions of interest
  15.                 Mat roi(hueImage, selectedRect), maskroi(mask, selectedRect);
  16.  
  17.                 // Compute the histogram and normalize it
  18.                 calcHist(&roi, 1, 0, maskroi, hist, 1, &histSize, &histRanges);
  19.                 normalize(hist, hist, 0, 255, CV_MINMAX);
  20.  
  21.                 trackingRect = selectedRect;
  22.                 trackingFlag = 1;
  23.             }
  24.  
  25.             // Compute the histogram back projection
  26.             calcBackProject(&hueImage, 1, 0, hist, backproj, &histRanges);
  27.             backproj &= mask;
  28.             RotatedRect rotatedTrackingRect = CamShift(backproj, trackingRect, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1));
  29.  
  30.             // Check if the area of trackingRect is too small
  31.             if(trackingRect.area() <= 1)
  32.             {
  33.                 // Use an offset value to make sure the trackingRect has a minimum size
  34.                 int cols = backproj.cols, rows = backproj.rows;
  35.                 int offset = MIN(rows, cols) + 1;
  36.                 trackingRect = Rect(trackingRect.x - offset, trackingRect.y - offset, trackingRect.x + offset, trackingRect.y + offset) & Rect(0, 0, cols, rows);
  37.             }
  38.  
  39.             // Draw the ellipse on top of the image
  40.             ellipse(image, rotatedTrackingRect, Scalar(0,255,0), 3, CV_AA);
  41.         }

Leave a Reply

Your email address will not be published. Required fields are marked *