OpenCV:Python等效于C ++中的"setTo" [英] OpenCV : Python equivalent of `setTo` in C++

查看:134
本文介绍了OpenCV:Python等效于C ++中的"setTo"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mat等效的python是什么:: setTo 在C ++中?

我想做的是通过遮罩设置值:

What I'm trying to do is to set value by mask:

Mat img;
...
img.setTo(0, mask);

更新:

这是可能的解决方案:

#set by mask area to zero
img= np.random.rand(200, 200, 3) * 255
img= img.astype(np.uint8)
mask = np.zeros((200, 200), np.uint8)
mask[10:100, 60:140] = 255
inv_mask= cv2.bitwise_not(mask)
n_channels= img.shape[2]
for i in range(0,n_channels):
    img[..., i]= img[..., i] * (inv_mask/255)

#to set arbitary value
img= np.random.rand(200, 200, 3) * 255
img= img.astype(np.uint8)
mask= np.zeros((200,200), np.uint8)
mask[10:100, 60:140]= 255
mask_bool= np.where(mask > 0)
value= 120
img[mask_bool]= value

推荐答案

您可以简单地使用 img [mask>0] = 0 ,它等效于 img.setTo(0,mask);

You can simply use img[mask > 0] = 0 which is the python equivalent of img.setTo(0, mask);

这篇关于OpenCV:Python等效于C ++中的"setTo"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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