【问题标题】:cv2.getOptimalNewCameraMatrix returns ROI of [0,0,0,0] on some data setscv2.getOptimalNewCameraMatrix 在某些数据集上返回 [0,0,0,0] 的 ROI
【发布时间】:2021-08-12 18:38:42
【问题描述】:

我正在使用 OpenCV 2.x 编写 Python 程序 下面是我的代码的摘录,它在已捕获和保存的文件列表上运行。所有图片均为80x60 8 位灰度图片。我得到的最好的是一个相机的投资回报率为[1, 6, 73, 49],但我的另一台相机获得了最好的投资回报率是[8, 9, 55, 39]。我已经在处理这么小的图像了,丢弃大约 50% 的像素并不是一个真正可行的解决方案。我只是不确定是什么导致cv2.getOptimalNewCameraMatrix() 返回如此小的投资回报率,尤其是当我将 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

编辑 经过多次反复试验,我找到了一个数据集,该数据集的投资回报率为[3, 4, 75, 53],因此对这个问题的需求并不迫切,但我确实觉得这个问题很有趣。在我进行实验时,我发现一个好的数据集 + 另一个好的图片并不总是会增加 ROI,实际上会降低 ROI。这对我来说并不直观,因为更多好的数据应该会增加可用区域。

【问题讨论】:

标签: python opencv


【解决方案1】:

解决了。

Tl;dr:确保边缘和角落在校准图像中由棋盘格很好地表示。

数据只在中心,失真最小,解决方案会出现更大的误差。

在这里进一步讨论。

http://answers.opencv.org/question/28438/undistortion-at-far-edges-of-image/?answer=180493#post-id-180493

【讨论】:

    【解决方案2】:

    我发现标准对此很重要: self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1) 返回好的值。

    self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.01) 返回 0 0 0 0

    换句话说,如果您将最后一项设置为 0.1,它可能会起作用,如果它更低......可能不会 (如果我将最后一个值设置为 1,它也可以工作......也许只是尝试修改该值,看看什么最适合你)

    我仍然不确定该值的作用,但是,如果我将其设置为 0.05,它可以工作并且图片是像素化(缩放)的,如果我将它设置为更高的值,例如 5 或 10,它看起来仍然可以工作但图片更清晰(未缩放)。

    总体而言,如果您遇到此问题,请尝试调整终止条件中的最后一个值。

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 2017-04-24
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多