5
$\begingroup$

I want to extract a part of an image. For example, let's start with something like the following image:

enter image description here

I would like to apply some image processing to get this result:

enter image description here

I have tried to manipulate Graphics[Disk] and the image using ImageAdd but it looks like the Graphics[Disk] is a box that contains a disk, so adding the Mathematica logo image to the Disk outputs a white box.

$\endgroup$
8
$\begingroup$

Here is one way:

extractAnnulus[image_, {r1_, r2_}] := Module[{mask},
  mask = Graphics[{
     White, Disk[{0, 0}, r2],
     Black, Disk[{0, 0}, r1]
     },
    PlotRange -> 1,
    Background -> Black,
    ImageSize -> ImageDimensions[image]
    ];

  ImageAdd[
   ImageMultiply[image, mask],
   ColorNegate[mask]
   ]
  ]

img = Import["http://i.stack.imgur.com/paxJ9.png"];
extractAnnulus[img, {0.4, 0.6}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ excellent!! Thank you so much. $\endgroup$ – JMarc Sep 30 '15 at 3:34
  • $\begingroup$ BTW, I was initially confused, but looks like your r1 and r2 are a %age of the image size..right? $\endgroup$ – JMarc Sep 30 '15 at 3:49
  • $\begingroup$ @JMarc Yes, I set PlotRange to one which means that zero is the top/left edges of the mask (a graphics object), and 1 is the bottom/right edges of the mask. i.e. I create a coordinate system where all coordinates are between 0 and 1. Then I specify, using ImageSize, that the corresponding image of this graphics object should have the same size as the image we are putting the mask over. This is actually useful to understand, because this approach breaks if the aspect ratio of the image is not square, then the aspect ratio has to be taken into account when creating the mask. $\endgroup$ – C. E. Sep 30 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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