If you know the height or width of an object and it’s distance, you can trace out a triangle. The vertex is the center of lens. Example an object is 2 meter in height, 8 meters from the camera -- the length of the base of this triangle is 2 meters, the height of this triangle is 8 meters. Bisect the triangle and ½ the base is 1 meter. The ratio of base to height is 1 ÷ 8 = 0.125.
Inside the camera, the projected image of this object traces out a similar triangle. The vertex is at the center of the rear lens element. The height of this triangle is the focal length. Suppose the lens has a focal length of 75mm. The height of the image as projected on the sensor will be 2 X 75 X 0.125 = 9.375 X 2 = 18.75mm.
Technically the measurement points associated with the lens is the front and rear nodal points. You will not easily discover their locations. If the subject is a meter or so distant, you need not worry, such a small inaccuracy is moot. Likely if you use the rear element as the rear nodal your calculation will also be satisfactory. If not, refine the height of the in-camera triangle by trial and error to improve accuracy.