雖然我的第一次經歷(很好的一次)是PIL,但我通常會爲圖像處理任務旋轉爲numpy/scipy。因此,我不確定下面是否會滿足您的需求。
給出一個特定的像素1,其中alpha1來自image1,pixel2來自alpha2,來自image2的outputPixel將如下所示。
alpha1>=alpha2 then outputPixel = (alpha1-alpha2)*pixel1 + alpha2*pixel2
alpha1==alpha2 then outputPixel = 0*pixel1 + alpha2*pixel2 note in this case alpha1-alpha2 equals 0
alpha1<alpha2 then outputPixel = 0*pixel1 + alpha2*pixel2
使用上述定義,我們將基本上計算從 的貢獻對每個像素的第一圖像,然後將其α映射後添加此第二圖像
我們可以直接從imshow作爲獲得此以及
r1 = scipy.misc.imread('red.png')
b1 = scipy.misc.imread('blue.png')
r1 = r1.astype(numpy.float32)
b1 = b1.astype(numpy.float32)
alpha1 = r1[:,:,3]
alpha2 = b1[:,:,3]
#scale the alpha mattes to keep resulting alpha'd images in display range
alpha1Scaled = alpha1/255
alpha2Scaled = alpha2/255
diff1 = alpha1Scaled - alpha2Scaled
i1 = r1[:,:,0:3]
i2 = b1[:,:,0:3]
#create the alpha mapped images
d1 = numpy.zeros(i1.shape)
d2 = numpy.zeros(i2.shape)
for z in range(3):
d1[:,:,z] =(diff1>=0)*diff1*i1[:,:,z]
d2[:,:,z] = i2[:,:,z]*alpha2Scaled
#belend the result
result = d1 + d2
#rescale in case of overflow
resultScaled = 255*(result/result.max())
#to display
imshow((resultScaled ).astype(uint8))
show()
#note the below gives us the same result
figure()
imshow(red)
imshow(blue)
show()
感謝您的答案保羅,幾乎是我後,儘管我似乎得到的黑色背景。我已經發布了一個鏈接到下面的alpha複合算法似乎得到了我想要以稍好的結果去。 – DizzyDoo