8
$\begingroup$

I have an MR image of a knee.

MRI 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];

Manipulate[grad = GradientFilter[img, size] // ImageAdjust;

loc = MousePosition["GraphicsAbsolute", None];

mask = Graphics[Disk[loc, radius],

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

maskedImage = SetAlphaChannel[img, Image[mask]];

Show[grad, maskedImage], {size, 2, 30}, {{radius, 30}, 1, 200}]

And here is the screenshot of the output: enter image description here

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:

https://www.youtube.com/watch?v=s1ot5MltxM8&spfreload=1

How can I do so?

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Searke
    Apr 8, 2016 at 20:54

2 Answers 2

9
$\begingroup$

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["https://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.

$\endgroup$
3
  • $\begingroup$ You forgot to include definitions for mask, minx and miny in your edited version, so it does not work anymore. $\endgroup$ Apr 10, 2016 at 6:44
  • $\begingroup$ +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? $\endgroup$ Apr 10, 2016 at 7:30
  • $\begingroup$ @Alexey Popkov -- thanks for noticing. I've added back the parts that were accidentally deleted. $\endgroup$
    – bill s
    Apr 10, 2016 at 14:28
8
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.