Python+OpenCV:摄像机标定(Camera Calibration)

理论

Some pinhole cameras introduce significant distortion to images. Two major kinds of distortion are radial distortion (径向畸变) and tangential distortion (切向畸变).

Radial distortion causes straight lines to appear curved. Radial distortion becomes larger the farther points are from the center of the image.

For example, one image is shown below in which two edges of a chess board are marked with red lines.

But, you can see that the border of the chess board is not a straight line and doesn't match with the red line.

All the expected straight lines are bulged out.

Radial distortion can be represented as follows:

Similarly, tangential distortion occurs because the image-taking lense is not aligned perfectly parallel to the imaging plane.

So, some areas in the image may look nearer than expected. The amount of tangential distortion can be represented as below:

In short, we need to find five parameters, known as distortion coefficients given by:

In addition to this, we need to some other information, like the intrinsic and extrinsic parameters of the camera.

Intrinsic parameters are specific to a camera. They include information like focal length ( fx,fy) and optical centers ( cx,cy).

The focal length and optical centers can be used to create a camera matrix, which can be used to remove distortion due to the lenses of a specific camera.

The camera matrix is unique to a specific camera, so once calculated, it can be reused on other images taken by the same camera.

It is expressed as a 3x3 matrix:

Extrinsic parameters corresponds to rotation and translation vectors which translates a coordinates of a 3D point to a coordinate system.

For stereo applications, these distortions need to be corrected first.

To find these parameters, we must provide some sample images of a well defined pattern (e.g. a chess board).

We find some specific points of which we already know the relative positions (e.g. square corners in the chess board).

We know the coordinates of these points in real world space and we know the coordinates in the image, so we can solve for the distortion coefficients.

For better results, we need at least 10 test patterns.

Camera Calibration in OpenCV

####################################################################################################
# 摄像机标定(Camera Calibration)
def lmc_cv_camera_calibration():"""函数功能: 摄像机标定(Camera Calibration)。"""# termination criteriacriteria = (lmc_cv.TERM_CRITERIA_EPS + lmc_cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)objp = np.zeros((6 * 7, 3), np.float32)objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)# Arrays to store object points and image points from all the images.objpoints = []  # 3d point in real world spaceimgpoints = []  # 2d points in image plane.# 读取所有匹配的图像stacking_images = []images = glob.glob('D:/99-Research/TestData/cv/stereo/case1/left*.png')for image_name in images:image = lmc_cv.imread(image_name, lmc_cv.IMREAD_UNCHANGED)image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)corners_image = image.copy()gray_image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)# Find the chess board cornersret, corners = lmc_cv.findChessboardCorners(gray_image, (7, 6), None)# If found, add object points, image points (after refining them)if ret:objpoints.append(objp)corners2 = lmc_cv.cornerSubPix(gray_image, corners, (11, 11), (-1, -1), criteria)imgpoints.append(corners)# Draw and display the cornerslmc_cv.drawChessboardCorners(corners_image, (7, 6), corners2, ret)# stacking images side-by-sidestacking_image = np.hstack((image, corners_image))stacking_images.append(stacking_image)# 显示图像for i in range(len(stacking_images)):pyplot.figure('Camera Calibration Corners Locations %d' % (i + 1), figsize=(16, 9))# pyplot.subplot(1, 1, 1)pyplot.imshow(stacking_images[i], 'gray')pyplot.title('Camera Calibration Corners Locations')pyplot.xticks([])pyplot.yticks([])pyplot.savefig('%02d.png' % (i + 1))pyplot.show()# Calibrationret, mtx, dist, rvecs, tvecs = lmc_cv.calibrateCamera(objpoints, imgpoints, gray_image.shape[::-1], None, None)# Undistortionimage = lmc_cv.imread(images[11], lmc_cv.IMREAD_UNCHANGED)image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)h, w = image.shape[:2]newcameramtx, roi = lmc_cv.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))# undistort using undistort()undistort_image = lmc_cv.undistort(image, mtx, dist, None, newcameramtx)# crop the imagex, y, w, h = roiundistort_image = undistort_image[y:y + h, x:x + w]# undistort using remappingmapx, mapy = lmc_cv.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), 5)remapping_image = lmc_cv.remap(image, mapx, mapy, lmc_cv.INTER_LINEAR)# crop the imagex, y, w, h = roiremapping_image = remapping_image[y:y + h, x:x + w]# Re-projection Errormean_error = 0for i in range(len(objpoints)):imgpoints2, _ = lmc_cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)error = lmc_cv.norm(imgpoints[i], imgpoints2, lmc_cv.NORM_L2) / len(imgpoints2)mean_error += errorprint("总误差: {}".format(mean_error / len(objpoints)))# 显示图像titles = ['Original Image', 'Corners', 'Undistortion using undistort()', 'Undistortion using remapping']images = [image, corners_image, undistort_image, remapping_image]for i in range(len(images)):pyplot.figure('Camera Calibration')pyplot.subplot(2, 2, i + 1)pyplot.imshow(images[i], 'gray')pyplot.title(titles[i])pyplot.xticks([])pyplot.yticks([])pyplot.savefig('Camera Calibration.png')pyplot.show()# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return

Python+OpenCV:摄像机标定(Camera Calibration)相关推荐

  1. python+OpenCV 相机标定

    相机标定 目录 原理 相机标定结果 流程简介 实验过程 总结 代码及调试问题 相机标定在机器人视觉和畸变校正上都是很关键的一部分,接下来用张正友相机标定法标定我的手机(Vivo xpaly5A)后置摄 ...

  2. matlab棋盘格标定角点,相机标定(Camera calibration)Matlab——棋盘格标定原理,流程...

    计算机视觉----相机标定 相机标定概念:图像测量过程以及计算器视觉中,为确定空间物体某点的三维几何关系位置与其在图像中对应点之间的相互关系,必须建立相机成像的几何模型,模型的参数就是相机的参数.求解 ...

  3. OpenCV相机校准camera calibration的实例(附完整代码)

    OpenCV相机校准camera calibration的实例 OpenCV相机校准camera calibration的实例 OpenCV相机校准camera calibration的实例 #inc ...

  4. OpenCV摄像机videocapture camera的实例(附完整代码)

    OpenCV摄像机videocapture camera的实例 OpenCV摄像机videocapture camera的实例 OpenCV摄像机videocapture camera的实例 #inc ...

  5. python opencv 摄像头标定_(五)单目摄像头标定与畸变矫正(C++,opencv)

    本文将梳理一种单目摄像头标定和矫正的方法,在梳理的过程中,首先使用网上离线的图片数据跑通流程,然后接入自己的camera,手动采集标定图像,实时矫正相机的畸变,然后输出矫正后的图像.全文基于Openc ...

  6. MFC+OPENCV摄像机标定

    摄像机的追踪标定 本文是我第一次在csdn上写的博客,有不详之处,望大家见谅,也希望大家多多支持. 废话不多说,直接进入正题.对于摄像机标定,是学习图像处理和机器视觉不可回避的话题,这方面的现有理论已 ...

  7. 十五天掌握OpenCV——摄像机标定和3D重构!—摄像机标定

    魏老师学生--Cecil:学习OpenCV-机器视觉之旅 基础 代码 设置 标定 畸变校正 反向投影误差 代码演示 Aim: 学习摄像机畸变以及摄像机的内部参数和外部参数: 对畸变图像进行修复. 基础 ...

  8. 计算机视觉-相机标定(Camera Calibration)

    1.相机标定基本原理 1.1 简介 在图像测量过程以及机器视觉应用中,为确定空间物体表面某点的三维几何位置与其在图像中对应点之间的相互关系,必须建立摄像机成像的几何模型,这些几何模型参数就是摄像机参数 ...

  9. ROS 教程之 vision: 摄像头标定camera calibration

    在上一个ROS教程视觉文章中,我们使用usb_cam包读入并发布了图像消息,但是图像没有被标定,因此存在畸变.ROS官方提供了用于单目或者双目标定的camera_calibration包.这个包是使用 ...

  10. python opencv 相机标定_使用OpenCV校准鱼眼镜头的方法

    01.简介 当我们使用的鱼眼镜头视角大于160°时,OpenCV中用于校准镜头"经典"方法的效果可能就不是和理想了.即使我们仔细遵循OpenCV文档中的步骤,也可能会得到下面这个奇 ...

最新文章

  1. 中国首场AI芯片产业峰会成功举办 GTIC 2018热度空前
  2. DDoS***如此猖獗,我们该如何解决?
  3. axure删除的页面怎么恢复_Axure如何应对意外关闭
  4. java 抽象接口类,Java接口(interface)和Java抽象类(abstract class)的区别(详诉版)
  5. 简单爬虫-爬取免费代理ip
  6. Kafka学习-入门
  7. powershell 运行策略
  8. 写在囧男囧女们的七夕节
  9. 浪潮n系列服务器指示灯_【科恩电气】通用薄型区域传感器 NA2N系列
  10. 华科开源多目标跟踪(MOT)实时新SOTA:FairMOT
  11. 官网下载的oracle有病毒,oracle 中勒索病毒怎么恢复?
  12. 企业发展滞缓,还不是因为踩了这四个数据大坑!
  13. 腾讯QQ不为人知的使用技巧
  14. 2013年 macbookair bootcamp 安装win10双系统
  15. 1000瓶有毒的水,用多少只老鼠可以试出有毒的那瓶
  16. python 声音基频f0_如何得到一个曲子的基频?
  17. python3.6学习十四 提示和传递
  18. PCIe TLP包内容 全
  19. word遇到网络地址过长自动换行
  20. 计算机语言python怎么读,python编程怎么读音发音

热门文章

  1. 程序员专属段子集锦 4/10
  2. Linux 命令(19)—— tar 命令
  3. C++ 控制对象的创建方式和数量
  4. [Flex] ButtonBar系列——垂直布局
  5. 无法向会话状态服务器发出回话状态请求
  6. Hi3515主板UBoot参数备份
  7. linux给进程加速,Linux 利用并行进程加速命令执行
  8. mysql5.5免安装包_mysql免安装版5.5
  9. android os FileUriExposedException file storage emulated 0 test tx
  10. java常用序列化与反序列化方法