我有一个RGB图像(224x224x3)和一个覆盖图(224x224)。 现在我想将我的叠加层应用为我的RGB图像上的红色像素,因此我将其设置为灰度。覆盖范围从0到255.值越高,红色越强 我尝试使用Stefan的教程,但我无法适应它:tutorial 这是我的代码:
# 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)))
结果只是一张黑色的照片,用nAlpha改变了一点。 result_screenshot
答案 0 :(得分:2)