2017-08-31 105 views
1

兩個圖像我有一個看起來像這樣的圖像:屏蔽,然後粘貼在Python

enter image description here

我想要的背景爲白色和所有其他的像素爲黑色,這樣將我的圖像看起來喜歡這樣的:

enter image description here

假設原始圖像是img和上面的結果是mask。當我嘗試從原始圖像中獲取mask時,事情並不按預期工作。我這樣做:

mask = np.ones_like(img)*255 
mask[img > 0] = 0 

理想情況下,我應該得到預期的結果,但這是我所得到的。

enter image description here

另外,我還有一個圖像,看起來像這樣:

enter image description here

我想粘貼此最後一次日落圖像上預期的面具。我如何使用numpy/scipy/PIL/skimage來做到這一點?

+1

看起來僅使用一個顏色通道的原始的顏色中的一種,所以例如用於RGB彩色像'(0,45,0)''成爲(255 ,0,255)' - 明亮的紫色。 –

回答

2

因爲我們希望得到什麼,這不是黑色img被設置爲mask零,只是看沿三個通道ANY(去年軸)和使用布爾數組用於掩蓋到mask -

mask[(img>0).any(-1)] = 0 

輸出,用於給定的樣品#1 -

enter image description here

爲了與日落圖像img2混合它 -

from scipy.misc import imresize 

mask_resized = imresize(mask, size=img2.shape) 
out = (mask_resized==255)*img2 

輸出 -

enter image description here

+0

第二部分呢?如何在日落圖像上粘貼這個遮罩? – mlRocks

+0

另外,'any()'中的'-1'表示什麼? – mlRocks

+0

@mlRocks你想使用自行車圖像的面具到第二個日落圖像? – Divakar