How To Blur The Image According To Segmentation Map
Forgive me if I am unable to explain well because I am not native speaker. I am working on blurring the part of image according to the white part of segmentation map. For example h
Solution 1:
You can do it in the following steps:
- Load original image and mask image.
- Blur the whole original image and save it in a different variable.
- Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.
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)
Solution 2:
Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter
and NumPy's boolean indexing:
from skimage import io
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt
mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
img = np.random.random(size=mask.shape[:2])
idx = mask.min(axis=-1) == 255
blurred = gaussian_filter(img, sigma=3)
blurred[~idx] = 0
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
for ax, im inzip(axs, [img, mask, blurred]):
ax.imshow(im, cmap='gray')
ax.set_axis_off()
plt.show(fig)
Post a Comment for "How To Blur The Image According To Segmentation Map"