如何根据分割图模糊图像 [英] How to blur the image according to segmentation map
本文介绍了如何根据分割图模糊图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请原谅我,因为我不是母语人士而无法很好地解释.
Forgive me if I am unable to explain well because I am not native speaker.
我正在根据分割图的白色部分来模糊图像的一部分.例如,这是我的分割图像(bmp图像). .
I am working on blurring the part of image according to the white part of segmentation map. For example here is my segmentation image ( bmp image ). .
现在我要模糊的是原始图像中分割图中像素为白色的部分.我只是这样写了下面的代码.
Now what I want is to blur the part of original image where the pixels are white in the segmentation map. I just wrote the following code to so.
mask = mask >= 0.5
mask = np.reshape(mask, (512, 512))
mh, mw = 512, 512
mask_n = np.ones((mh, mw, 3))
mask_n[:,:,0] *= mask
mask_n[:,:,1] *= mask
mask_n[:,:,2] *= mask
# discard padded area
ih, iw, _ = image_n.shape
delta_h = mh - ih
delta_w = mw - iw
top = delta_h // 2
bottom = mh - (delta_h - top)
left = delta_w // 2
right = mw - (delta_w - left)
mask_n = mask_n[top:bottom, left:right, :]
# addWeighted
image_n = image_n *1 + cv2.blur(mask_n * 0.8, (800, 800))
请帮助我,谢谢.
推荐答案
您可以按照以下步骤操作:
You can do it in the following steps:
- 加载原始图像和蒙版图像.
- 对整个原始图像进行模糊处理并将其保存在其他变量中.
- 使用np.where()方法从蒙版中选择想要模糊值的像素,然后替换它.
请参见下面的示例代码:
See the sample code below:
import cv2
import numpy as np
img = cv2.imread("./image.png")
blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
mask = cv2.imread("./mask.png")
output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
cv2.imwrite("./output.png", output)
这篇关于如何根据分割图模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文