OpenCV Python-设置背景颜色 [英] OpenCV Python - Set background colour

查看:1074
本文介绍了OpenCV Python-设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从照片中删除灰色背景,并用白色背景代替

I am trying to remove a greyish background from a photo and replace it with a white one

到目前为止,我有以下代码:

so far I have this code:

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

问题在于,当我遮住灰色时,图像现在具有黑色背景.如何用白色替换空白像素?

The problem is that the image now has a black background as I have masked out the grey. How can I replace the empty pixels with white ones?

推荐答案

您可以使用遮罩为数组建立索引,并将遮罩的白色部分仅分配给白色:

You can use the mask to index the array, and assign just the white parts of the mask to white:

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

这篇关于OpenCV Python-设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆