OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 02 May 2017 12:35:54 -0500OpenCV speed tracker logic / formulahttp://answers.opencv.org/question/145051/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! Sun, 30 Apr 2017 16:54:30 -0500http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/Comment by LBerger for <p>Hello, </p>
<p>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. </p>
<p>So far I've got my code to:</p>
<ul>
<li>convert to hsv </li>
<li>detect the contour </li>
<li>draw a circle around the ball & calculate the centre point </li>
<li>track its previous points</li>
</ul>
<p>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. </p>
<p>this is a snip of the code storing the coordinates and determining direction</p>
<pre><code># 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"
</code></pre>
<p>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. </p>
<p>What I can't figure out what to do is <strong>calculate the size of the contour found</strong>, I tried using: </p>
<pre><code> area = cv2.contourArea(cnt)
diameter = np.sqrt(4*area/np.pi)
</code></pre>
<p>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.</p>
<p>And then the next step, making use of the <strong>coordinates to calculate speed</strong>. 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?</p>
<p>Thanks in advance, its really appreciated! </p>
http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145174#post-id-145174you mean [length](http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#arclength)Mon, 01 May 2017 09:59:02 -0500http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145174#post-id-145174Comment by LBerger for <p>Hello, </p>
<p>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. </p>
<p>So far I've got my code to:</p>
<ul>
<li>convert to hsv </li>
<li>detect the contour </li>
<li>draw a circle around the ball & calculate the centre point </li>
<li>track its previous points</li>
</ul>
<p>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. </p>
<p>this is a snip of the code storing the coordinates and determining direction</p>
<pre><code># 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"
</code></pre>
<p>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. </p>
<p>What I can't figure out what to do is <strong>calculate the size of the contour found</strong>, I tried using: </p>
<pre><code> area = cv2.contourArea(cnt)
diameter = np.sqrt(4*area/np.pi)
</code></pre>
<p>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.</p>
<p>And then the next step, making use of the <strong>coordinates to calculate speed</strong>. 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?</p>
<p>Thanks in advance, its really appreciated! </p>
http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145133#post-id-145133Your question is not about opencv but physics. google is [your friend](http://link.springer.com/chapter/10.1007%2F978-3-319-24560-7_5)Mon, 01 May 2017 04:18:35 -0500http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145133#post-id-145133Comment by Sinjon for <p>Hello, </p>
<p>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. </p>
<p>So far I've got my code to:</p>
<ul>
<li>convert to hsv </li>
<li>detect the contour </li>
<li>draw a circle around the ball & calculate the centre point </li>
<li>track its previous points</li>
</ul>
<p>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. </p>
<p>this is a snip of the code storing the coordinates and determining direction</p>
<pre><code># 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"
</code></pre>
<p>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. </p>
<p>What I can't figure out what to do is <strong>calculate the size of the contour found</strong>, I tried using: </p>
<pre><code> area = cv2.contourArea(cnt)
diameter = np.sqrt(4*area/np.pi)
</code></pre>
<p>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.</p>
<p>And then the next step, making use of the <strong>coordinates to calculate speed</strong>. 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?</p>
<p>Thanks in advance, its really appreciated! </p>
http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145172#post-id-145172Okay, how can I calculate the size of a contour? @LBergerMon, 01 May 2017 09:44:33 -0500http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145172#post-id-145172Comment by Sinjon for <p>Hello, </p>
<p>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. </p>
<p>So far I've got my code to:</p>
<ul>
<li>convert to hsv </li>
<li>detect the contour </li>
<li>draw a circle around the ball & calculate the centre point </li>
<li>track its previous points</li>
</ul>
<p>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. </p>
<p>this is a snip of the code storing the coordinates and determining direction</p>
<pre><code># 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"
</code></pre>
<p>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. </p>
<p>What I can't figure out what to do is <strong>calculate the size of the contour found</strong>, I tried using: </p>
<pre><code> area = cv2.contourArea(cnt)
diameter = np.sqrt(4*area/np.pi)
</code></pre>
<p>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.</p>
<p>And then the next step, making use of the <strong>coordinates to calculate speed</strong>. 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?</p>
<p>Thanks in advance, its really appreciated! </p>
http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145374#post-id-145374I 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? @LBergerTue, 02 May 2017 12:35:54 -0500http://answers.opencv.org/question/145051/opencv-speed-tracker-logic-formula/?comment=145374#post-id-145374