Object Detection and Tracking using Color Separation

Steps for Object Detection & Tracking | OpenCV with Visual Studio in Windows 10. In this Tutorial, we are going to Detect and Track a Yellow Ball using Object Detection (Color Separation) OpenCV.
  1. First of all, Follow this tutorial to Install & Configure OpenCV with Visual Studio 2015

  2. Copy the Full Source Code for Object Detection and Tracking from here:
  3. #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    
    #include <iostream>
    
    /*
    TRACK A YELLOW BALL - OBJECT DETECTION METHOD USING COLOR SEPERATION OPEN CV 3.1.0
    author - Rachit Gulati
    */
    
    int main() {
    
     cv::VideoCapture capWebcam(0);  // declare a VideoCapture object to associate webcam, 0 means use 1st (default) webcam
    
     if (capWebcam.isOpened() == false)  //  To check if object was associated to webcam successfully
     {    
      std::cout << "error: Webcam connect unsuccessful\n"; // if not then print error message
      return(0);            // and exit program
     }
    
     cv::Mat imgOriginal;  // Input image
     cv::Mat hsvImg;    // HSV Image
     cv::Mat threshImg;   // Thresh Image
    
     std::vector v3fCircles;  // 3 element vector of floats, this will be the pass by reference output of HoughCircles()
    
     char charCheckForEscKey = 0;
    
     int lowH = 21;       // Set Hue
     int highH = 30;
    
     int lowS = 200;       // Set Saturation
     int highS = 255;
    
     int lowV = 102;       // Set Value
     int highV = 225;
     // HUE for YELLOW is 21-30.
     // Adjust Saturation and Value depending on the lighting condition of the environment as well as the surface of the object.
    
     while (charCheckForEscKey != 27 && capWebcam.isOpened()) {    // until the Esc is pressed or webcam connection is lost
      bool blnFrameReadSuccessfully = capWebcam.read(imgOriginal);  // get next frame
    
      if (!blnFrameReadSuccessfully || imgOriginal.empty()) {    // if frame read unsuccessfully
       std::cout << "error: frame can't read \n";      // print error message
       break;               // jump out of loop
      }
    
      cv::cvtColor(imgOriginal, hsvImg, CV_BGR2HSV);      // Convert Original Image to HSV Thresh Image
    
      cv::inRange(hsvImg, cv::Scalar(lowH, lowS, lowV), cv::Scalar(highH, highS, highV), threshImg);
    
      cv::GaussianBlur(threshImg, threshImg, cv::Size(3, 3), 0);   //Blur Effect
      cv::dilate(threshImg, threshImg, 0);        // Dilate Filter Effect
      cv::erode(threshImg, threshImg, 0);         // Erode Filter Effect
    
      // fill circles vector with all circles in processed image
      cv::HoughCircles(threshImg,v3fCircles,CV_HOUGH_GRADIENT,2,threshImg.rows / 4,100,50,10,800);  // algorithm for detecting circles  
    
      for (int i = 0; i < v3fCircles.size(); i++) {      // for each circle
                   
       std::cout << "Ball position X = "<< v3fCircles[i][0]   // x position of center point of circle
        <<",\tY = "<< v3fCircles[i][1]        // y position of center point of circle
        <<",\tRadius = "<< v3fCircles[i][2]<< "\n";     // radius of circle
    
                        // draw small green circle at center of object detected
       cv::circle(imgOriginal,            // draw on original image
        cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),  // center point of circle
        3,                // radius of circle in pixels
        cv::Scalar(0, 255, 0),           // draw green
        CV_FILLED);              // thickness
    
                        // draw red circle around object detected 
       cv::circle(imgOriginal,            // draw on original image
        cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),  // center point of circle
        (int)v3fCircles[i][2],           // radius of circle in pixels
        cv::Scalar(0, 0, 255),           // draw red
        3);                // thickness
      } 
    
      // declare windows
      cv::namedWindow("imgOriginal", CV_WINDOW_AUTOSIZE);
      cv::namedWindow("threshImg", CV_WINDOW_AUTOSIZE); 
    
         /* Create trackbars in "threshImg" window to adjust according to object and environment.*/
      cv::createTrackbar("LowH", "threshImg", &lowH, 179); //Hue (0 - 179)
      cv::createTrackbar("HighH", "threshImg", &highH, 179);
    
      cv::createTrackbar("LowS", "threshImg", &lowS, 255); //Saturation (0 - 255)
      cv::createTrackbar("HighS", "threshImg", &highS, 255);
    
      cv::createTrackbar("LowV", "threshImg", &lowV, 255); //Value (0 - 255)
      cv::createTrackbar("HighV", "threshImg", &highV, 255);
      
    
      cv::imshow("imgOriginal", imgOriginal);     // show windows
      cv::imshow("threshImg", threshImg);
    
      charCheckForEscKey = cv::waitKey(1);     // delay and get key press
     }
     
     return(0);           
    }
    
    OR
    Get ObjectTracker.cpp from Github:
    Download ObjectTracker.cpp

  4. Paste full source code and Run it (Ctrl+F5 or F5).
OUTPUT

Yellow Ball Detection using Color Separation
Yellow Ball Detection and Tracker OpenCV
Yellow Ball Threshold Image
Yellow Ball Threshold Image
Coordinates and Radius of Yellow Ball
X-Coord, Y-Coord and Radius Of Yellow Ball


Explaination: You can use trackbars in "threshImg" window to adjust Hue, Saturation, Value according to object and environment(lightning), Hue values of basic colors:

  • Yellow 21- 30
  • Blue 75-130
  • Violet 130-160
  • Orange 0-21
  • Green 38-75
  • Red 160-179
You have to find the exact range of HUE values according to the color of the object. The SATURATION and VALUE is depend on the lighting condition of the environment as well as the surface of the object.


Like, Share and Comment Below

9 comments:

  1. Helpful. waiting for more opencv tutorials..

    ReplyDelete
  2. ||=== Build: Debug in cvtest (compiler: GNU GCC Compiler) ===|
    C:\Users\dev\Documents\cvtest\main.cpp||In function 'int main()':|
    C:\Users\dev\Documents\cvtest\main.cpp|26|error: missing template arguments before 'v3fCircles'|
    C:\Users\dev\Documents\cvtest\main.cpp|58|error: 'v3fCircles' was not declared in this scope|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
    Can u help Please!! got struck and thanks for sharing your codes :)

    ReplyDelete
  3. how to draw rectangle boundary on detected colour object?

    ReplyDelete
  4. i am using vs2017 and nuget packages for open cv libraries. there is absolutely no error in the code but when i run the code there is just a white screen appearing as a result. what could be done?

    ReplyDelete
  5. Buggy code
    line 26 std::vector v3fCircles;
    wrong usage of vector object

    ReplyDelete
    Replies
    1. in sach situation you use std::vector< string> v3fCircles;

      Delete