[Python] Python: perform blur only within a mask of image

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a greyscale image and a binary mask of an ROI in that image. I would like to perform a blur operation on the greyscale image but only within the confines of the mask. Right now I'm blurring the whole image and than just removing items outside the mask, but I don't want pixels outside of the mask affecting my ROI. Is there a way to do this without building a custom blur function?

    hoping for something like:

    import scipy
    blurredImage = scipy.ndimage.filters.gaussian_filter(img, sigma = 3, weight = myMask)


    @stefan:

    blur = 3
    invmask = np.logical_not(mask).astype(int)

    masked = img * mask
    remaining = img * invmask

    blurred = scipy.ndimage.filters.gaussian_filter(masked, sigma = blur)
    blurred = blurred+remaining


    Dilate approach:

    blur = 3
    invmask = np.logical_not(mask).astype(int)
    masked = img * mask
    masked2 = scipy.ndimage.morphology.grey_dilation(masked,size=(5,5))
    masked2 = masked2 *invmask
    masked2 = masked + masked2
    blurred = scipy.ndimage.filters.gaussian_filter(masked2, sigma = blur)

    Continue reading...

Compartilhe esta Página