Wednesday 10 March 2021

opencv 9 mouse event

click on video, will create region of interest around mouse location

zoomed in image of region of interest is displayed
#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Santa Barbara.mp4")
roi_w = 50

def click_event(event, x, y, flags, param):
    if event ==cv2.EVENT_LBUTTONDOWN:
        cv2.rectangle(param, (x-roi_w-2, y-roi_w-2), (x+roi_w+2, y+roi_w+2), (0, 0, 255), 2)
        cv2.imshow('original', param)

        roi = param[y-roi_w:y+roi_w, x-roi_w:x+roi_w]
        zoom_img = cv2.resize(roi, (0, 0), fx=5, fy=5)
        cv2.imshow('zoom in', zoom_img)

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

    cv2.setMouseCallback('frame', click_event, frame)

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):
        break

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

setMouseCallback param

pause video

No comments:

Post a Comment