python怎么调背景颜色_OpenCV Python-设置背景颜色

我真的建议你坚持使用OpenCV,它是很好的优化。诀窍是反转遮罩并将其应用于某个背景,您将得到您的遮罩图像和遮罩背景,然后将两者结合起来。image1是用原始遮罩遮罩的图像,image2是用反转遮罩遮罩的背景图像,image3是组合图像。很重要。image1、image2和image3的大小和类型必须相同。掩码必须为灰度。

LH4wW.jpgimport cv2

import numpy as np

# opencv loads the image in BGR, convert it to RGB

img = cv2.cvtColor(cv2.imread('E:\\FOTOS\\opencv\\zAJLd.jpg'),

cv2.COLOR_BGR2RGB)

lower_white = np.array([220, 220, 220], dtype=np.uint8)

upper_white = np.array([255, 255, 255], dtype=np.uint8)

mask = cv2.inRange(img, lower_white, upper_white) # could also use threshold

mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))) # "erase" the small white points in the resulting mask

mask = cv2.bitwise_not(mask) # invert mask

# load background (could be an image too)

bk = np.full(img.shape, 255, dtype=np.uint8) # white bk

# get masked foreground

fg_masked = cv2.bitwise_and(img, img, mask=mask)

# get masked background, mask must be inverted

mask = cv2.bitwise_not(mask)

bk_masked = cv2.bitwise_and(bk, bk, mask=mask)

# combine masked foreground and masked background

final = cv2.bitwise_or(fg_masked, bk_masked)

mask = cv2.bitwise_not(mask) # revert mask to original

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值