[Math] Calculate height of rectangle in perspective

geometry

How can one calculate the height of a rectangle where the bottom part is unknown.
The rectangle is in perspective, I know the top part and sides. However not the bottom.

Extra info: it's a picture taken of a paper, due to the angle of the camera the object is in perspective, top part being the furthest away and bottom part outside of the camera closest to you.


Situation like this:
I know the green intersections, therefore the width in between and both sides (not the length obvious).
enter image description here


Below is how I think it should be done, but it's not precise so I'm looking for people to help me determine if I'm doing something wrong or can improve it.

enter image description here

#Paper size:
Pw=21.02cm
Ph=29,73cm

Calibration constant, number of pixels per cm at 50cm distance:
k = ### pixels/cm = (observed pixels calibrated)/Pw
kdist = 50cm

Q = k * Pw pixels width on the observed paper

Distance to top:
dtop = kdist*Q/Px

Px is the top width in pixels

We define a new point for measurement of the width, and define pixels to be perfect squares.
Py = 0.1 * Px (10% down)

New width for the new point, Py: is being manually measured for now.
Px2 calculated at Py.

Distance to Px2:
d2 = kdist*Q/Px2

Calculate height between top and Py:
h = sqrt(sqr(0.1*Pw)+sqr(dtop-d2))

Ratio between 0.1*Pw and full paper length:
r = Ph/h

Bottom line will be:
Y = r*Py

Best Answer

The problem with using "Calibration constant, number of pixels per cm at 50cm distance" is that not all of the scene is at 50cm depth or distance from the camera.

I think you need to remove the perspective correction first like in this example... http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/

Perspective remapping

Your green points would be the red and green ones in this example. Now obviously you want your blue points to be the white and blue in this example, which you don't have because they are outside the image. But if you work out where the left and right edges will coincide with the bottom edge of the image (the last row) you can still perform the perspective correction.

http://i.imgur.com/9T1iBIY.jpg

Now the correction is removed you can map pixels to cm and estimate where the bottom edge will appear.