我正在使用 OpenCV 2.x 开发一个 Python 程序,下面是我的代码摘录,该代码在已捕获和保存的文件列表上运行。所有图像都是80 x 60 8 位灰度图像。我得到的最好的投资回报率是[1, 6, 73, 49]
对于一台相机,但我的另一台相机获得了最佳的投资回报率[8, 9, 55, 39]
。我已经在处理如此小的图像,丢弃约 50% 的像素并不是真正可行的解决方案。我只是不确定是什么原因造成的cv2.getOptimalNewCameraMatrix()
返回这么小的 ROI,尤其是当我向它提供 15-40 张似乎已正确找到角点的图像时。
import numpy as np
import cv2
import glob
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, .01)
goodImages = 0
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((3*4,3), np.float32)
objp[:,:2] = np.mgrid[0:4,0:3].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('*Left.bmp')
for fname in images:
print("Working on file: %s" % (fname))
img = cv2.imread(fname,cv2.CV_LOAD_IMAGE_COLOR)
gray = cv2.imread(fname,0)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (4,3),None)
# If found, add object points, image points (after refining them)
if ret == True:
print("Found Corners for %s" % (fname))
objpoints.append(objp)
cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),criteria)
if corners is None:
print("Something went wrong with cornerSubPix in file: %s" % (fname))
else:
imgpoints.append(corners)
goodImages+=1
# Draw and display the corners
cv2.drawChessboardCorners(img, (4,3), corners,ret)
cv2.imshow('img',img)
cv2.waitKey(0)
if goodImages >9:
ret, intrinsicMatrix, distortionCoeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
h, w = img.shape[:2]
refinedCameraMatrix, roi=cv2.getOptimalNewCameraMatrix(intrinsicMatrix,distortionCoeffs,(w,h),1,(w,h))
np.savez("LeftCamera", refinedCameraMatrix=refinedCameraMatrix, roi=roi, intrinsicMatrix=intrinsicMatrix, distortionCoeffs=distortionCoeffs)
样本数据集可以在以下位置下载:http://s000.tinyupload.com/?file_id=67483192025612443532
EDIT经过多次试验和错误,我发现了一个数据集,它给我带来了投资回报率[3, 4, 75, 53]
所以这个问题的必要性并不迫切,但我确实觉得这个问题很有趣。当我进行实验时,我发现一个好的数据集+另一个好的图片并不总是会增加投资回报率,实际上还会降低投资回报率。这对我来说并不直观,因为更多好的数据应该会增加可用区域。