1

Is it possible to calculate the distance of an object with known size? I would like to do this with an ball which has 7cm diamater. Now for the first calculation I would put him in 30cm distance to the webcam and in the second 50cm. Is there a linear function or formular to calculate somehow the distance? Lets say in the first measure it has a diamater of 6 pixel and in the second only 4. There must be a formular for this?

Best regards

potu1304
  • 75
  • 4
  • 19
  • 1
    Yes, it has been explained [before here.](https://stackoverflow.com/questions/14038002/opencv-how-to-calculate-distance-between-camera-and-object-using-image) – pastaleg Jun 02 '17 at 07:26
  • Do I need the focal lenght? Or are there other ways without the focal lenght? – potu1304 Jun 02 '17 at 07:31
  • You need the focal length in mm. You can read more here using this [calculator](https://www.scantips.com/lights/subjectdistance.html). Please take time to read all the theory – Ali Waqas Apr 19 '22 at 19:03

1 Answers1

1

In optical scheme you have two similar right triangles with edges F (objective focus distance), PixelSize, Distance and Size

    Distance / Size = F / PixelSize

So having parameters for some known Distance0, you can get F (in pixel units, consider it as some constant)

   F = Distance0 * PixelSize0 / Size0

and use it to calculate unknown distance (until zoom changes)

  Distance = F * Size / PixelSize

(Note that you can vary object size)

MBo
  • 77,366
  • 5
  • 53
  • 86
  • How can you accomodate for the fact that camera is tilted at an angle ? – Ali Waqas Apr 19 '22 at 15:26
  • @Ali Waqas Camera tilt shouldn't change formula. But object image is projected onto plane normal to camera axis, so observed size can diminish – MBo Apr 19 '22 at 15:38
  • Thanks for the reply. Can we somehow accomodate that angle into above formulas. Distance/cos(theta) = F * Size / PixelSize Do you think this would work. Thanks again man! – Ali Waqas Apr 19 '22 at 18:42
  • 1
    I think - yes, this should work, assuming that theta is angle between camera direction axis and object plane normal – MBo Apr 19 '22 at 18:45