Finding the Corner points from the mask image


I want to find the marked point in the image from this polygon region. I have fitted a ellipse and done

import cv2
import numpy as np

# Load the binary mask image
# Make sure 'image_2fd641.png' is in the same directory as the script
try:
    image = cv2.imread('/content/Output_mask/group_02.png')
    if image is None:
        raise FileNotFoundError("Image file not found or could not be read.")
except FileNotFoundError as e:
    print(e)
    exit()

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Find contours in the image
# cv2.RETR_EXTERNAL retrieves only the extreme outer contours
# cv2.CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments
# and leaves only their end points.
contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Check if any contours were found
if contours:
    # Assuming the largest contour corresponds to the object of interest
    largest_contour = max(contours, key=cv2.contourArea)

    # Ensure the contour has at least 5 points to fit an ellipse
    if len(largest_contour) >= 5:
        # Fit an ellipse to the largest contour
        # The function returns a rotated rectangle in which the ellipse is inscribed.
        # It's a tuple: ((center_x, center_y), (minor_axis, major_axis), angle)
        rotated_rect = cv2.fitEllipse(largest_contour)

        # Create a copy of the original image to draw the result on
        output_image = image.copy()

        # Draw the original contour in blue
        cv2.drawContours(output_image, [largest_contour], -1, (255, 0, 0), 2)

        # Draw the fitted ellipse (represented by the rotated rectangle) in red
        cv2.ellipse(output_image, rotated_rect, (0, 0, 255), 2)

        # Print the parameters of the rotated rectangle
        (center_x, center_y), (minor_axis, major_axis), angle = rotated_rect
        print("Ellipse Parameters:")
        print(f"  Center: ({center_x:.2f}, {center_y:.2f})")
        print(f"  Size (Minor, Major Axis): ({minor_axis:.2f}, {major_axis:.2f})")
        print(f"  Angle of Rotation: {angle:.2f} degrees")


        # Display the result
        cv2_imshow(output_image)
    else:
        print("Contour found, but it has fewer than 5 points, so an ellipse cannot be fitted.")
else:
    print("No contours found in the image.")