本文介绍了用原始图像opencv Python替换蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将使用蒙版找到的对象替换为原始图像像素.我有一个遮罩,在未检测到物体的情况下显示黑色,而在检测到时显示白色.然后,我在where语句中使用图像

I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement

image[np.where((image2 == [255,255,255].any(axis = 2))

我被困在这里,我不知道如何将找到的白色值更改为原始图像(与其他遮罩一起使用).我已经尝试过image.shape,但这没有用.

I am stuck here and I have no idea how to change found white values to what the original image is (to use alongside other masks). I have tried image.shape and this did not work.

谢谢.

推荐答案

制作蒙版的副本,然后根据白色像素坐标在蒙版的白色像素上绘制原始图像.您也可以选中mask == 255进行逐元素比较.您不需要np.where,因为您可以通过mask == 255创建的布尔掩码对数组进行索引.

Make a copy of the mask and then draw the original image over the white pixels of the mask from the white pixel coordinates. You can also check mask == 255 to compare element-wise. You don't need np.where because you can index arrays via the boolean mask created by mask == 255.

out = mask.copy()
out[mask == 255] = original_image[mask == 255]

这篇关于用原始图像opencv Python替换蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:33