2017-03-11 62 views
0

所以基本上我有一個彩色的RGB圖像,我想在RGB圖像上添加彩色覆蓋而不將其轉換爲灰度級。 例如,如果我有一個彩色圖像(RGB)。我想通過索引添加一個透明的藍色像這樣在Skimage上使用一些透明色在彩色RGB圖像上應用彩色覆蓋圖

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

這個問題是一個兄弟的問題這一個: Applying a coloured overlay to an image in either PIL or Imagemagik 區別是色彩空間。上面的問題是針對灰度級圖像而非彩色的(RGB)。

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

請用上面的代碼來回答。

+0

請先選擇要添加疊加層的區域。然後將顏色添加到該區域 –

+0

@JeruLuke問題在於顏色不透明。我也需要看看背景。所以在疊加中應該有一個合適的alpha通道。 –

+0

我在OpenCV中有確切的解決方案。你想看看嗎? –

回答

1

這裏是在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() 

一些結果:

當阿爾法= 0.1

enter image description here

當阿爾法= 0.5

enter image description here

當阿爾法= 0.8

enter image description here

當阿爾法= 1.0(覆蓋不再透明的,但不透明)

enter image description here

+1

這正是我所期待的。謝謝 :) –