How to convert black holes on white background, so that I can see a binary image of holes on white image or bright dots on black image.

enter image description here

I can create a mask then also I am stuck with bright dot on black area, and then remaining white area.

 ms = ColorNegate[image]

enter image description here

  • 1
    $\begingroup$ A simpler way to create that mask would be ColorNegate. $\endgroup$
    – C. E.
    Feb 21, 2014 at 20:15

2 Answers 2


ImageDifference[] is ideal for such a task:

i = ColorNegate@Import["https://i.sstatic.net/Eskg5.png"];
ImageDifference[DeleteSmallComponents[i, 30, CornerNeighbors -> False], i]

Mathematica graphics

  • $\begingroup$ I would argue that the key to this is not ImageDifference but DeleteSmallComponents which acts as kind of inverse top-hat transform. I.E. we need ImageDifference because DeleteSmallComponents does the opposite of what we want. $\endgroup$
    – C. E.
    Feb 22, 2014 at 0:47
  • $\begingroup$ @Pickett Ok, you can keep your key. I'll keep mine and everybody's happy :) $\endgroup$ Feb 22, 2014 at 0:58
  • $\begingroup$ I'm didn't feel happy/content when I read your comment because you make it sound like a matter of opinion. You don't even need ImageDifference, you might as well use e.g. ImageSubtract or ImageMultiply. And you don't even bother to explain why you think ImageDifference is "ideal" for this situation, when in fact it is not even necessary in the solution, which is based on DeleteSmallComponents? $\endgroup$
    – C. E.
    Feb 23, 2014 at 12:43

Using TopHatTransform:

img = Import["https://i.sstatic.net/TlMUh.png"];
TopHatTransform[img, DiskMatrix[5]]


Since you ask this kind of basic image processing question, I guess a good idea would be to read about morphological operations. A very nice practical book is the legendary Digital Image Processing by Gonzales & Woods. There you will learn that the top-hat transform is built up from more basic morphological operators. Therefore, all the next lines give the same result:

TopHatTransform[img, 3]
ImageSubtract[img, Opening[img, 3]]
ImageSubtract[img, Dilation[Erosion[img, 3], 3]]

Additionally, you should be aware, that morphological operations probably don't work in all circumstances. Then, belisarius' approach might be an alternative (and worth an upvote) as long as the components you want to extract are separated.


Your Answer

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

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