How to find intersection between a masked polygon and a line drawn using OpenCV?

enter image description here

Here is what I have got so far. I have tried to find the intersection between a masked polygon and the line drawn using OpenCV.

import cv2 
from ultralytics import YOLO
import numpy as np
from shapely.geometry import Polygon, LineString, Point
# import imutils
model = YOLO('best (5) (1).pt')
video_path = "21-09-17-12-39-53.mp4"
cap = cv2.VideoCapture(video_path)
isClosed = True
color = (255, 255, 255)
thickness = 1
pts = np.array([[0, 130], [320,130]],np.int32)


while cap.isOpened():
    success, frame = cap.read()
    if success:
        results = model(frame)
        annotated_frame = results[0].plot()
        res = results[0]
        masks = res.masks
        mask1 = masks[0]
        mask = mask1.data[0].numpy()
        polygon = mask1.xy[0]
        polygon = polygon.reshape((-1, 1, 2))
        # polygon = np.ndarray.tolist(polygon)
        print(type(polygon))
        image = cv2.polylines(frame, np.int32([polygon]), isClosed, color, thickness)
        image = np.zeros(frame.shape,np.uint8)
        #cv2.fillPoly(image,np.int32([polygon]), color)
        cv2.polylines(image, np.int32([polygon]), isClosed, color, thickness)
        l1 = cv2.polylines(image, [pts],isClosed, (0,255,0), thickness)
        l2 = cv2.polylines(frame,[pts],isClosed, (0,255,0), thickness)
        cv2.imshow("YOLOv8 Inference", frame)
        cv2.imshow("YOLOv8 Inference1", image)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()`

Leave a Comment