如何基于二进制蒙版裁剪图像 [英] How to crop image based on binary mask

查看:124
本文介绍了如何基于二进制蒙版裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有某些语义分割算法的手电筒来生成分割图像的二进制掩码.然后,我想基于该蒙版裁剪图像.为了清楚起见,我需要在每个像素的基础上进行裁剪.看来这是一个简单的问题,但我能想到的唯一解决方案是像Coco API中那样反转draw mask函数,或者遍历数组中的每个像素,并在不需要的情况下一起将像素设置为黑色.我觉得有更好的方法可以做到这一点. Lua,Python,Go或C ++中的库将对我有用.有什么想法吗?

I am using torch with some semantic segmentation algorithms to produce a binary mask of the segmented images. I would then like to crop the images based on that mask. To be clear I need to crop it on a per pixel basis. It seems like a simple problem but the only solution I can conjure up is to either invert a draw mask function like in the Coco API, or iterate over each pixel in the array and mask together setting the pixel to black if not needed. I feel like there is a better way of doing this. Libraries in Lua, Python, Go, or C++ will work for me. Any ideas?

推荐答案

我已经在Python中实现了此功能,假设您可以将输入图像和蒙版用作Mat对象. 假设src1是您的映像,而src1_mask是您的二进制掩码:

I've implemented this in Python, assuming that you have your input image and mask available as Mat Objects. Given that src1 is your image and src1_mask is your binary mask:

src1_mask=cv2.cvtColor(src1_mask,cv2.COLOR_GRAY2BGR)#change mask to a 3 channel image 
mask_out=cv2.subtract(src1_mask,src1)
mask_out=cv2.subtract(src1_mask,mask_out)

现在mask_out包含图像src1的一部分,该部分位于您定义的二进制掩码之内.

Now mask_out contains the part of the image src1 located inside the binary mask you defined.

这篇关于如何基于二进制蒙版裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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