Reputation: 404
I have an image and I am trying to black out all the area that is outside the circle with opencv.
Upvotes: 3
Views: 5320
Reputation: 53081
Here is one way in Python/OpenCV.
Input:
import cv2
import numpy as np
# read image
img = cv2.imread('jeep.jpg')
hh, ww = img.shape[:2]
hh2 = hh // 2
ww2 = ww // 2
# define circles
radius = hh2
yc = hh2
xc = ww2
# draw filled circle in white on black background as mask
mask = np.zeros_like(img)
mask = cv2.circle(mask, (xc,yc), radius, (255,255,255), -1)
# apply mask to image
result = cv2.bitwise_and(img, mask)
# save results
cv2.imwrite('jeep_mask.png', mask)
cv2.imwrite('jeep_masked.png', result)
cv2.imshow('image', img)
cv2.imshow('mask', mask)
cv2.imshow('masked image', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result Image:
Upvotes: 7