2
$\begingroup$

I need an equation for an object of a static height and width of 20x20 pixels. I get the object's width/height from camera, according to how close it's. I can tell if it's too close or too far. But I can't measure the rough distance from the camera.

On a normal scenario, if I am one meter away, the width/height of the object is 20 pixels. If I am 12 cm away, it's 80 pixels wide. I need an equation to dynamically measure the distance from the camera.

Thank you

$\endgroup$
  • $\begingroup$ Such equation could have much value for you:) $\endgroup$ – Moti Nov 11 '18 at 0:33
1
$\begingroup$

First, see this answer of mine to understand how 3D projection on a 2D surface is calculated.

Simply put, if we use a coordinate system where the focal point of the camera is at origin, $+z$ axis is where the camera is pointed at, $+x$ is right and $+y$ is up, and the projection plane is at distance $d$ from the focal point, then a detail at $z$ units away from the focal point, $w$ units in size, will be $w^\prime$ units on the projection plane:

$$\bbox{w^\prime = w \frac{d}{z}} \quad \iff \quad \bbox{ z = d \frac{w}{w^\prime} }$$

In your case, you measure the distance $L$ from an unknown distance $r$ from the projection plane, so the equation is really $$\bbox{L + r = d \frac{w}{w^\prime}} \quad \iff \quad \bbox{L = d \frac{w}{w^\prime} - r}$$ We also must assume that the camera optics do not change. Many zoom lenses and autofocus mechanisms actually change the field of view and focal length, but since we do not know how (it really depends on the camera), we cannot do anything about that.

We know you measured $w^\prime = 20\text{ px}$ for $L = 100\text{ cm}$, and $w^\prime = 80\text{ px}$ for $L = 12\text{ cm}$. These give us equations $$\begin{cases} 100\text{ cm} = d \frac{w}{20\text{ px}} - r \\ 12\text{ cm} = d \frac{w}{80\text{ px}} - r \\ \end{cases}$$ They are enough to solve for $d\cdot w \approx 2347\text{ px cm}$ and $r \approx 17.33\text{ cm}$.

So, with our assumptions for this particular camera, and for this particular object, we can calculate the distance from the size in pixels $w^\prime$: $$\bbox[#ffffef, 1em]{ L\text{ [cm]} = \frac{2347\text{ [px}\cdot\text{cm]}}{w^\prime\text{ [px]}} - 17.33\text{ [cm]}}$$

$\endgroup$
  • $\begingroup$ Isn't this supposed to be a simple problem from Newton's optics? Looks like a lot of reading on a completely different and advanced subject, just to solve a problem from a primary school physics. $\endgroup$ – Oldboy Nov 11 '18 at 7:34
  • $\begingroup$ @Oldboy: The question states that OP is dealing with computer analysis of camera sensor imagery, not doing primary school physics. In my view, you are the one picking an odd context. If you use the exact values of the constants ($7040/3\text{ [px}\cdot\text{cm]}$ and $52/3\text{ [cm]}$), the results are equivalent; the difference being I show it from first principles (geometry) in the relevant context (computer graphics), and list the factors that can lead the model to give results that do not match observations. Feel free to downvote. $\endgroup$ – Nominal Animal Nov 11 '18 at 9:40
  • $\begingroup$ No, I will not downvote your answer, it's perfectly valid. Actually I have upvoted it, no hard feelings please :) $\endgroup$ – Oldboy Nov 11 '18 at 12:17
0
$\begingroup$

The lens in the camera has some focal length $f$. The object is in front of the lens and the image is created on the camera sensor. If the distance between the object and the camera is p and the distance between the lens and the sensor is q:

$$\frac 1p+\frac1q=\frac 1f\tag{1}$$

That's famous Newton's formula. Height of the object H and height of the image h are proportional:

$$\frac Hp=\frac hq\tag{2}$$

From (2):

$$ q=\frac {ph}{H}$$

Replace that into (1) and you get:

$$\frac1p\left(1+{H \over h}\right)=\frac 1f $$

From it, you can find the size of the image $h$ as a function of distance (the formula you are looking for):

$$h=\frac{Hf}{p-f}\tag{3}$$

The only problem is that you don't know the focal distance $f$ and the height of the object $H$. But you know that for $p_1=1$m, $h_1=20$px and for $p_2=0.12$m, $h_2=80$px.

Replace that into (3) and you have the following two equations:

$$h_1=\frac{Hf}{p_1-f}$$

$$h_2=\frac{Hf}{p_2-f}$$

This is a simple system of 2 equations with 2 unknowns ($H,f$) and the solutions are:

$$f=\frac{p_1h_1-p_2h_2}{h_1-h_2}\tag{4}$$

$$H=h_1h_2\frac{p_2-p_1}{h_1p_1-h_2p_2}\tag{5}$$

Calculate $f,H$ from (4) and (5), replace into (3) and you are done.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.