Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 938
Nicolas Gervais Avatar asked Feb 28 '20 17:02

Nicolas Gervais


People also ask

How do you fill a rectangle in OpenCV?

To fill the rectangle we use the thickness = -1 in the cv2. rectangle() function.

How do I draw a rectangle on cv2 image?

Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Define the starting coordinates. Step 5: Define the ending coordinates.

How to draw a rectangle in OpenCV Python?

Using Rectangular shape. Using Line. and, Using Circle. Note: The concept will be the same for any other shape or image. To draw a rectangle in Opencv Python. Syntax: cv2.rectangle (image, start_point, end_point, color, thickness) image: It is the image on which line is to be drawn.

How do I construct transparent overlays With OpenCV?

Today’s tip comes from my bag of experiences: constructing transparent overlays with OpenCV. In order to construct a transparent overlay, you need two images: Your original image. An image containing what you want to “overlay” on top of the first using some level of alpha transparency.

How do I use image masking using OpenCV?

Our opencv_masking.py script will load the input adrian.png image from disk. We’ll then use masking to extract both the body and face from the image using rectangular and circular masks, respectively. Let’s learn how to apply image masking using OpenCV! Open the opencv_masking.py file in your project directory structure, and let’s get to work:

Is the text in the rectangle transparent or opaque?

The results of drawing the rectangle and text can be seen below: Figure 2: Drawing a rectangle and text on the original image. However, both the text and bounding box are entirely opaque — no transparency has been applied. However, notice that both the rectangle and text are fully opaque!


2 Answers

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

like image 74
Quang Hoang Avatar answered Oct 23 '22 13:10

Quang Hoang


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:

enter image description here

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:

enter image description here

Result:

enter image description here

like image 1
fmw42 Avatar answered Oct 23 '22 13:10

fmw42