Shu*_*ubs 2 python opencv computer-vision triangulation
我正在尝试使用 OpenCV Python 中的结构光对投影仪和相机中的点进行三角测量。在此过程中,我有一个在相机和投影仪之间一对一匹配的元组列表。我将其传递给 cv2.un DistortedPoints() ,如下所示:
camera_normalizedPoints = cv2.undistortPoints(camera_points, camera_K, camera_d)
但是,python 抛出以下错误,我无法理解该错误的含义。
camera_normalizedPoints = cv2.undistortPoints(camera_points, camera_K, camera_d)
cv2.error: /home/base/opencv_build/opencv/modules/imgproc/src/undistort.cpp:312: 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
任何帮助是极大的赞赏。
谢谢。
不幸的是,文档并不总是明确说明 Python 中的输入形状,甚至undistortPoints()还没有 Python 文档。
输入点需要是形状为 的数组(n_points, 1, n_dimensions)。因此,如果您有 2D 坐标,它们应该是形状(n_points, 1, 2)。或者对于 3D 坐标,它们应该是 shape (n_points, 1, 3)。对于大多数OpenCV 函数来说都是如此。AFAIK,这种格式适用于所有OpenCV 函数,而一些 OpenCV 函数也接受形状中的点(n_points, n_dimensions)。我发现最好保持所有内容的一致性和格式(n_points, 1, n_dimensions)。
需要明确的是,这意味着四个 32 位浮点 2D 点的数组将如下所示:
points = np.array([[[x1, y1]], [[x2, y2]], [[x3, y3]], [[x4, y4]]], dtype=np.float32)
Run Code Online (Sandbox Code Playgroud)
如果您有一个具有以下形状的数组,(n_points, n_dimensions)您可以使用以下命令扩展它np.newaxis:
>>> points = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> points.shape
(4, 2)
>>> points = points[:, np.newaxis, :]
>>> points.shape
(4, 1, 2)
Run Code Online (Sandbox Code Playgroud)
或与np.expand_dims():
>>> points = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> points.shape
(4, 2)
>>> points = np.expand_dims(points, 1)
>>> points.shape
(4, 1, 2)
Run Code Online (Sandbox Code Playgroud)
np.transpose()或者根据您的尺寸顺序进行各种排序。例如,如果您的形状是(1, n_points, n_dimensions),那么您想要将轴 0 与轴 1 交换以获得(n_points, 1, n_dimensions),因此points = np.transpose(points, (1, 0, 2))将更改轴以首先放置轴 1,然后是轴 0,然后是轴 2,因此新形状将是正确的。
如果您认为这是一种奇怪的点格式,那么如果您只考虑点列表,那么这是合理的,但如果您将点视为图像的坐标,则这是合理的。如果您有图像,则图像中每个点的坐标由一对定义(x, y),例如:
(0, 0) (1, 0) (2, 0) ...
(0, 1) (1, 1) (2, 1) ...
(0, 2) (1, 2) (2, 2) ...
...
Run Code Online (Sandbox Code Playgroud)
在这里,将每个坐标放入双通道数组的单独通道中是有意义的,这样您就可以获得一个 x 坐标的 2D 数组和 y 坐标的一个 2D 数组,例如:
通道 0(x 坐标):
0 1 2 ...
0 1 2 ...
0 1 2 ...
...
Run Code Online (Sandbox Code Playgroud)
通道 1(y 坐标):
0 0 0 ...
1 1 1 ...
2 2 2 ...
...
Run Code Online (Sandbox Code Playgroud)
这就是将每个坐标放在单独通道上的原因。
其他一些需要这种格式的 OpenCV 函数包括cv2.transform()和cv2.perspectiveTransform(),我分别在之前、这里和这里回答了相同的问题。