Creating a simple Region of Interest (ROI) inside a video streaming using OpenCV in Python

Michel Fernandes
BeyondLabsEY
Published in
2 min readJul 23, 2018

Regions of interests help to mix and add a window for several tasks, such as transformations, virtual pen, object detection, etc.

This simple example shows how to include a rectangle inside a image, like a window for specific transformations.

In that particular example we used a sketch frame transformation applying Canny edge detector and image inversion.

First, import the libraries.

import cv2
from matplotlib import pyplot as plt
def sketch_transform(image):
image_grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_grayscale_blurred = cv2.GaussianBlur(image_grayscale, (7,7), 0)
image_canny = cv2.Canny(image_grayscale_blurred, 10, 80)
_, mask = image_canny_inverted = cv2.threshold(image_canny, 30, 255, cv2.THRESH_BINARY_INV)
return mask

The function sketch_transform will transform the image into sketch like, applying Canny edge detector and imagem inversion.

The next step is to use the Video Capture of OpenCV, which is quite simple.

Initially, we get the webcam using VideoCapture(0). Multiple webcams can be selected changing the number in the constructor.

After that, using a loop, we can get each frame of webcam with the method read().

cam_capture = cv2.VideoCapture(0)
cv2.destroyAllWindows()
upper_left = (50, 50)
bottom_right = (300, 300)
while True:
_, image_frame = cam_capture.read()

#Rectangle marker
r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5)
rect_img = image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]]

sketcher_rect = rect_img
sketcher_rect = sketch_transform(sketcher_rect)

#Conversion for 3 channels to put back on original image (streaming)
sketcher_rect_rgb = cv2.cvtColor(sketcher_rect, cv2.COLOR_GRAY2RGB)

#Replacing the sketched image on Region of Interest
image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]] = sketcher_rect_rgb
cv2.imshow("Sketcher ROI", image_frame)
if cv2.waitKey(1) == 13:
break

cam_capture.release()
cv2.destroyAllWindows()

At the end, let's release the webcam and clear all remaining window that could be opened.

Here is the result.

In further studies we can use the same code to explore additional features and give on-line feedback, specially for object detection.

--

--

Michel Fernandes
BeyondLabsEY

Solution Architect at EY and Teacher at FIAP. Helping everyone in journey to the cloud and practice of machine learning and visual computing.