2

我想校准汽车视频录像机,并将其用于运动结构(SfM)的三维重建。我用这台相机拍摄的照片的原始尺寸为1920x1080。基本上,我一直在使用OpenCV tutorial的源代码进行校准。使用OpenCV进行运动结构的摄像机标定(Python)

但也有一些问题,我会很感激任何帮助。

所以,像往常一样(至少在上面的源代码),这里是管道:

  1. 查找与findChessboardCorners
  2. 棋盘角落cornerSubPix
  3. 画出它获取它的像素值可视化与drawhessboardCorners
  4. 然后,我们校准相机的电话calibrateCamera
  5. 致电getOptimalNewCameraMatrixundistort功能undistort图像

在我的情况下,由于图片太大(1920×1080),我把它调整为640X320(SFM过程中,我也将使用这个尺寸的图像,因此,我不认为这会有什么问题)。而且,我还使用了9x6棋盘角进行校准。

在这里,问题出现了。在致电getOptimalNewCameraMatrix后,失真出现完全错误。即使是返回的投资回报率是[0,0,0,0]。以下是原始图像和无失真的版本:

Original image ​​ 你可以看到不失真的图像中的图像左下角。

但是,如果我没有拨打getOptimalNewCameraMatrix而只是直接拨打undistort,那我就有了一个相当不错的形象。 Undistorted image

所以,我有三个问题。

  1. 这是为什么?我尝试过使用同一台相机拍摄的另一个数据集,以及我的iPhone 6 Plus,但结果与上述相同。

  2. 另一个问题是,什么是getOptimalNewCameraMatrix呢?我已经多次阅读文档,但仍然无法理解它。从我观察到的情况来看,如果我没有拨打getOptimalNewCameraMatrix,我的图片将保留其大小,但会缩放和模糊。任何人都可以更详细地解释这个函数吗?

  3. 对于SfM,我估计拨打getOptimalNewCameraMatrix很重要?因为如果不是这样,未失真的图像将变得更加放大和模糊,从而使关键点检测变得更加困难(在我的情况下,我将使用光流)?

我用opencv示例图片测试了代码,结果很好。

下面是我的源代码:

from sys import argv 
import numpy as np 
import imutils # To use the imutils.resize function. 
         # Resizing while preserving the image's ratio. 
         # In this case, resizing 1920x1080 into 640x360. 
import cv2 
import glob 

# termination criteria 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.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((9*6,3), np.float32) 
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2) 

# Arrays to store object points and image points from all the images. 
objpoints = [] # 3d point in real world space 
imgpoints = [] # 2d points in image plane. 

images = glob.glob(argv[1] + '*.jpg') 
width = 640 

for fname in images: 
    img = cv2.imread(fname) 
    if width: 
     img = imutils.resize(img, width=width) 

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

    # Find the chess board corners 
    ret, corners = cv2.findChessboardCorners(gray, (9,6),None) 

    # If found, add object points, image points (after refining them) 
    if ret == True: 
     objpoints.append(objp) 

     corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 
     imgpoints.append(corners2) 

     # Draw and display the corners 
     img = cv2.drawChessboardCorners(img, (9,6), corners2,ret) 
     cv2.imshow('img',img) 
     cv2.waitKey(500) 

cv2.destroyAllWindows() 
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) 

for fname in images: 
    img = cv2.imread(fname) 
    if width: 
     img = imutils.resize(img, width=width) 

    h, w = img.shape[:2] 
    newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h)) 

    # undistort 
    dst = cv2.undistort(img, mtx, dist, None, newcameramtx) 

    # crop the image 
    x,y,w,h = roi 
    dst = dst[y:y+h, x:x+w] 
    cv2.imshow("undistorted", dst) 
    cv2.waitKey(500) 

mean_error = 0 
for i in xrange(len(objpoints)): 
    imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist) 
    error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2) 
    mean_error += error 

print "total error: ", mean_error/len(objpoints) 

已经请同学们在answers.opencv.org,他想我的代码和我成功的数据集。我不知道什么是错的。

回答

0

问题2:

随着cv::getOptimalNewCameraMatrix(...)您可以根据自由缩放参数alpha计算一个新的相机矩阵。

如果alpha设置为1那么所有的源图像像素都保留在未失真的图像中,沿着未失真图像看到黑色和曲线边界(如枕形)。这种情况对于几种计算机视觉算法是不利的,因为例如在未失真的图像上出现新的边缘。

默认情况下cv::undistort(...)规定将在校正图像中可见的源图像的子集,这就是为什么只显示可感知像素 - 校正图像周围没有枕形,但数据丢失。

不管怎样,你被允许控制源图像的子集,将校正图像中可见:

cv::Mat image, cameraMatrix, distCoeffs; 
// ... 

cv::Mat newCameraMatrix = cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, image.size(), 1.0); 

cv::Mat correctedImage; 
cv::undistort(image, correctedImage, cameraMatrix, distCoeffs, newCameraMatrix); 

问题1:

这只是我的感觉,但如果在校准后重新调整图像大小,则还必须小心,例如:相机矩阵也必须“缩放”,例如:

cv::Mat cameraMatrix; 
cv::Size calibSize; // Image during the calibration, e.g. 1920x1080 
cv::Size imageSize; // Your current image size, e.g. 640x320 
// ... 

cv::Matx31d t(0.0, 0.0, 1.0); 
t(0) = (double)imageSize.width/(double)calibSize.width; 
t(1) = (double)imageSize.height/(double)calibSize.height; 

cameraMatrixScaled = cv::Mat::diag(cv::Mat(t)) * cameraMatrix; 

这只能用于摄像机矩阵,因为失真系数不取决于分辨率。

问题3:

无论我想cv::getOptimalNewCameraMatrix(...)是不是在你的情况下,重要的是,失真的图像可以因为你删除一个非线性变换的放大效应和模糊。如果我是你,那么我会尝试光流而不要致电cv::undistort(...)。我认为,即使是扭曲的图像也可以包含许多用于跟踪的优秀特征。

+0

在校准过程中,我已将图像大小调整为640x320。所以,我不需要重新调整我的内在参数是不是?而且,至于cv :: undistort,我确实需要它,因为对于SfM,我将需要图像的未失真版本 – Hilman

+1

然后,您不必重新调整内部函数,只需在'cv: :如'cv :: calibrateCamera(...)'中的unistort(...)'。不使用'cv :: getOptimalNewCameraMatrix(...)'来实现你的方法也更好。 – Kornel

+0

但这只是让我想知道。问题是什么?错误?由于直接调用'cv2.undistort'解决这个问题,我可以假设校准是成功的? – Hilman