繁体   English   中英

如何使用python cv2 api格式化undistortPoints的xy点?

[英]How to format xy points for undistortPoints with the python cv2 api?

我无法格式化x,y列表以传递给undistortPoints(opencv 2.4.1)。

错误消息是特定于c ++的,并且抱怨不是CV_32FC2类型的点数组。 我不应该能够传入Nx2 numpy阵列吗?

import cv2

camera_matrix = array(mat('1.3e+03, 0., 6.0e+02; 0., 1.3e+03, 4.8e+02; 0., 0., 1.'), dtype=float32)
dist_coeffs = array(mat('-2.4-01, 9.5e-02, -4.0e-04, 8.9e-05, 0.'), dtype=float32)

test = zeros((10,2), dtype=float32)

print test.shape, type(test)

xy_undistorted = cv2.undistortPoints(test, camera_matrix, dist_coeffs)

结果是:

opencv/modules/imgproc/src/undistort.cpp:279: error: (-215) CV_IS_MAT(_src) && CV_IS_MAT(_dst) && (_src->rows == 1 || _src->cols == 1) && (_dst->rows == 1 || _dst->cols == 1) && _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 && (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) && (CV_MAT_TYPE(_dst->type) == CV_32FC2 || CV_MAT_TYPE(_dst->type) == CV_64FC2) in function cvUndistortPoints

在samples / python2 / video.py中,使用了projectPoints,它接受一个数组并对其进行整形(-1,3),从而得到该函数的Nx3数组,看起来这里的格式应该相同。

我不太了解相机校准。 但看到你的代码和错误,我改变如下:

import cv2
import numpy as np
camera_matrix = np.array([[1.3e+03, 0., 6.0e+02], [0., 1.3e+03, 4.8e+02], [0., 0., 1.]], dtype=np.float32)
dist_coeffs = np.array([-2.4-01, 9.5e-02, -4.0e-04, 8.9e-05, 0.], dtype=np.float32)

test = np.zeros((10,1,2), dtype=np.float32)
xy_undistorted = cv2.undistortPoints(test, camera_matrix, dist_coeffs)

print xy_undistorted

以下是我得到的结果,检查它是否正确:

[[[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]

 [[ 0.0187303   0.01477836]]]

问题是什么 :

错误说,源应该有EITHER one row OR one column 它应该是CV_32FC2或CV_64FC2,表示两个通道和浮点。 因此,使你的src形状(10,1,2) or (1,10,2) 这两种方法都有效并且给出相同的结果(我自己检查过)。 唯一的问题是,我不知道它是否正确,所以请自行检查。

我测试了Abid Rahman的方法并且效果很好,但有一些问题需要注意。

calib = np.reshape(calib, [3, 3])
dist = np.array(kp_mat, dtype=np.float32)
newcameramtx, _ = cv2.getOptimalNewCameraMatrix(calib, dist, (w, h), 1, (w, h))
# undistort
dst = cv2.undistortPoints(pt, calib, dist, None, newcameramtx)
dst = np.squeeze(dst)

注意

  1. 点应为[1,N,2]或[N,1,2]形状,坐标为[x,y]
  2. 应指定新的相机矩阵。 或者输出将是标准化坐标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM