How do I make an inverse filled transparent rectangle with OpenCV?

Nicolas Gervais :

I want to make an inverse filled rectangle in this picture.

enter image description here

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)

enter image description here

What I want:

enter image description here

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:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16472&siteId=1