gpt4 book ai didi

python - OpenCV Python - 设置背景颜色

转载 作者:太空狗 更新时间:2023-10-30 01:50:58 25 4
gpt4 key购买 nike

我正在尝试从照片中删除灰色背景并将其替换为白色

到目前为止我有这段代码:

image = cv2.imread(args["image"])
r = 150.0 / image.shape[1]
dim = (150, int(image.shape[0] * r))
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
lower_white = np.array([220, 220, 220], dtype=np.uint8)
upper_white = np.array([255, 255, 255], dtype=np.uint8)
mask = cv2.inRange(resized, lower_white, upper_white) # could also use threshold
res = cv2.bitwise_not(resized, resized, mask)
cv2.imshow('res', res) # gives black background

问题是图像现在有黑色背景,因为我已经屏蔽掉了灰色。如何用白色像素替换空白像素?

Before After

最佳答案

您可以使用 mask 来索引数组,并仅将 mask 的白色部分分配给白色:

coloured = resized.copy()
coloured[mask == 255] = (255, 255, 255)

Screenshot

关于python - OpenCV Python - 设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com