Calculating relative focal length based on a cylinder

3dcomputer visiongeometrytrigonometry

I am trying to camera match a photo of a cylinder.

The photo is taken relatively straight, and I can get a rotation matrix for the camera based on the elipsis created by projecting the top circle.

enter image description here

The problem shows up when I try to figure out what focal length the camera has.

My thinking is that given the principal point in the center of the image, and that the x and y axis(green and red) are equal in length in 3d space, I should be able to increase the focal length until they align in length with what is on the screen.

Does this make sense, and is there a mathematical solution to this that I am currently not seeing?

working on the answer by Hosam:
I created an image using threejs editor.
Placed a cylinder at origo with height 1 and radius 0.5.

Placed the camera arbitrarily at $[0,0.64,1.96]$
Rotated it down(in x) by $-15.4deg$
And lastly set the field of view to $75deg$ (bottom to top)

I then pulled the image into my tool and mapped out all points.
printscr

from this I got:
$$
a = 0.555 \\
b = 0.039 \\
c_0 = 0.506 \\
$$

I threw this into wolfram alpha as it is a bit quicker than writing the code for it

2 results came out that are quite similar to each other at roughly $0.12$.

so from that focal length I can get a field of view.
$$
2 * atan(1 / 0.12)
$$
.
Which problematically does not equal $1.308$ (75deg is radians)

Best Answer

Suppose your identified ellipse (which lies in the image plane) is given by the $2D$ curve

$ p(t) = C + \cos t u_1 + \sin t u_2 $

where $C$ is the center of the ellipse, $u_1, u_2$ are the semi-axes of the ellipse. Since the photo is taken straight, then we can assume that

$ C = (0, c_0) $

$ u_1 = (a, 0 ) $

$ u_2 = (0, b) $

where $c_0, a$ and $b$ are known.

Since the image plane lies a distance $z_0$ from the center of the camera (the origin of the camera coordinate frame), then with respect to the camera coordinate frame, the equation of the ellipse is

$ p(t) = (0, c_0, -z_0) + \cos t (a, 0, 0) + \sin t (0, b, 0) $

Connecting the origin to the points of this ellipse generates a cone of view.

It can be shown that this cone has the equation

$p^T Q p = 0 $

where

$ Q = \begin{bmatrix} \dfrac{1}{a^2} && 0 && 0 \\ 0 && \dfrac{1}{b^2} && \dfrac{c_0}{b^2 z_0} \\ 0 && \dfrac{c_0}{b^2 z_0} && \dfrac{ c^2 }{(b z_0)^2} - \dfrac{1}{z_0^2} \end{bmatrix} $

This cone of view is to be intersected with a plane representing the top of the cylinder which is assumed horizontal in the world coordinate frame. Now, with respect to the camera coordinate frame, the equation of this "horizontal" plane is

$ n^T p = K $

where $K$ is an arbitrary non-zero constant. And

$ n = [0, \sin \theta, \cos \theta ]^T =:[0, s, c ]^T $

It can be shown that this intersection curve between the cone of view and the plane representing the top of the cylinder will be a circle if and only if

$ \dfrac{1}{a^2} = \dfrac{1}{b^2} \left( - 2 s c \dfrac{c_0}{z_0} + c^2 + s^2 \left( \dfrac{c_0}{z_0} \right)^2 \right) - \dfrac{s^2}{z_0^2} $

This equation relates the angle $\theta$ (which is the angle between the camera axis (it's $z$ axis) and the normal of the intercepting plane of the cylinder top.

Since the plane of the cylinder top is horizontal, then the camera must be pointing in a direction that makes an angle of $\phi =\dfrac{\pi}{2} - \theta \ $ below the horizontal.

The above equation implies that to determine the focal length $z_0$ we need to know the angle $\phi$ at which the camera was pointing. Then solving for $z_0$ is a a matter of solving a quadratic equation in $\left( \dfrac{1}{z_0} \right) $, which is trivial.

enter image description here

Related Question