How do I draw a polygon with my mouse in order to create zones which can be processed by cv2?

Viewed 38

I am working on a project where I need to draw polygons with my mouse.

Python version 3.9, running it on pycharm on windows 10.

I have been told that the tutorial I was following may just be bad code so I am rephrasing my question.

How do I draw a polygon with my mouse in order to create zones which can be processed by cv2?

Here is my current "zone" code.

import cv2
import pickle

width, height = 107, 48

try:
    with open('CarParkPos', 'rb') as f:
        posList = pickle.load(f)
except:
    posList = []


def mouseClick(events, x, y, flags, params):
    if events == cv2.EVENT_LBUTTONDOWN:
        posList.append((x, y))
    if events == cv2.EVENT_RBUTTONDOWN:
        for i, pos in enumerate(posList):
            x1, y1 = pos
            if x1 < x < x1 + width and y1 < y < y1 + height:
                posList.pop(i)

    with open('CarParkPos', 'wb') as f:
        pickle.dump(posList, f)


while True:
    img = cv2.imread('carParkImg.png')
    for pos in posList:
        cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), (255, 0, 255), 2)

    cv2.imshow("Image", img)
    cv2.setMouseCallback("Image", mouseClick)
    cv2.waitKey(1)

From this video

I haven't been able to figure out how to draw polygons from any videos or documentation I have seen.

This is the end result I want picture of carpark

From this video

0 Answers
Related