我想校准汽车视频录像机,并将其用于运动结构(SfM)的三维重建。我用这台相机拍摄的照片的原始尺寸为1920x1080。基本上,我一直在使用OpenCV tutorial的源代码进行校准。使用OpenCV进行运动结构的摄像机标定(Python)
但也有一些问题,我会很感激任何帮助。
所以,像往常一样(至少在上面的源代码),这里是管道:
- 查找与
findChessboardCorners
- 棋盘角落
cornerSubPix
- 画出它获取它的像素值可视化与
drawhessboardCorners
- 然后,我们校准相机的电话
calibrateCamera
- 致电
getOptimalNewCameraMatrix
和undistort
功能undistort图像
在我的情况下,由于图片太大(1920×1080),我把它调整为640X320(SFM过程中,我也将使用这个尺寸的图像,因此,我不认为这会有什么问题)。而且,我还使用了9x6棋盘角进行校准。
在这里,问题出现了。在致电getOptimalNewCameraMatrix
后,失真出现完全错误。即使是返回的投资回报率是[0,0,0,0]
。以下是原始图像和无失真的版本:
但是,如果我没有拨打getOptimalNewCameraMatrix
而只是直接拨打undistort
,那我就有了一个相当不错的形象。
所以,我有三个问题。
这是为什么?我尝试过使用同一台相机拍摄的另一个数据集,以及我的iPhone 6 Plus,但结果与上述相同。
另一个问题是,什么是
getOptimalNewCameraMatrix
呢?我已经多次阅读文档,但仍然无法理解它。从我观察到的情况来看,如果我没有拨打getOptimalNewCameraMatrix
,我的图片将保留其大小,但会缩放和模糊。任何人都可以更详细地解释这个函数吗?对于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,他想我的代码和我成功的数据集。我不知道什么是错的。
在校准过程中,我已将图像大小调整为640x320。所以,我不需要重新调整我的内在参数是不是?而且,至于cv :: undistort,我确实需要它,因为对于SfM,我将需要图像的未失真版本 – Hilman
然后,您不必重新调整内部函数,只需在'cv: :如'cv :: calibrateCamera(...)'中的unistort(...)'。不使用'cv :: getOptimalNewCameraMatrix(...)'来实现你的方法也更好。 – Kornel
但这只是让我想知道。问题是什么?错误?由于直接调用'cv2.undistort'解决这个问题,我可以假设校准是成功的? – Hilman