不使用cv2.findChessboardCorners在Python中进行OpenCV摄像机校准 [英] OpenCV camera calibration in Python without using cv2.findChessboardCorners
问题描述
我正在尝试使用openCV进行相机校准.只要使用cv2.findChessBoardCorners在图像中找到我的校准目标,我就没有问题,但是如果我使用自己的函数来找到这些点并使用这些点构建一个数组,则在尝试估计相机参数.这是一个将引发相同错误的示例.
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))
它工作正常.在这两种情况下,角的类型均为ndarray大小(12,2),并且元素均为float32.
it works fine. The type of corners is an ndarray size (12,2) in both cases and the elements are float32.
为什么会出现此错误:
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
当我尝试从头开始构建img_points数组而不是使用cv2.findChessboardCorners时?
when I try to construct the img_points array from scratch instead of using cv2.findChessboardCorners?
推荐答案
我遇到了同样的问题.我通过使用向量的向量来解决它,如所记录的;对于每一帧,
imPts = [ [px0, py0, pz0],..., [pxn, pyn, pzn] ]
,和
obPts = [ [qx0, qy0],..., [qxn, qyn] ]
,然后在函数中使用它们时执行:imPts.astype('float32')
和obPts.astype('float32')
.如果使用多于一帧,则对每一帧都这样做.这样就可以了.
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.
这篇关于不使用cv2.findChessboardCorners在Python中进行OpenCV摄像机校准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!