cv2.getOptimalNewCameraMatrix在某些数据集上返回[0,0,0,0]的ROI [英] cv2.getOptimalNewCameraMatrix returns ROI of [0,0,0,0] on some data sets

查看:388
本文介绍了cv2.getOptimalNewCameraMatrix在某些数据集上返回[0,0,0,0]的ROI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV 2.x开发Python程序.以下是我的代码摘录,该代码在已捕获和保存的文件列表上运行.所有图像均为 80x60 8位灰度图像.我得到的最好的是一个摄像机的投资回报率[1, 6, 73, 49],但是我的另一台摄像机获得的最佳投资回报率是[8, 9, 55, 39].我已经在处理如此小的图像,丢弃约50%的像素并不是一个切实可行的解决方案.我只是不确定是什么原因导致cv2.getOptimalNewCameraMatrix()返回如此小的ROI,尤其是当我从15-40张看起来正确找到角落的图像中送入它时.

I am working on a Python program using OpenCV 2.x Below is an exerpt of my code that runs on a list of already captured and saved files. All the images are 80x60 8-bit greyscale images. The best that I have got is a ROI of [1, 6, 73, 49] for one camera, but my other camera has gotten the best ROI is [8, 9, 55, 39]. I am already working with such small images, throwing away ~50% of my pixels is not really a workable solution. I am just not sure what is causing cv2.getOptimalNewCameraMatrix() to return such small ROI, especially when I feed it anywhere from 15-40 images that seem to have found the corners correctly.

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

A sample data set can be downloaded at: http://s000.tinyupload.com/?file_id=67483192025612443532

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

EDIT I have found a data set after much trial and error that gives me a ROI of [3, 4, 75, 53] so the need for this question is not urgent, but I do find the question interesting. While I was experimenting I found that a good data set + another good picture does not always increase the ROI and can in fact decrease the ROI. That does not intuitively work for me, as more good data should increase the usable area.

推荐答案

已解决.

Tl; dr:请确保校准图像中的棋盘格很好地表示了边缘和拐角.

仅将数据放在失真最小的中心位置,该解决方案会遇到较大的误差.

With data only in the center, where distortion is minimal, the solution is subject to greater error.

在此进行进一步的讨论.

Further discussion here.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆