Interacting with a live video stream
Let's see how we can use the mouse to interact with a live video stream from the webcam. We can use the mouse to select a region, and then apply a negative film effect on that region, as shown in the following image:
In the following program, we will capture the video stream from the webcam, select a region of interest with the mouse, and then apply this effect:
import cv2 import numpy as np def update_pts(params, x, y): global x_init, y_init params["top_left_pt"] = (min(x_init, x), min(y_init, y)) params["bottom_right_pt"] = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] def draw_rectangle(event, x, y, flags, params): global x_init, y_init, drawing # First click initialize the init rectangle point if event == cv2.EVENT_LBUTTONDOWN: drawing = True x_init, y_init = x, y # Meanwhile mouse button is pressed, update diagonal rectangle point elif event ==...