1 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
正如評(píng)論中提到的,在這種情況下,簡(jiǎn)單地移除 alpha 通道不會(huì)移除背景,因?yàn)?BGR 通道具有您要移除的偽像,如下所示,當(dāng)您只繪制 B、G 或 R 通道時(shí)。
你的 alpha 通道看起來像這樣
為了實(shí)現(xiàn)您的需求,您需要應(yīng)用一些矩陣數(shù)學(xué)來獲得結(jié)果。我在這里附上了代碼
import cv2
import matplotlib.pyplot as plt
img_path = r"path/to/image"
#saving image into a white bg
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
plt.imshow(img)
plt.show()
b,g,r, a = cv2.split(img)
print(img.shape)
new_img = cv2.merge((b, g, r))
not_a = cv2.bitwise_not(a)
not_a = cv2.cvtColor(not_a, cv2.COLOR_GRAY2BGR)
plt.imshow(not_a)
plt.show()
new_img = cv2.bitwise_and(new_img,new_img,mask = a)
new_img = cv2.add(new_img, not_a)
cv2.imwrite(output_dir, new_img)
plt.imshow(new_img)
print(new_img.shape)
plt.show()
結(jié)果是具有尺寸的圖像 (1200, 1200, 3)
添加回答
舉報(bào)