# How to make binary image from black holes on white background

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.

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

 ms = ColorNegate[image]


• A simpler way to create that mask would be ColorNegate. Feb 21, 2014 at 20:15

ImageDifference[] is ideal for such a task:

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


• 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. Feb 22, 2014 at 0:47
• @Pickett Ok, you can keep your key. I'll keep mine and everybody's happy :) Feb 22, 2014 at 0:58
• 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? 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.