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
I think this is what you are looking for:
The portion in red is the overlay.
I was able to do this using OpenCV, with a little help from THIS POST i read a few months back.
This blogpost covers many other interesting stuff in image processing. Cheers!!
来源:https://stackoverflow.com/questions/41376337/applying-overlays-to-image-with-varying-transparency