# Calculate size of an object inside a picture

## Issue

I’m trying to calculate the real size of a papersheet sticked to a panel whose I know the dimensions. Here is a little example : My program works fine when the picture is taken in front of the panel, I get 0.416 * 0.294 while my sheet’s real size is 0.420 * 0.297 which is very satisfying. You can try my code here.
And below is the function I made for the calculations

``````window.calculer = function(){

//-------------Calcul de X ---------------------------------------------------------------------------

ag_distance_x_metres=0.9;

ag_delta_x=x2-x1;                                                                       // Calcul des ag_delta_x et ag_delta_y
ag_delta_y=y2-y1;

ag_distance_x_pixels=Math.sqrt(Math.pow(ag_delta_x, 2) + Math.pow(ag_delta_y, 2));      // Calcul de la distance x en pixels

ag_rapport_x = ag_distance_x_pixels/ag_distance_x_metres;                               // Calcul du rapport taille pixel / taille mètres

ag_delta_X = x5-x4;                                                                     // Calcul des ag_delta_X et ag_delta_Y
ag_delta_Y = y5-y4;

ag_distance_X_pixels=Math.sqrt(Math.pow(ag_delta_X, 2) + Math.pow(ag_delta_Y, 2));      // Calcul de la distance X en pixels

ag_distance_X_metres=ag_distance_X_pixels/ag_rapport_x;                                 // Calcul de la distance X en mètres
alert("ag_distance_X_metres = "+ ag_distance_X_metres);

//-------------Calcul de Y -----------------------------------------------------------------------------

ag_distance_y_metres=1.20;

ag_delta_x=x3-x2;                                                                       // Calcul des ag_delta_x et ag_delta_y
ag_delta_y=y3-y2;

ag_distance_y_pixels=Math.sqrt(Math.pow(ag_delta_x, 2) + Math.pow(ag_delta_y, 2));      // Calcul de la distance y en pixels

ag_rapport_y = ag_distance_y_pixels/ag_distance_y_metres;                               // Calcul du rapport taille pixel / taille mètres

ag_delta_X = x6-x5;                                                                     // Calcul des ag_delta_X et ag_delta_Y
ag_delta_Y = y6-y5;

ag_distance_Y_pixels=Math.sqrt(Math.pow(ag_delta_X, 2) + Math.pow(ag_delta_Y, 2));      // Calcul de la distance Y en pixels

ag_distance_Y_metres=ag_distance_Y_pixels/ag_rapport_y;                                 // Calcul de la distance Y en mètres
alert("ag_distance_Y_metres = "+ ag_distance_Y_metres);

}
``````

Now I’m trying to do the exact same thing, but when the picture is not taken in front of the panel, like this one : You can try the same code with this image here

With this image I get 0.405*0.254 for my sheet dimensions, which is not that far, but not good enough. I’d like to have something more accurate, but I don’t know how to do it, I think I may have to take more parameters into account but I don’t really know much about photography and I’m kinda lost here.

Any help would be greatly appreciated 🙂

Edit : As there is isn’t any answers, and i searched on google without success, I’m starting to think that I don’t have enough datas to make such a calculation.
Now what if :

• I have the focal length and sensor size
• I have the distances between my camera and the points 1,2 and 3
• I have both

## Solution

You have to find the vanishing points of your perspective, and use it to compute the cross-ratio of the parallel lines. See this question for more details.

As it is said in the answer, your first example works because:

If the vanishing point of a given line is at infinity, like for the horizontal
lines in your picture, then they are only subject to an affine
transformation, so ratios of lengths on these lines are preserved by
the perspective projection.

For your second example, you have to use the method given in the answer.

Let’s consider that you have a coordinate system, with origin O (the side of the panel) and axis x (inverse of the direction of the vanishing point from O). A is the vanishing point, B, the other side of the panel, and C and D the intersection of axis x with the sheet. The cross-ratio `CR(A,O;B,C)` is `((A-B)/(A-C))/((O-B)/(O-C)) = 0.189`, with
`A = -317px` (distance from O to A in the resized picture), `B = 166`, `C = 22`, `D = 92.`
So, since you know that `OB = 90cm`, you have `OC = 0.189 * 90 = 17 cm`.
For `OD, CR(A,O;B,D) = 0.654`, so you have `OD = 0.654 * 90 = 58.9`.
Thus, `CD = OD - OC = 41.9 cm`.

You have to do the same for the other direction, following the blue lines.

If your sheet is not aligned with the panel direction, you just have to compute the coordinates of each corner in a coordinate system defined by the panel axes. The principle remains the same.

Answered By – Gwen

Answer Checked By – Robin (AngularFixing Admin)