簡體   English   中英

根據另一個圖像中的像素值設置圖像的 Alpha 通道

[英]Setting the alpha channel of an image, based on the pixel values in another image

我有兩張圖片。 在一個圖像中,所有非 alpha 通道像素都等於 0,我希望 alpha 通道值等於 255,而在另一個相同大小的圖像中,像素不是 0。在這次嘗試中,我m 嘗試基於原始圖像創建一個 4 通道 np 數組,然后使用 np.argwhere 查找像素值非零的位置,然后在新的 np 數組中,根據該值設置 alpha 通道值。

例如,對於輸入圖像中值為 [255, 255, 255] 的每個像素,我希望新圖像中的相應像素為 [0, 0, 0, 255]。 對於輸入圖像中值為 [0, 0, 0] 的每個像素,我希望新圖像中的相應像素為 [0, 0, 0, 0]。

mask_file = cv.imread(r'PlateMask_0001.png', cv.IMREAD_UNCHANGED)

scale_factor = 0.125
w = int(mask_file.shape[1] * scale_factor)
h = int(mask_file.shape[0] * scale_factor)
scaled = cv.resize(mask_file, (w, h))

coords = np.argwhere(scaled > 0)
new_object = np.zeros((120, 160, 4))
new_object[coords, :] = 255
cv.imshow('Mask', mask)
cv.imshow('Scaled', new_object)
cv.waitKey(0)
cv.destroyAllWindows()

這是我在 Stack 上的第一個問題,因此請隨時提出問題格式等方面的改進建議。謝謝。

img1視為您的原始圖像,將img2視為需要修改 alpha 通道的圖像。

在下面, img2的 alpha 通道在img1的坐標 (255, 255, 255) 中包含值 255:

img2[:,:,3][img1 == (255, 255, 255)]  = 255

同樣對於值 0:

img2[:,:,3][img1 == (0, 0, 0)]  = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM