I am trying to write a program using opencv to calculate the distance from a webcam to a one inch white sphere. I feel like this should be pretty easy, but for whatever reason I’m drawing a blank. Thanks for the help ahead of time.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use triangle similarity to calibrate the camera angle and find the distance.
You know your ball’s size:
Dunits (e.g. cm). Place it at a known distanceZ, say 1 meter = 100cm, in front of the camera and measure its apparent width in pixels. Call this widthd.The focal length of the camera
f(which is slightly different from camera to camera) is thenf=d*Z/D.When you see this ball again with this camera, and its apparent width is
d'pixels, then by triangle similarity, you know thatf/d'=Z'/Dand thus:Z'=D*f/d'whereZ'is the ball’s current distance from the camera.