pythonopencvimage-processingpixel

How to measure object size in real world in terms of measurement like inches centimeters etc from object size in the image in pixels?


I have calculated object size in terms of pixel from the image containing object. I want to measure object size in real world. Is there any way to find out multiplying factor to measure actual size ? I'm currently using python for implementation.


Solution

  • Typically you will have obtained your image with a camera which projects the 3 dimensional scene onto a 2 dimensional sensor by means of a lens. The vertical (height) projection is represented in the following diagram ( I am assuming a rectilinear lens):

    Lens Scene to Sensor projection (Vertical axis)

    You say the height of your object of interest in pixels of your image as 150 pixels:

    h_{obj} = 150 pixels

    You say the image size has a total height of 800 pixels, assuming this is the sensor resolution:

    h_{sensor} = 800 pixels

    You are interested in finding the real height in metric system of the object H_{obj} that finds itself at distance D from the camera.

    Expressing the angles in radians we can establish the following relationships:

    D \cdot \theta = H_{obj} , f \cdot \theta = h_{obj}

    where f is the focal length of the lens.

    isolating terms and substituting we reach \frac{D}{f}\cdot h_{obj} = H_{obj}

    but you have expressed h_{obj} in pixels and you want H_{obj} expressed in the metric system. So, lets first move from pixels to millimeters.

    h_{obj} (mm) = \frac{h_{obj}(pixels)}{h_{sensor}(pixels)}\cdot h_{sensor} (mm)

    Lets assume you don't know the sensor height, so we keep it as the variable for the moment. Rearranging, expressing focal length as millimeters (mm) and substituting into the previous equation we have:

    D \cdot \frac{h_{obj}(pixels)}{h_{sensor}(pixels)} \cdot \frac{h_{sensor}(mm)}{f(mm)} = H_{obj}

    notice the term : \frac{h_{sensor}(mm)}{f(mm)} = \alpha (radians)

    \alpha represents the Vertical Field of View (since we use the sensor height) of the camera, this is a parameter that is typically given for camera & lens calculations.

    3dimensional Field of View

    Normally its given in degrees, so we just convert it to radians.

    \frac{V_{fov}º}{180º}\cdot \pi = V_{fov} (radians)

    • If you actually know the focal length of your lens and the size of your sensor, just calculate the Field of View directly. This leaves us with the following equation:

    H_{obj} = D \cdot \frac{h_{obj}(pixels)}{h_{sensor}(pixels)} \cdot V_{FOV} (radians)

    The Field of View is the parameter you are missing to be able to complete your calculation. To complete the example let's assume it is 90º:

    V_{FOV} = \frac{\pi}{2} (radians)

    The system of units you use now to express your distance D will define the units with which the h_{obj} will be expressed in.

    H_{obj} (meters) = D(meters) \cdot \frac{150}{800} \cdot \frac{\pi}{2}


    Another approach is, given a Field of View, and assuming a rectilinear lens, you can calculate pixel height and pixel width at a defined distance from the camera.

    Vertical resolution at distance D

    V_{resolution@D(meters)} \left(\frac{mm}{pixel}\right) = \frac{1000(mm)}{1(meter)} \cdot D (meters) \cdot \frac{V_{FOV}(radians)}{V_{sensor}(pixels)}


    For more information on Field of view: