Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Actually, I used raspberry pi 3b+. Unfortunately, I didn't used videocapture(0). So you can modified yourself to suit your needed.

import cv2
import numpy as np

left_click = np.empty((0,2), np.uint32)
left_click_bool = False
right_click = np.empty((0,2), np.uint32)

lpnts = np.empty((0,2), np.uint32)
rpnts = np.empty((0,2), np.uint32)

# mouse callback function
def get_points(event,x,y,flags,param):
    global lpnts,rpnts
    if event == cv2.EVENT_LBUTTONDOWN:
        lpnts = np.append(lpnts, np.array([[x, y]]), axis=0)
        cv2.polylines(img, [lpnts], False, (0, 0, 255))

    if event == cv2.EVENT_RBUTTONDOWN:
        rpnts = np.append(rpnts, np.array([[x, y]]), axis=0)
        cv2.polylines(img, [rpnts], False, (255, 0, 0))

img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image', get_points)

while True:
    cv2.imshow('image', img)
    k = cv2.waitKey(1) 
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break