0

I know there already are some answers posted about this topic, but it's not very clear to me how to measure the distance between a camera and an object.

My goal:

I managed to track a red ball with color detection using OpenCV. Now I'm trying to point a laser in the middle of the red ball. The laser should always follow the red ball when it moves. I'm using a small servo motor to turn the laser.

Image

I'm thinking if I can measure the distance between the object and the camera, I will be able to calculate the angle the servo needs to turn...

I tried to follow Cameron Lowell Palmer's post.

What I did:

I calibrated my Pi Camera V2

f_x : 463.64
f_y : 463.64
c_x : 319,50
c_y : 239.5

Native resolution = 3280 x 2464 (I think? I'm not sure how to find this...)
Focal length = 3.00mm (I think? I'm not sure how to find this...)

Then I should be able to measure my object size in pixels? I'm don't know how...

Could someone help me with this?

Community
  • 1
  • 1
  • post sample images... you can detect red color quite well with HSV colorspace. Maybe that will help you detecting the red ball. – Micka Sep 15 '16 at 22:40
  • Detecting the red ball isn't the problem, it's measuring the distance between the red ball and the camera... – Kristof van Woensel Sep 16 '16 at 05:14
  • try solvePnP to get object placement or use the known size of the real ball and the measured size in pixel. – Micka Sep 16 '16 at 05:18

2 Answers2

0

Could you possibly infer the approximate distance by relating pixel size with object (red ball) size? Going back to elementary algebra you infer the height of something far away given known distance between it and an object. In this case you the known are object size, pixel size, and focal length.

jamalin
  • 76
  • 3
  • 1
    Yes, I am trying to find the numbers for this formula Distance (mm) =[ focal lenght(mm) * real height object (mm) * height of image (px)] / [(object height (px) * sensor height (px) ] 1) I am not sure about focal length and sensor height 2) I should consider the inaccuracy values of my calibration That's where I am stuck... – Kristof van Woensel Sep 16 '16 at 05:20
  • Maybe this can help you: http://ksimek.github.io/2013/08/13/intrinsic/ It discusses the intrinsic parameters of a camera model. You already have focal length don't you: f_x and f_y. – jamalin Sep 16 '16 at 05:26
0

I managed to get the distance, but it's not really exact.

What I did:

  • I calibrated the camera as described in the official tutorial
  • I undistorted my image using the cameraMatrix and distortionCoeff. with the undistort() function
  • I thresholded my image to filter out the red ball as explained here.
  • I calculated the distance between the camera and the red ball using the formula:

Distance = FocalLength in mm * (Real object width in mm) / (Virtual object width in px)

  • To get the virtual object width, I drew a contour around my thresholded image (I used minEnclosingCircle). I then calculated the width of this contour.
  • I retrieved my focalLength from the function 'calibrationMatrixValues'

Result image: Result

Distance table (run code snippet):

    <table border="1" style="text-align: center">
    <thead>
      <tr>
        <th>Real Distance (mm)</th>
        <th>Calculated Distance (mm)</th> 
      </tr>
    </thead>
     <tbody>
       <tr>
           <td>100</td> 
           <td>83</td> 
       </tr>
       <tr>
           <td>200</td> 
           <td>174</td> 
       </tr>
       <tr>
           <td>300</td> 
           <td>275</td> 
       </tr>
       <tr>
           <td>400</td> 
           <td>384</td> 
       </tr>
       
     <tbody>   
    </table>

For now, I just add 20 mm to the result. If anyone can help me getting a more exact measurement, I would be very grateful :-).