By using the segmentation I am able to find the number of pixels occupied by an object in an image. Now I need to kind the distance by using the pixels occupied.
First, we will assume that the lens focuses the image flat.
After segmenting the object out, you will have W2 x H2, the pixel dimensions of the object.
Compare the actual dimensions of the object W x H to your measured dimensions. Take
%Scale on width axis
W / W2 = s1
%Scale on height axis
H / H2 = s2
%Average both scales
s = (s1 + s2) / 2
s
will be your scale measured in meters/pixel. Now examine the total dimensions of the image in pixels, ie 1000 x 1000
or similar. If s=0.01
then your total dimensions would be calculated as
dim * s = L
1000 * 0.01 = 10 meters
Where 10 meters is the size of your frame.
Now you are stuck. If you only have the one image you will not be able to calculate the distance to the object. You need to know how wide the angle of the lens is. This can be taken from the camera specifications or can be derived. If you take one image, measure the distance to the object with a meterstick. Lets call this dimension d
.
We will use some trigonometry to derive the angle of the aperture. For example let d=20 meters
arctan(L/2 / d) = theta
arctan(10/2 / 20) = 14 degrees
With theta derived, let's consider an example. You find L = 10
and have observed theta = 14
.
L/2 / tan(theta) = d
10/2 / tan(14) = 20 meters