Hello,
I am trying to detect a charuco diamond marker with opencv-contrib version 4.8.1.78. What I see in the documentation is that you first need to detect the aruco markers and then after that you use detectCharucoDiamond(). But when I input the marker_corners and markerIds into the detectCharucoDiamond function I get the error: cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix_wrap.cpp:787: error: (-215:Assertion failed) (flags & FIXED_TYPE) != 0 in function ‘cv::_InputArray::type’
Is there something wrong in the function itself or am I doing something wrong?
My Code:
import cv2
import numpy as np
class CharucoDiamond:
def __init__(self):
self.ARUCO_DICT = {
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
"DICT_5X5_100": cv2.aruco.DICT_5X5_100,
"DICT_5X5_250": cv2.aruco.DICT_5X5_250,
"DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv2.aruco.DICT_6X6_50,
"DICT_6X6_100": cv2.aruco.DICT_6X6_100,
"DICT_6X6_250": cv2.aruco.DICT_6X6_250,
"DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv2.aruco.DICT_7X7_50,
"DICT_7X7_100": cv2.aruco.DICT_7X7_100,
"DICT_7X7_250": cv2.aruco.DICT_7X7_250,
"DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
"DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5,
"DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9,
"DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10,
"DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11,
}
self.CREATED_MARKERS = {}
return
def create_marker(
self,
aruco_ids: list = [0, 1, 2, 3],
square_size: int = 200,
marker_size: int = 120,
margin_size: int = 0,
border_bits: int = 1,
image_name: str = "aruco_marker",
aruco_dictionary: str = "DICT_4X4_50",
):
"""
Function for creating charuco diamond marker
Args:
aruco_ids: The aruco markers used in the charuco diamond.
square_size: Size of one square in the diamond total diamond (3x3).
marker_size: Size of the markers in the diamond.
margin_size: Size of the white boundary around the charuco diamond.
border_bits: Size of the border around the marker.
image_name: Name of the output image.
aruco_dictionary: The dictionary of the aruco markers that are being used.
Returns:
Saves an image of the charuco diamond.
Raises:
Nothing."""
dictionary = cv2.aruco.getPredefinedDictionary(
self.ARUCO_DICT[aruco_dictionary]
)
marker = cv2.aruco.drawCharucoDiamond(
dictionary=dictionary,
ids=np.array(aruco_ids),
squareLength=square_size,
markerLength=marker_size,
marginSize=margin_size,
borderBits=border_bits,
)
self.CREATED_MARKERS[image_name] = {
"marker": marker,
"squareLength": square_size,
"markerLength": marker_size,
"dictionary": dictionary,
}
cv2.imwrite(f"{image_name}.png", marker)
def detection(
self,
image: np.ndarray = np.zeros([5, 5]),
image_name: str = "aruco_marker",
aruco_dictionary: str = "DICT_4X4_50",
square_size: int = 200,
marker_size: int = 120,
):
"""
Function for creating charuco diamond marker
Args:
image: Image that needs to be analyzed.
image_name: Name of the output image.
aruco_dictionary: (optional if marker is created with class) The dictionary of the aruco markers that are being used.
square_size: (optional if marker is created with class) Size of one square in the diamond total diamond (3x3).
marker_size: (optional if marker is created with class) Size of the markers in the diamond.
Returns:
The pixel locations of the diamond corners and the diamond id.
Raises:
Nothing."""
if not bool(self.CREATED_MARKERS):
dictionary = cv2.aruco.getPredefinedDictionary(
self.ARUCO_DICT[aruco_dictionary]
)
marker_corners, marker_ids, rejected = cv2.aruco.detectMarkers(
image, dictionary
)
if marker_ids.size != 0:
diamond_corners, diamond_ids = cv2.aruco.detectCharucoDiamond(
image=image,
markerCorners=marker_corners[0][0],
markerIds=marker_ids[0],
squareMarkerLengthRate=float(square_size / marker_size),
)
else:
marker_dict = self.CREATED_MARKERS.get(image_name)
marker_corners, marker_ids, rejected = cv2.aruco.detectMarkers(
marker_dict.get("marker"), marker_dict.get("dictionary")
)
diamond_corners, diamond_ids = cv2.aruco.detectCharucoDiamond(
image=marker_dict.get("marker"),
markerCorners=marker_corners,
markerIds=marker_ids,
squareMarkerLengthRate=float(
marker_dict.get("squareLength") / marker_dict.get("markerLength")
),
)
return diamond_corners, diamond_ids
charuco_test = CharucoDiamond()
charuco_test.create_marker(image_name="aruco_marker")
# image = cv2.imread("./aruco_marker.png")
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(
charuco_test.detection(
image_name="aruco_marker",
aruco_dictionary="DICT_4X4_50",
square_size=0.4,
marker_size=0.2,
)
)