Perspective correction with OpenCV - Image is cropped to reference marker

140 Views Asked by At

I'm trying a simple perspective correction script with Aruo markers, The script is correctly detecting the Aruco markers and fixes the perspective, But when it outputs the result, The image is cropped to a selected marker, But I need to apply this perspective transformation to the whole image not into a single marker.

The image I attached (untitled7.png) is the input image Results.png shows what the attached script output now

untitled7.png Result.png

At the end of the day, I need to correct the perspective of the input image so it looks like it is taken from directly above the marker grid

This is my working code now with opencv-contrib-python

import cv2
import numpy as np


def detect_aruco_and_correct_perspective(image_path):
    # Load the image
    image = cv2.imread(image_path)

    # Define the ArUco dictionary with the specified parameters (4x4, 250)
    aruco_dictionary = cv2.aruco.getPredefinedDictionary(
        cv2.aruco.DICT_4X4_250)

    # Create ArUco parameters for detection
    aruco_parameters = cv2.aruco.DetectorParameters()

    # Detect ArUco markers in the image
    corners, ids, rejected = cv2.aruco.detectMarkers(
        image, aruco_dictionary, parameters=aruco_parameters)

    # Check if any markers were detected
    if corners:
        # Assume the first detected marker is our point of interest
        corners = corners[0].reshape((4, 2))

        # Sort the corners to top-left, top-right, bottom-right, bottom-left
        top_left = min(corners, key=lambda x: x[0] + x[1])
        top_right = max(corners, key=lambda x: x[0] - x[1])
        bottom_right = max(corners, key=lambda x: x[0] + x[1])
        bottom_left = min(corners, key=lambda x: x[0] - x[1])
        sorted_corners = np.array(
            [top_left, top_right, bottom_right, bottom_left], dtype="float32")

        # Define the size of the new image
        width, height = 500, 500
        destination_corners = np.array(
            [[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]], dtype="float32")

        # Get the perspective transform matrix
        matrix = cv2.getPerspectiveTransform(
            sorted_corners, destination_corners)

        # Apply the perspective transformation
        transformed_image = cv2.warpPerspective(image, matrix, (width, height))

        return transformed_image
    else:
        return None

transformed_img = detect_aruco_and_correct_perspective(
    'Impacto24_OpenCV-Testings\\v1\\venv\\SampleData\\untitled7.png')
if transformed_img is not None:
    cv2.imshow('Transformed Image', transformed_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print('No ArUco markers detected in the image')

Any Help is appreciated. Thank you.

0

There are 0 best solutions below