Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
A simpler way to create that mask would be ColorNegate. – Pickett Feb 21 '14 at 20:15

2 Answers 2

up vote 4 down vote accepted

ImageDifference[] is ideal for such a task:

i = ColorNegate@Import[""];
ImageDifference[DeleteSmallComponents[i, 30, CornerNeighbors -> False], i]

Mathematica graphics

share|improve this answer
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. – Pickett Feb 22 '14 at 0:47
@Pickett Ok, you can keep your key. I'll keep mine and everybody's happy :) – belisarius has settled Feb 22 '14 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? – Pickett Feb 23 '14 at 12:43

Using TopHatTransform:

img = Import[""];
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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