Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - Applying overlays to image with varying transparency

I have got a RGB image (224x224x3) and an overlay (224x224). Now I want to apply my overlay as red pixels on my RGB image, which I make therefore to grayscale. The overlay range from 0 to 255. Higher values should make a stronger red.
I tried to use Stefan's tutorial, but I could not adapt it: tutorial.
Here is my code:

# RGB input, shape (224x224x3)
img = self.inputImage
# make to grayscale 
img = np.average(img, axis=2)
rows, cols = img.shape[0], img.shape[1]

color_mask = np.zeros((rows, cols, 3))

# convert to uint8 to plot in QImage::Format_RGB888
img = img.astype(np.uint8)
overlay = self.outputImage.astype(np.uint8)
# normalize to range 0 to 1
img = (img*1.0-img.min())/(img.max()-img.min())
overlay = (overlay*1.0 - overlay.min()) / (overlay.max() - overlay.min())

# create a mask, where only the red channels contains values
mask = np.zeros((rows,cols,3))
mask[:,:,0] = mask[:,:,0]+overlay
color_mask = mask

img_color = np.dstack((img, img, img))

# make everysthing to HSV colorspace
img_hsv = color.rgb2hsv(img_color)
color_mask_hsv = color.rgb2hsv(color_mask)
img_hsv[..., 0] = color_mask_hsv[..., 0]
img_hsv[..., 1] = color_mask_hsv[..., 1] * nAlpha

# convert back
img_masked = color.hsv2rgb(img_hsv)
# rescale
ov = img_masked*255
self.mainWindow_images.label_outputImg.setPixmap(
    QPixmap(QImage(ov, ov.shape[1], ov.shape[0], ov.shape[1] * 3, QImage.Format_RGB888)))

The result is just a mainly black picture, which changes a little bit with nAlpha. result_screenshot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...