如何使用OpenCV 4.10.0的charuco板进行相机校准?

问题描述 投票:0回答:1

我正在尝试使用 OpenCV 版本

4.10.0.
进行相机校准 我已经获得了常用棋盘格的工作版本,但我不知道它如何与 charuco 一起使用。如果有任何有效的代码示例,我将不胜感激。

我尝试过的: 我尝试按照本教程进行操作:https://medium.com/@ed.twomey1/using-charuco-boards-in-opencv-237d8bc9e40d

似乎缺少诸如

cv.aruco.interpolateCornersCharuco
cv.aruco.interpolateCornersCharuco
之类的基本功能。 甚至抛出文档说明现有的Python实现,请参阅:https://docs.opencv.org/4.10.0/d9/d6a/group__aruco.html#gadcc5dc30c9ad33dcf839e84e8638dcd1

我还尝试遵循C++的官方文档,请参阅https://docs.opencv.org/4.10.0/da/d13/tutorial_aruco_calibration.html

Python 中的 ArucoDetector 没有

detectBoard
方法。所以完全遵循这个教程也是不可能的。

但我从文档中的提示猜想Medium使用的函数已被弃用?但没有任何地方标记为“已删除”!

我已经检测到标记了:

Detected image.

但是获取物体和图像点失败:

`object_points_t, image_points_t = charuco_board.matchImagePoints( marker_corners, marker_ids)`

任何帮助或工作代码将不胜感激。

附注: 我的“DetectMarkers”方法的输出似乎是有效的。检测到的角点是类型

std::vector<std::vector<Point2f>.

(所以翻译成 python 数组的数组,包含 4 个点,每个点由 2 个坐标组成。)ID 是

std::vector<int>

Python 中的整数列表。

所以我猜Python函数“matchImagePoints”得到了它想要的!

标记检测似乎成功。我已经尝试更改角数组: detectorMarkers 方法返回一个元组。我使用以下代码创建了所需的形状数组 (X, 4, 2)。 (X 是检测到的标记的数量。每个标记有 4 个角,有 2 个坐标 x 和 y。)

marker_corners = np.array(marker_corners)
marker_corners = np.squeeze(marker_corners)

所以我有以下内容:

marker_corners = [
[[8812. 5445.]
[8830. 5923.]
[8344. 5932.]
[8324. 5452.]],

[[7172. 5469.]
[7184. 5947.]
[6695. 5949.]
[6687. 5476.]],

[[3896. 5481.]
[3885. 5952.]
[3396. 5951.]
[3406. 5483.]],
...
]

marker_ids = [
[11],
[27],
[19],
...
]

两者都通过了我收到的原始退货

detector.detectMarkers

进入函数并传递我修改后的数组失败。 (也不使用挤压并输入 (X, 1, 4, 2) 数组失败!)

我已经无法再继续了。

最小工作示例:

使用这张图片: Charuco board 11x8

import cv2 as cv
import numpy as np
    
image = cv.imread("charuco_board.png")
im_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
charuco_marker_dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_250)
charuco_board = cv.aruco.CharucoBoard(
size=(11, 8),
squareLength=500,
markerLength=300,
dictionary=charuco_marker_dictionary
)

# Initial method of this question:
params = cv.aruco.DetectorParameters()
detector = cv.aruco.ArucoDetector(charuco_marker_dictionary, params)
marker_corners, marker_ids, rejected_candidates = detector.detectMarkers(im_gray)
marker_corners = np.array(marker_corners)
marker_corners = np.squeeze(marker_corners)

    # Using cv.aruco.CharucoDetector as pointed out in the comments.
    detector_charuco = cv.aruco.CharucoDetector(charuco_board)
    result = detector_charuco.detectBoard(im_gray)
    marker_corners_charuco, marker_ids_charuco = result[2:]

    # Compare the two results
    assert (marker_ids == marker_ids_charuco).all()  # Detected ID's are identical.
    # assert (marker_corners == marker_corners_charuco).all()  # There seems to be a difference.
    print(marker_corners[0:2], marker_corners_charuco[0:2])  # They seem to be in a different order.

# Proof of the different order statement:
def reshape_and_sort(array):
    array_reshaped = array.copy().reshape(-1, 2)
    return np.array(sorted(array_reshaped, key=lambda x: x[0]**2 + x[1]**2))  # Using geometric distance between each point and the point (0, 0). Leaving out the square.

marker_corners_reshaped = reshape_and_sort(marker_corners)
marker_corners_reshaped_charuco = reshape_and_sort(np.array(marker_corners_charuco))
assert (marker_corners_reshaped == marker_corners_reshaped_charuco).all()


# Trying with new resutls:  # Still fails!
try:
    object_points_t, image_points_t = charuco_board.matchImagePoints(
        marker_corners_charuco,
        marker_ids_charuco
    )
except cv.error as err:
    print(err)
python opencv computer-vision aruco
1个回答
0
投票

结果你需要安装

pip install opencv-contrib-python
不需要安装
pip install opencv-python
。最好确保您将其安装在新环境中,而没有任何旧安装。

这里将提供以下两个基本功能:

cv.aruco.interpolateCornersCharuco
cv.aruco.calibrateCameraCharuco

我没有设法仅使用非 cotrib 版本的“新”objdetcet 模块使其工作。 如需更深入的解释,请参阅此处

© www.soinside.com 2019 - 2024. All rights reserved.