ComputerVision/OpenCV Notes/Notes/Basics/masking.py
2021-09-26 16:24:06 -04:00

21 lines
638 B
Python

import cv2 as cv
import numpy as np
img = cv.imread('Photos/cats2.jpg')
cv.imshow('Cats', img)
blank = np.zeros(img.shape[:2], dtype='uint8') # the dimensions of the mask have to be the same size as that of the image
cv.imshow('Blank Image', blank)
circle = cv.circle(blank.copy(), (img.shape[1]//2 + 45,img.shape[0]//2), 100, 255, -1)
# cv.imshow('Mask', mask)
rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1)
weird_shape = cv.bitwise_and(circle, rectangle)
cv.imshow('Weird Shape', weird_shape)
masked = cv.bitwise_and(img, img, mask = weird_shape)
cv.imshow('Weird Shaped Masked Image', masked)
cv.waitKey(0)