Search code examples
androidandroid-cameratrigonometryandroid-sensors

Find width of an object from camera and sensors


I should find the width of the object in camera. I have read many posts here in a SO but none of them explained about width calculation, except the distance and height of object. So someone please guide me explaining theoretically with trignometric formulas to find the width of object. I have only one input i.e the height of camera from ground.

Thanks in advance.


Solution

  • Well I am answering my own question to help others having same problem.

    In order to find the width follow below steps. This requires a 3D imagination to understand

    1) User need to enter the height of the point of observation(that is camera height from ground) manually .

    2) The object whose width is to be measured , needs to be at the same level where the person is standing.

    3) We need to point at bottom of the object's start point from the same height and capture the angle.

    4) Then we need to tilt the device at the same height and capture the bottom of the end point of object and measure the angle.

    5) With the angle and camera height from ground, we will find the respective distance of both sides applying tan rule.

       tan(θ)=opposite/adjacent
       tan(θ)=height_of_camera/distance
    

    6) From the distances we calculated in previous step and with the height of camera, using pythagorean theorem, calculate the projected distances ie.camera to the objects start and end points(imagine in 3D view)

    7) Now as we have 2 sides and one angle use the following formula(cosine rule) to get the other side which is the width of the object.

      Width = sqrt((s1*s1) + (s2*s2) − 2(s1)(s2)cos(θ))
      s1=sideOne , s2=sideTwo