OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 12 Sep 2019 01:50:48 -0500Calculate the distance between two objects (example cars) in a videohttp://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/Hello,
I am using Haar cascade function to detect the cars from a video file and want to find the distance between two cars? Is it possible to do that using OpenCV or something similar to find the gap between two detected objects?
ThanksMon, 17 Sep 2018 00:42:29 -0500http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/Answer by monxarat for <p>Hello,
I am using Haar cascade function to detect the cars from a video file and want to find the distance between two cars? Is it possible to do that using OpenCV or something similar to find the gap between two detected objects?
Thanks</p>
http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?answer=199646#post-id-199646You must [calibration camera](https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d)(find extrinsic and intrinsic camera matrix, [Use can using cheeboard or Calibrated Cameras in OpenGL width gluPerspective without glFrustum method](http://ksimek.github.io/2013/06/18/calibrated-cameras-and-gluperspective/)).
After, set of 2D points provided onto 3D space.
![](/upfiles/15372617277830708.jpg)
We do know many points on the 3D model ( i.e. (U, V, W) ), but we do not know (X, Y, Z). We only know the location of the 2D points ( i.e. (x, y) ). In the absence of radial distortion, the coordinates (x, y) of point p in the image coordinates is given by
![image description](/upfiles/1537261948633882.png)
where, fx and fy are the focal lengths in the x and y directions, and (cx, cy) is the optical center (assume fx = fy = focal length). Things get slightly more complicated when radial distortion is involved and for the purpose of simplicity I am leaving it out.
![image description](/upfiles/15372623057626921.png)
See more [https://en.wikipedia.org/wiki/Angle_of_view](https://en.wikipedia.org/wiki/Angle_of_view)
Using [cv::solvePnP](https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga549c2075fac14829ff4a58bc931c033d)
the function solvePnP and solvePnPRansac can be used to estimate pose.
Calculate distance and bearing between two point
![image description](/upfiles/15372626843062692.png)
Or you can using double [hypot](http://devdocs.io/cpp/numeric/math/hypot) method measure distance.Tue, 18 Sep 2018 04:31:53 -0500http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?answer=199646#post-id-199646Comment by Ayeshayounis for <p>You must <a href="https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d">calibration camera</a>(find extrinsic and intrinsic camera matrix, <a href="http://ksimek.github.io/2013/06/18/calibrated-cameras-and-gluperspective/">Use can using cheeboard or Calibrated Cameras in OpenGL width gluPerspective without glFrustum method</a>).</p>
<p>After, set of 2D points provided onto 3D space.</p>
<p><img alt="" src="/upfiles/15372617277830708.jpg"></p>
<p>We do know many points on the 3D model ( i.e. (U, V, W) ), but we do not know (X, Y, Z). We only know the location of the 2D points ( i.e. (x, y) ). In the absence of radial distortion, the coordinates (x, y) of point p in the image coordinates is given by</p>
<p><img alt="image description" src="/upfiles/1537261948633882.png"></p>
<p>where, fx and fy are the focal lengths in the x and y directions, and (cx, cy) is the optical center (assume fx = fy = focal length). Things get slightly more complicated when radial distortion is involved and for the purpose of simplicity I am leaving it out.</p>
<p><img alt="image description" src="/upfiles/15372623057626921.png"></p>
<p>See more <a href="https://en.wikipedia.org/wiki/Angle_of_view">https://en.wikipedia.org/wiki/Angle_of_view</a></p>
<p>Using <a href="https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga549c2075fac14829ff4a58bc931c033d">cv::solvePnP</a>
the function solvePnP and solvePnPRansac can be used to estimate pose.</p>
<p>Calculate distance and bearing between two point</p>
<p><img alt="image description" src="/upfiles/15372626843062692.png"></p>
<p>Or you can using double <a href="http://devdocs.io/cpp/numeric/math/hypot">hypot</a> method measure distance.</p>
http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?comment=218248#post-id-218248simran i am doing work on same thing can you help me please ,i done one source code which can detect object but i want also distance from camera to detecting things how much far.did you finished this program i am very new i think you understand #monxarat answer .
help me please i dont know how i can use calibration method
thanksThu, 12 Sep 2019 01:50:48 -0500http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?comment=218248#post-id-218248Comment by Simran for <p>You must <a href="https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d">calibration camera</a>(find extrinsic and intrinsic camera matrix, <a href="http://ksimek.github.io/2013/06/18/calibrated-cameras-and-gluperspective/">Use can using cheeboard or Calibrated Cameras in OpenGL width gluPerspective without glFrustum method</a>).</p>
<p>After, set of 2D points provided onto 3D space.</p>
<p><img alt="" src="/upfiles/15372617277830708.jpg"></p>
<p>We do know many points on the 3D model ( i.e. (U, V, W) ), but we do not know (X, Y, Z). We only know the location of the 2D points ( i.e. (x, y) ). In the absence of radial distortion, the coordinates (x, y) of point p in the image coordinates is given by</p>
<p><img alt="image description" src="/upfiles/1537261948633882.png"></p>
<p>where, fx and fy are the focal lengths in the x and y directions, and (cx, cy) is the optical center (assume fx = fy = focal length). Things get slightly more complicated when radial distortion is involved and for the purpose of simplicity I am leaving it out.</p>
<p><img alt="image description" src="/upfiles/15372623057626921.png"></p>
<p>See more <a href="https://en.wikipedia.org/wiki/Angle_of_view">https://en.wikipedia.org/wiki/Angle_of_view</a></p>
<p>Using <a href="https://docs.opencv.org/3.4.3/d9/d0c/group__calib3d.html#ga549c2075fac14829ff4a58bc931c033d">cv::solvePnP</a>
the function solvePnP and solvePnPRansac can be used to estimate pose.</p>
<p>Calculate distance and bearing between two point</p>
<p><img alt="image description" src="/upfiles/15372626843062692.png"></p>
<p>Or you can using double <a href="http://devdocs.io/cpp/numeric/math/hypot">hypot</a> method measure distance.</p>
http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?comment=200838#post-id-200838Thank you so much!Tue, 09 Oct 2018 16:25:16 -0500http://answers.opencv.org/question/199541/calculate-the-distance-between-two-objects-example-cars-in-a-video/?comment=200838#post-id-200838