If you know the actual width or height and camera-to-object distant, you can easily calculate a ratio size-to-distance.
Say a object is 3 meters wide and a picture is taken with the camera positioned 12 meters from the subject. The ratio size-to-distance is 3÷ 12 = 0.250.
Inside the camera, the image forming rays from the lens, trace out the same ratio as they travel. In other words, the projection distance inside the camera and the image size yield the same ratio. If one of these values is known, the other is easily calculated.
Likely, of the two, the projection distance is the most easily discoverable. This will be the focal length of the lens at the time the picture was taken.
Suppose the camera’s zoom lens was set to 50mm (or a prime 50mm was used). The span of the projected image will be 50 X 0.250 = 12.5mm.
From the camera specification sheet, we can discover the pixel pitch. Suppose the center-to-center pixel spacing is 3.89 µm (micrometers), 0.00389mm.
Now we calculate the span of the image in pixels is 12.5 ÷ 0.00389 = 3214 pixels (rounded up).
The span of the image covers 3214 pixels = 12.5mm