If I have a photograph of a rectangular object whose image recedes with perspective, is it possible to work out the scale of measurements along that object?

For example: I have a photograph of a (metric) ruler.

The ruler is graduated in centimetres as shown, 30 cms.

Is it possible to work out the distance between each centimetre?

The screenshot shows the photograph.

The representation on the Picture Plane shows the nearest edge of the ruler to be 50mm and the farthest edge to be 15mm. By measuring the photo, I see that the distance between 0 and 1 is 7.5mm and the distance between 27 and 28 is 0.9mm.

Is there a way to get these results from a formula?

It doesn't need to be 100% accurate: it's for photometric software I'm writing, so it's only going to be used as a rough guide.