Multiple Colored ball tracking using OpenCV python and threading

Multiple Colored Ball tracking using opencv in python.

Tracking balls of different colors and tracing it’s path as well using opencv in python and threading

Here is what i did.

Multiple Color tracking

Color tracking

The workflow goes as:

1. Get an image from the camera

2. Find out where the different colored balls

3. Trace the path by placing the coordinates of ball in a array and drawing the line

Logical part of code :

#!/usr/bin/python
#different library imports
import sys
from threading import Thread
from opencv.cv import *
from opencv.highgui import *
storage=cvCreateMemStorage(0)
capture = cvCreateCameraCapture( 0 )

if __name__ == '__main__':
     cvNamedWindow( "result", CV_WINDOW_AUTOSIZE )
cvCvtColor(img, new_img, CV_BGR2HSV ) #Converting the image from BGR2HSV

cvInRangeS(new_img,self.h_min,self.h_max,thresh) #creating a threshold image that contains pixels in between h_min and h_max

cvSmooth(thresh,thresh,CV_GAUSSIAN,9,9) #Smooth the thresh image to remove noise

circles=cvHoughCircles(thresh,storage,CV_HOUGH_GRADIENT,2,thresh.height/4,200,100,25,0) #Detecting circles out of the thresholded images

maxRadius=0 #used to hold the value of the largest circle found in the given frame

##Merging images img is current frame and path is traced path of the ball
cvAdd(img,self.path,img)
#displaying the image on the window
cvShowImage(self.color,thresh)

Complete code is available here at: Github

If you have any queries or doubt. Please reply here

5 thoughts on “Multiple Colored ball tracking using OpenCV python and threading

  1. Help Me,. i got error like this
    “from opencv.cv import *
    ImportError: No module named opencv.cv”

    how to solving..?? i using raspberrypi, opencv 2.43, and python 2.7 (numpy,scipy)

  2. URGENT! Is there anyway to get this strickly in C++ for open CV this is exactly what I need but I cannot figure out python. Thanks!

    • Hi everyone,
      I’m pretty much new to Opencv and C++. I’m now have the same problem that Kyle has/had, I tried to come up with a code which traces the center of the ball in C++ (like the one shown in this post) but my code seems to have some sort of exception handling error and I’m not able to figure out why.
      What i intended to do with my code was to detect the circular object/ball, mark its center and trace its center when I move it in front of the webcam on a blank window(which has black color background).
      I really struggling with this, any help with the code or suggestions will be much appreciated. Please help me out.
      My code:
      ////add your libraries here///
      IplImage* imgTracking;
      int lastX = -1;
      int lastY = -1;
      void trackObject(IplImage* gray){
      // Calculate the moments of ‘imgThresh’
      CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
      cvMoments(gray, moments, 1);
      double moment10 = cvGetSpatialMoment(moments, 1, 0);
      double moment01 = cvGetSpatialMoment(moments, 0, 1);
      double area = cvGetCentralMoment(moments, 0, 0);

      // if the area1500){
      // calculate the position of the ball
      int posX = moment10/area;
      int posY = moment01/area;

      if(lastX>=0 && lastY>=0 && posX>=0 && posY>=0)
      {
      // Draw a White line from the previous point to the current point
      cvLine(imgTracking, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(255,255,255), 4);//255
      }

      lastX = posX;
      lastY = posY;
      }

      free(moments);
      }

      int main(int argc, char** argv)

      {
      CvCapture* capture =0;
      capture = cvCaptureFromCAM(0);
      if(!capture){
      printf(“Capture failure\n”);
      return -1;
      }
      IplImage* frame=0;
      frame = cvQueryFrame(capture);
      if(!frame) return -1;
      while(true){
      frame = cvQueryFrame(capture);
      if(!frame) break;
      frame=cvCloneImage(frame);
      IplImage* im_rgb = frame;
      IplImage* gray = cvCreateImage(cvGetSize(im_rgb), 8, 1);
      CvMemStorage* storage = cvCreateMemStorage(0);

      //rgb to grey
      cvCvtColor(im_rgb, gray, CV_BGR2GRAY);
      cvSmooth(gray, gray, CV_GAUSSIAN, 9, 9);

      //track the possition of the ball
      trackObject(gray);
      //blank image
      IplImage* image = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);//900,650
      cvZero( image );

      //add trace
      cvAdd(image, imgTracking,gray);

      CvSeq* circles = cvHoughCircles(gray, storage,
      CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100);
      int i;

      for (i = 0; i total; i++)
      {
      float* p = (float*)cvGetSeqElem( circles, i );

      cvCircle( im_rgb, cvPoint(cvRound(p[0]),cvRound(p[1])),
      3, CV_RGB(0,255,0), -1, 8, 0 );
      cvCircle( im_rgb, cvPoint(cvRound(p[0]),cvRound(p[1])),
      cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );

      }

      cvShowImage(“Binary”, gray);
      cvShowImage(“Video”, frame);
      cvNamedWindow( “circles”, 1 );
      cvShowImage( “circles”, im_rgb );
      cvWaitKey(1);
      }
      return 0;
      }

Leave a comment