OpenCV camera calibration in Python without using cv2.findChessboardCorners

穿精又带淫゛_ 提交于 2019-12-10 19:03:33

问题


I am trying to use openCV for camera calibration. I do not have a problem as long as I use the cv2.findChessBoardCorners to find my calibration targets in the image, but if I use my own function to find the points and build an array with the points, I get an error when trying to estimate the camera parameters. Here is an example that will throw the same error.

import numpy as np
import cv2


pattern_size         = (4, 3)
pattern_points       = np.zeros( (np.prod(pattern_size), 3), np.float32 )
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points      *= 20

obj_points = []
img_points = []

for fn in range(5):
    corners = np.asarray(pattern_points[:,1:], dtype=np.float32)

    img_points.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)


ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points,
                                                   img_points,
                                                   (1088, 2048),
                                                   None,
                                                   None)

If I instead make the corners array with the usual

ret, corners = cv2.findChessboardCorners(gray, (4,3))

it works fine. The type of corners is an ndarray size (12,2) in both cases and the elements are float32.

Why do I get this error:

OpenCV Error: Unsupported format or combination of formats (imagePoints1 should contain vector of vectors of points of type Point2f) in cv::collectCalibrationData, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\calib3d\src\calibration.cpp, line 2982

when I try to construct the img_points array from scratch instead of using cv2.findChessboardCorners?


回答1:


I've been having the same issue. I solved it by using a vector of vectors, as documented; for each frame, imPts = [ [px0, py0, pz0],..., [pxn, pyn, pzn] ], and obPts = [ [qx0, qy0],..., [qxn, qyn] ], then do: imPts.astype('float32') and obPts.astype('float32'), when using them inside the function. If more than one frame is used, then do that for each frame. That does the trick.



来源:https://stackoverflow.com/questions/31806135/opencv-camera-calibration-in-python-without-using-cv2-findchessboardcorners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!