title: Python: perform blur only within a mask of image
body: <p>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?</p>
  
  <p>hoping for something like:</p>
  
  <pre><code>import scipy
  blurredImage = scipy.ndimage.filters.gaussian_filter(img, sigma = 3, weight = myMask)
  </code></pre>
  
  <p>@stefan:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Dilate approach:</p>
  
  <pre><code>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)
  </code></pre>