[英]Convert an image to black and white mask using opencv python
I have a transparent background image and i want to color all transparent region as black and rest of region as white.我有一个透明的背景图像,我想将所有透明区域着色为黑色,将其余区域着色为白色。
imgage = cv2.imread("imgage.png", cv2.IMREAD_UNCHANGED)
trans_mask = image[:,:,3] == 255
image[trans_mask] = [255, 255, 255, 255]
The output i got it.我得到它的输出。 I got inside area to fill white and outside area to fill black.
我在内部区域填充白色,在外部区域填充黑色。 Any suggestion
任何建议
The simplest way to do this is starting out with a fully-black image and filling in just the area with positive alpha:最简单的方法是从全黑图像开始,并仅用正 alpha 填充区域:
res = np.zeros(image.shape[:2], np.uint8) # black by default
colored_areas = image[...,3] > 0
res[colored_areas] = 255
You say:你说:
i want to color all transparent region as black and rest of region as white.
我想将所有透明区域着色为黑色,将其余区域着色为白色。
So this should satisfy your request:所以这应该满足您的要求:
and you get that from this simple code:你可以从这个简单的代码中得到:
image[:,:,3]
Just apply imwrite
or imshow
+ waitKey
(or matplotlib) to see the data.只需应用
imwrite
或imshow
+ waitKey
(或 matplotlib)即可查看数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.