Мне удалось обнаружить все маркеры с помощью кода cv2:< /p>
Код: Выделить всё
corners, ids = self.detect_aruco_markers(image)
Однако есть проблема, связанная с тем, что фотографии не сделаны идеально сверху вниз. Я прикрепил пример изображения ниже. Проблема в том, что в полученной системе координат маркеры слегка повернуты и сдвинуты с того места, где они должны быть. Например, маркеры, которые должны быть выровнены по оси X или Y, отсутствуют в результирующей системе координат.
Чтобы решить эту проблему, я хотел использовать перспективное преобразование с использованием матрицы гомографии. Однако у меня возникли проблемы с решением, какие именно точки использовать в качестве опорных точек «реального мира» и исходных точек для метода getPerspectiveTransform cv2.
Я пробовал что-то, используя выпуклую оболочку, но безрезультатно. :
Код: Выделить всё
# Flatten the list of corners and convert to a NumPy array
src_points = np.concatenate(corners, axis=0).reshape(-1, 2)
# Compute the bounding box of the detected markers
x_min, y_min = np.min(src_points, axis=0)
x_max, y_max = np.max(src_points, axis=0)
# Define destination points for the perspective transform
dst_points = np.array([
[0, 0],
[x_max - x_min, 0],
[x_max - x_min, y_max - y_min],
[0, y_max - y_min]
], dtype=np.float32)
# Ensure src_points has exactly four points by selecting the outermost corners
if src_points.shape[0] > 4:
# Compute the convex hull to find the outermost corners
hull = cv2.convexHull(src_points)
if hull.shape[0] > 4:
# Approximate the hull to a quadrilateral
epsilon = 0.02 * cv2.arcLength(hull, True)
approx = cv2.approxPolyDP(hull, epsilon, True)
if approx.shape[0] == 4:
src_points = approx.reshape(4, 2)
else:
rect = cv2.minAreaRect(hull)
src_points = cv2.boxPoints(rect)
else:
src_points = hull.reshape(-1, 2)
elif src_points.shape[0] < 4:
raise ValueError("Not enough points to compute perspective transform.")
# Compute the perspective transform matrix
matrix = cv2.getPerspectiveTransform(src_points.astype(np.float32), dst_points)
# Warp the image using the transformation matrix
width = int(x_max - x_min)
height = int(y_max - y_min)
warped_image = cv2.warpPerspective(image, matrix, (width, height))

Подробнее здесь: https://stackoverflow.com/questions/793 ... co-markers