OpenCV speed tracker logic / formula

asked 2017-04-30 16:54:30 -0500

Sinjon gravatar image

updated 2017-05-01 03:58:17 -0500


My aims are to create a tracker that tracks a coloured ball and outputs its speed. I'm new at programming and can't get my head around the logic or functions needed to calculate the speed.

So far I've got my code to:

  • convert to hsv
  • detect the contour
  • draw a circle around the ball & calculate the centre point
  • track its previous points

I want to implement (space in http, as i cant publish links) h ttp:// for the speed detection. in that code it uses the previous coordinates to determine the direction it's travelling in. I want to use the data from the previous points to calc speed.

this is a snip of the code storing the coordinates and determining direction

# loop over the set of tracked points
    for i in np.arange(1, len(pts)):
        # if either of the tracked points are None, ignore
        # them
        if pts[i - 1] is None or pts[i] is None:

            # check to see if enough points have been accumulated in
            # the buffer
            if counter >= 10 and i == 1 and pts[-10] is not None:
                # compute the difference between the x and y
                # coordinates and re-initialize the direction
                # text variables
                dX = pts[-10][0] - pts[i][0]
                dY = pts[-10][1] - pts[i][1]
                (dirX, dirY) = ("", "")

                # ensure there is significant movement in the
                # x-direction
                if np.abs(dX) > 20:
                    dirX = "East" if np.sign(dX) == 1 else "West"

I've calibrated my camera and know the px/mm of my pi. So say my ball is 10cm, in the video capture its size is 100px. 10px is 1cm at its current distance from the camera.

What I can't figure out what to do is calculate the size of the contour found, I tried using:

   area = cv2.contourArea(cnt) 
   diameter = np.sqrt(4*area/np.pi)

but from that got: 'is not a numpy array neither a scalar error' , I tried creating an array for cnt and also putting area in where cnt is declared, no luck though.

And then the next step, making use of the coordinates to calculate speed. the ball will going up and down but it won't be in a perfectly straight line. If I know the frames per second and use this as my time, say its 20fps, count to 40, 2 seconds then perform the speed formula. Now how would I determine the pixels the tracker has travelled in the 40 frames?

Thanks in advance, its really appreciated!

edit retag flag offensive close merge delete


Your question is not about opencv but physics. google is your friend

LBerger gravatar imageLBerger ( 2017-05-01 04:18:35 -0500 )edit

Okay, how can I calculate the size of a contour? @LBerger

Sinjon gravatar imageSinjon ( 2017-05-01 09:44:33 -0500 )edit

you mean length

LBerger gravatar imageLBerger ( 2017-05-01 09:59:02 -0500 )edit

I was able to get arclength working after some trial and error and could calculate the diameter! now the problem is that the diameter is constantly changing as the video detects the contours. I want to input the first 5 values into a np.array and calculate the mean to use as my object size. But I'm having difficulty setting the array up to only contain 5 values.

av_diameter = np.array(diameter) 
av_diameter = np.mean(av_diameter)

I've tried putting (5), a data type and also using .shape but having no luck. Also, once the array is filled. will these values be maintained in there or be replaced with new values entering array? @LBerger

Sinjon gravatar imageSinjon ( 2017-05-02 12:35:54 -0500 )edit