How to apply GradientFilter locally to an image?

Here's an interactive version -- wherever you point the mouse, the gradient image is shown. Thanks to nikie for the improved mask (using Graphics instead of array manipulations).

img = Import["http://i.stack.imgur.com/Na1fq.png"];
dims = ImageDimensions[img];
grad = GradientFilter[img, 2] // ImageAdjust;
Manipulate[loc = MousePosition["GraphicsAbsolute", None];
 mask = Graphics[Disk[loc, radius], 
   PlotRange -> Transpose[{{0, 0}, dims}], ImageSize -> dims];
 maskedImage = SetAlphaChannel[img, Image[mask]];
 Show[grad, maskedImage], {{radius, 30}, 1, 100}]

enter image description here

This works by creating an alpha channel for the top image that is transparent when within "radius" pixels of the mouse and opaque otherwise. Where it is transparent, the gradient image shows through.


I would do this in Mathematica by creating a mask and then applying the filter within that mask.

To create a mask, click the image and select "Mask tool in the pop-up dialog".

enter image description here

You can then use ImageFilter with the Mask option or you can do the Masking yourself.

filtered = ImageAdjust@GradientFilter[originalImage, 4];

ImageCompose[originalImage, SetAlphaChannel[filtered, mask]]

enter image description here

We could make a user interface for this. That would probably involve a ClickPane or LocatorPane for a simple interface.