OpenCV:如何使用图像计算相机和物体之间的距离? [英] OpenCV: How-to calculate distance between camera and object using image?

查看:1639
本文介绍了OpenCV:如何使用图像计算相机和物体之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenCV的新手。我正在使用以下公式来计算距离:

I am a newbie in OpenCV. I am working with the following formula to calculate distance:

distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels)
                          ----------------------------------------------------------------
                                object height (pixels) * sensor height (mm)

OpenCV中是否有可以确定物距的函数?如果没有,对样本代码的任何引用?

Is there a function in OpenCV that can determine object distance? If not, any reference to sample code?

推荐答案

如何计算给定已知大小的对象的距离



你需要事先知道两件事之一

How to calculate distance given an object of known size

You need to know one of 2 things up front


  • 焦距(单位为mm和像素) mm)

  • 图像传感器的物理尺寸(计算每毫米像素数)

I我将使用焦距,因为我不想谷歌传感器数据表。

I'm going to use focal-length since I don't want to google for the sensor datasheet.

使用OpenCV calibrate.py 工具和源代码中提供的Chessboard pattern PNG生成校准矩阵。我从尽可能多的角度拍摄了大约二十张棋盘的照片,然后将文件导出到我的Mac上。有关更多详细信息,请查看 OpenCV的相机校准文档

Use the OpenCV calibrate.py tool and the Chessboard pattern PNG provided in the source code to generate a calibration matrix. I took about 2 dozen photos of the chessboard from as many angles as I could and exported the files to my Mac. For more detail check OpenCV's camera calibration docs.

RMS: 1.13707201375
camera matrix:
[[  2.80360356e+03   0.00000000e+00   1.63679133e+03]
 [  0.00000000e+00   2.80521893e+03   1.27078235e+03]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
distortion coefficients:  [ 0.03716712  0.29130959  0.00289784 -0.00262589 -1.73944359]




  • f_x = 2803

  • f_y = 2805

  • c_x = 1637

  • c_y = 1271

    • f_x = 2803
    • f_y = 2805
    • c_x = 1637
    • c_y = 1271
    • 检查拍摄的棋盘系列照片的详细信息,您会发现原始分辨率(3264x2448) )照片及其JPEG EXIF标题,visi在iPhoto中,您可以找到焦距值(4.15mm)。这些项目取决于相机。

      Checking the details of the series of chessboard photos you took, you will find the native resolution (3264x2448) of the photos and in their JPEG EXIF headers, visible in iPhoto, you can find the Focal Length value (4.15mm). These items should vary depending on camera.

      我们需要知道每毫米像素数( px / mm)在图像传感器上。从相机切除页面我们知道f_x和f_y是焦距乘以缩放因子。

      We need to know the pixels per millimeter (px/mm) on the image sensor. From the page on camera resectioning we know that f_x and f_y are focal-length times a scaling factor.

      f_x = f * m_x
      f_y = f * m_y
      

      由于每个公式都有两个变量,我们可以求解m_x和m_y。我只是平均2803和2805得到2804.

      Since we have two of the variables for each formula we can solve for m_x and m_y. I just averaged 2803 and 2805 to get 2804.

      m = 2804px / 4.15mm = 676px/mm 
      



      对象大小(以像素为单位)



      我使用OpenCV(C ++)来抓取了解点的 Rotated Rect 并确定对象的大小为41px。注意我已经检索了对象的角落,并询问边界矩形的大小。

      Object size in pixels

      I used OpenCV (C++) to grab out the Rotated Rect of the points and determined the size of the object to be 41px. Notice I have already retrieved the corners of the object and I ask the bounding rectangle for its size.

      cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
      



      小皱纹



      对象是41px在相机拍摄的视频@ 640x480。

      Small wrinkle

      The object is 41px in a video shot on the camera @ 640x480.

      3264/676 = 640/x
      x = 133 px/mm
      

      给定41px / 133px / mm,我们看到图像传感器上物体的尺寸为.308mm。

      So given 41px/133px/mm we see that the size of the object on the image sensor is .308mm .

      distance_mm = object_real_world_mm * focal-length_mm / object_image_sensor_mm
      distance_mm = 70mm * 4.15mm / .308mm
      distance_mm = 943mm
      

      这恰好相当不错。我测量了910毫米并进行了一些改进,我可以减少误差。

      This happens to be pretty good. I measured 910mm and with some refinements I can probably reduce the error.

      感谢您的反馈。

      Adrian在 pyimagesearch.com 上演示使用类似三角形的不同技术。我们事先讨论了这个话题,他采用了类似的三角形方法,我做了相机内在函数。

      Adrian at pyimagesearch.com demonstrated a different technique using similar triangles. We discussed this topic beforehand and he took the similar triangles approach and I did camera intrinsics.

      这篇关于OpenCV:如何使用图像计算相机和物体之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆