How to apply GradientFilter locally to an image?

I have an MR image of a knee.

I want to apply a gradient filter locally so that if I click on a pixel, for example, say on any part of the femur, I'll be able to choose a circular or rectangular region of interest dynamically, on which the gradient filter will be applied.

Is it possible to do so?

Thanks a lot for the answer from bill s.

I've made one modification to his code for my specific purpose to apply a threshold to the gradient.

dims = ImageDimensions[img];

loc = MousePosition["GraphicsAbsolute", None];

PlotRange -> Transpose[{{0, 0}, dims}], ImageSize -> dims];

And here is the screenshot of the output:

Now suppose, I want to select and highlight the femur by applying suitable values for the size and radius and make all other parts transparent (as it can be done here). To do this I need to choose the nearest gradient peaks along all directions from the position of the mouse, so that I can create the desired boundary and thus the binary mask to select the region of interest.

A region growing or watershed transformation may be applied as explained in this video:

How can I do so?

• Yes. It's a full fledged programming language, so it's possible. The real question you want to ask is what is the best workflow for doing a filter locally. I could write you a program to do this exactly like you describe, but there's probably a better workflow. Apr 8 '16 at 20:54

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];
Manipulate[loc = MousePosition["GraphicsAbsolute", None];
PlotRange -> Transpose[{{0, 0}, dims}], ImageSize -> dims];


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.

• You forgot to include definitions for mask, minx and miny in your edited version, so it does not work anymore. Apr 10 '16 at 6:44
• +1. Suggestions: You could use Graphics to create the mask, instead of array manipulations. That way, you don't need the bounds checks and it's easier to change the mask shape (e.g Graphics[Disk[pt, radius], PlotRange -> Transpose[{{0, 0}, ImageDimensions[img]}], ImageSize -> ImageDimensions[img], Antialiasing -> False]). Also: Why not use LocatorPane? Apr 10 '16 at 7:30
• @Alexey Popkov -- thanks for noticing. I've added back the parts that were accidentally deleted. Apr 10 '16 at 14:28

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".

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

filtered = ImageAdjust@GradientFilter[originalImage, 4];