为了账号安全,请及时绑定邮箱和手机立即绑定

使用 python 模糊图像部分的最优雅方法是什么?

使用 python 模糊图像部分的最优雅方法是什么?

慕尼黑8549860 2021-11-16 15:40:53
我发现以下使用 PIL 在本地模糊图像的答案: Filter part of image using PIL, python。建议的答案裁剪图像的一部分,将其模糊并将其复制回原始图像。这会在模糊部分和原始图像之间创建清晰的边缘(请参见下面的示例)。
查看完整描述

1 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

为了避免这个问题,可以使用以下程序:

  • 给定一个图像和一个掩码(0 到 1 之间的值)

  • 模糊完整的输入图像和蒙版

  • 使用模糊蒙版对原始图像进行加权

  • 使用倒置的模糊蒙版对模糊图像进行加权

  • 添加加权图像

下面是一些使用 scipy 的示例代码:

import numpy as np

import matplotlib.pyplot as plt

from scipy import misc

import scipy.ndimage



def gaussian_blur(sharp_image, sigma):

    # Filter channels individually to avoid gray scale images

    blurred_image_r = scipy.ndimage.filters.gaussian_filter(sharp_image[:, :, 0], sigma=sigma)

    blurred_image_g = scipy.ndimage.filters.gaussian_filter(sharp_image[:, :, 1], sigma=sigma)

    blurred_image_b = scipy.ndimage.filters.gaussian_filter(sharp_image[:, :, 2], sigma=sigma)

    blurred_image = np.dstack((blurred_image_r, blurred_image_g, blurred_image_b))

    return blurred_image



def uniform_blur(sharp_image, uniform_filter_size):

    # The multidimensional filter is required to avoid gray scale images

    multidim_filter_size = (uniform_filter_size, uniform_filter_size, 1)

    blurred_image = scipy.ndimage.filters.uniform_filter(sharp_image, size=multidim_filter_size)

    return blurred_image



def blur_image_locally(sharp_image, mask, use_gaussian_blur, gaussian_sigma, uniform_filter_size):


    one_values_f32 = np.full(sharp_image.shape, fill_value=1.0, dtype=np.float32)

    sharp_image_f32 = sharp_image.astype(dtype=np.float32)

    sharp_mask_f32 = mask.astype(dtype=np.float32)


    if use_gaussian_blur:

        blurred_image_f32 = gaussian_blur(sharp_image_f32, sigma=gaussian_sigma)

        blurred_mask_f32 = gaussian_blur(sharp_mask_f32, sigma=gaussian_sigma)


    else:

        blurred_image_f32 = uniform_blur(sharp_image_f32, uniform_filter_size)

        blurred_mask_f32 = uniform_blur(sharp_mask_f32, uniform_filter_size)


    blurred_mask_inverted_f32 = one_values_f32 - blurred_mask_f32

    weighted_sharp_image = np.multiply(sharp_image_f32, blurred_mask_f32)

    weighted_blurred_image = np.multiply(blurred_image_f32, blurred_mask_inverted_f32)

    locally_blurred_image_f32 = weighted_sharp_image + weighted_blurred_image


    locally_blurred_image = locally_blurred_image_f32.astype(dtype=np.uint8)


    return locally_blurred_image



if __name__ == '__main__':


    sharp_image = misc.face()

    height, width, channels = sharp_image.shape

    sharp_mask = np.full((height, width, channels), fill_value=1)

    sharp_mask[int(height / 4): int(3 * height / 4), int(width / 4): int(3 * width / 4), :] = 0


    result = blur_image_locally(

        sharp_image,

        sharp_mask,

        use_gaussian_blur=True,

        gaussian_sigma=31,

        uniform_filter_size=201)

    plt.imshow(result)

    plt.show()

结果:

//img1.sycdn.imooc.com//619360ac0001794705800433.jpg

查看完整回答
反对 回复 2021-11-16
  • 1 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信