How do I make an inverse filled transparent rectangle with OpenCV?
Others
2022-04-20 12:49:47
views: 0
Nicolas Gervais :
I want to make an inverse filled rectangle in this picture.
The code I have:
import cv2
lena = cv2.imread('lena.png')
output = lena.copy()
cv2.rectangle(lena, (100, 100), (200, 200), (0, 0, 255), -1)
cv2.addWeighted(lena, 0.5, output, 1 - .5, 0, output)
cv2.imshow('', output)
What I want:
Quang Hoang :
Here's what I would do:
# initialize output
output = np.zeros_like(lena, dtype=np.uint8)
output[:,:,-1] = 255
# this is your box top_x
tx,ly,bx,ry = 100,100,200,200
# copy lena to output
output[tx:bx,ly:ry] = lena[tx:bx,ly:ry]
cv2.addWeighted(lena, 0.5, output, 1 - .5, 0, output);
OUtput:
Origin http://43.154.161.224:23101/article/api/json?id=16472&siteId=1