# 3D objects in 2D drawing: How to get the size of a box relative to a known plane?

Sorry if this is a really badly worded question. Say you have a box of unknown size, and a planar object of a known size (say, a credit card).

You arrange these object somehow (probably with the card aligned with two of the boxes axes) and take a picture of them.

Knowing the exact size of the card, is there any maths I could use to calculate the exact size of the box? Would I have to move the card and take another picture to get the size along the 3rd axis?

-

A photo is usually a projection, the conserved quantity in a projection is the cross ratio of four points on a line. So, for calculation purposes, you want to already know that your credit card shares lines with your box. This approach indeed necessitates more than one picture.

I will first assume that you know the mid-points of the side of your credit card and that the side of the credit card coincide with two sides of the box (that is, the credit card is places directly in a corner).

We will just look at one side of the box and the credit card now, the other calculation is analogous:

Now, measure in your photo $a=$ length from corner to mid-point of credit card side, $b=$ length from mid-point of credit card side to end of credit card side, $c=$ length from corner to other corner of unknown box and $d=$end of credit card side to other corner of unknown box.

Denote $l$ the length of this side of the credit card and $x$ the unknown length of the side of the box.

You have: $$\frac{a}{b}\frac{d}{c}=\frac{x-l}{x}.$$

So, $$x= \frac{lbc}{bc- ad}.$$

How you can recover a mid-point of a credit card side (or more precisely, $a$ and $b$ as above):

Two points, their mid-point and the point at infinity of that line have cross-ratio -1. The two parallel sides of the credit card intersect at the point at infinity. Therefore, with $a,b$ as above, $e$ the length of the credit card in the picture and $m$ the distance of the beginning of the credit card side to the intersection of the credit card side with its "parallel" one in the picture, you get: $$\frac a{e-a} \frac {m-e}{m} = -1$$

which permits you to calculate $a$.

-
This is awesome, thanks :D. Out of curiousity what do you mean by "This can be recovered as a a harmonic point in the photo"?, I don't think there's any way to have the midpoint of the card available so I'll probably need to calculate it. –  J.Ashworth Dec 4 '12 at 22:57
Also will this approach work for a photo from any angle? As long as the edge of the card is exactly parallel with the edge of the box? –  J.Ashworth Dec 4 '12 at 22:58
@J.Ashworth I have added some details. My calculation if for the case that the edge of the card is placed exactly on the edge of the box. If it is just parallel, you would first have to shift the card in the picture by connecting it to the respective points at infinity (each family of originally parallel line intersects in one point which permits to construct parallel lines easily). –  Phira Dec 4 '12 at 23:29
Thanks :), one more question. Would there be any way to get all 3 dimensions of the box from a single picture? Say, if the camera had a "Y" shape reference over it that you had to line the box up with so you knew the exact perspective? –  J.Ashworth Dec 5 '12 at 0:08

If the card is perpendicular to the axis (parallel to the sensor plane), so is the near surface of the box, and they are at the same distance from the camera, the sizes are in the same ratio as the images. So if the near surface of the box looks three times as wide and twice as tall as the card, it is. If the box is aligned this way on axis, the rear surface is hidden and you can tell nothing about the depth. If you can see one of the back sides, the ratio of the image sizes of the edge is the inverse of the ratio of distance from the lens. So if the rear edge is $\frac 45$ as tall as the front edge, it is $\frac 54$ as far from the lens. With today's lenses, finding the optical center of the lens is not easy, so the calculation will have some error.

-