Drawing ROI in the Video

asked 2018-05-16 06:21:31 -0500

KaanCidar gravatar image

updated 2018-12-10 09:27:06 -0500

Currently I have a code to draw rectangles on the images but i want to be able to draw a rectangle on an image and use it as ROI. Here's the code I use.

import argparse
import cv2

refPt = []
cropping = False

def click_and_crop(event, x, y, flags, param):

    global refPt, cropping

        if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x, y)]
        cropping = True

        elif event == cv2.EVENT_LBUTTONUP:
               refPt.append((x, y))
        cropping = False

                cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
        cv2.imshow("image", image)

image = cv2.imread('/home/kaan/Downloads/a.jpg')
clone = image.copy()
cv2.setMouseCallback("image", click_and_crop)

while True:
    # display the image and wait for a keypress
    cv2.imshow("image", image)
    key = cv2.waitKey(1) & 0xFF

    # if the 'r' key is pressed, reset the cropping region
    if key == ord("r"):
        image = clone.copy()

    # if the 'c' key is pressed, break from the loop
    elif key == ord("c"):

if len(refPt) == 2:
    roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
    cv2.imshow("ROI", roi)

# close all open windows
edit retag flag offensive close merge delete