I have a video stream where I do the detection of people using Opencv and python. My ROI is rectangular, but I would like to make a custom shape as in the figure.
Asked
Active
Viewed 1,164 times
1 Answers
2
It seems this is a stationary camera. If so, you can hard code the rectangular region of interest. You can then use a mask (created with for instance MS Paint) to black out everything outside of the custom shape.
Code:
import cv2
# load image
img = cv2.imread('image.jpg')
# load mask
mask = cv2.imread('roi_mask.png',0)
# create subimage
roi = img[120:350,150:580]
# mask roi
masked_roi = cv2.bitwise_and(roi,roi,mask=mask)
# display result
cv2.imshow('Roi',roi)
cv2.imshow('Mask',mask)
cv2.imshow('Result',masked_roi)
cv2.waitKey(0)
cv2.destroyAllWindows()

J.D.
- 4,511
- 2
- 7
- 20
-
I used the code but it gave error (mtype == CV_8U || mtype == CV_8S). – Márcio Jun 30 '19 at 23:12
-
Did you load the mask as a grayscale? Note the `,0)` in `mask = cv2.imread('roi_mask.png',0)` – J.D. Jul 01 '19 at 06:54