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

一笑奈何 提交于 2020-04-07 03:47:46

问题


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:


回答1:


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:




回答2:


Here is another way to do it in Python/OpenCV. Though it is not as elegant as the solution from Quang Hoang.

  • Read the input
  • Create a red image of the same size
  • Blend the red image with the input
  • Create a white image with a black rectangle for the "hole"
  • Combine the blended image and the original image using the mask
  • Save the result

Input:

import cv2
import numpy as np

# read image
img = cv2.imread('lena.jpg')

# create red image
red = np.full_like(img,(0,0,255))

# add red to img and save as new image
blend = 0.5
img_red = cv2.addWeighted(img, blend, red, 1-blend, 0)

# create white image for mask base
mask = np.full_like(img, (1,1,1), dtype=np.float32)

# define rectangle for "hole" and draw as black filled on the white base mask
x1,y1,x2,y2 = 100,100,200,200
mask = cv2.rectangle(mask, (x1, y1), (x2, y2), (0, 0, 0), -1)

# combine img and img_red using mask
result = cv2.add(img*(1-mask),img_red*mask).astype(np.uint8)

cv2.imshow('img', img)
cv2.imshow('red', red)
cv2.imshow('img_red', img_red)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('lena_hole_mask.jpg', (255*mask).astype(np.uint8))
cv2.imwrite('lena_plus_red.jpg', result)


Mask:

Result:



来源:https://stackoverflow.com/questions/60456616/how-do-i-make-an-inverse-filled-transparent-rectangle-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!