如何使用 OpenCV 制作反向填充的透明矩形?

Nic*_*ais 5 python opencv image image-processing

我想在这张图片中制作一个反向填充的矩形。

在此处输入图片说明

我有的代码:

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想要的是:

在此处输入图片说明

Qua*_*ang 4

这就是我要做的:

# 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);
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述