# Revision history [back]

### OpenCV speed tracker logic / formula

Hello,

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://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/ 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:
continue

# 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!

### OpenCV speed tracker logic / formula

Hello,

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://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/ 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:
continue

# 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!