0

So basically I have a colored RGB image and I want to add a colored overlay over the RGB image without converting it to gray level. For example if I have a colored image(RGB). And I want to add a transparent blue color over the index like this

img[200:350, 200:350] = [0, 0, 1] # Blue block

This question is a sibling question to this one: Applying a coloured overlay to an image in either PIL or Imagemagik Difference is the color space. The above question is for gray level images rather colored (RGB).

from skimage import io, data
import numpy as np
img = data.astronaut()

Please use the above code to answer.

Community
  • 1
  • 1
Imran Salam
  • 145
  • 1
  • 10

1 Answers1

3

Here is the code in OpenCV:

import cv2

# load the image
image = cv2.imread("2.jpg")

# I resized the images because they were to big
image = cv2.resize(image, (0,0), fx=0.75, fy=0.75)

overlay = image.copy()
output = image.copy()

#select the region that has to be overlaid
cv2.rectangle(overlay, (420, 205), (595, 385),(0, 255, 255), -1)

#Adding the transparency parameter
alpha = 1

#Performing image overlay
cv2.addWeighted(overlay, alpha, output, 1 - alpha,0, output)

#Save the overlaid image
cv2.imwrite('Output'+str(alpha) +'.jpg', output)

cv2.waitKey(0)
cv2.destroyAllWindows()

Some results:

when alpha = 0.1

enter image description here

when alpha = 0.5

enter image description here

when alpha = 0.8

enter image description here

when alpha = 1.0 (the overlay is no longer transparent but opaque)

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87