# Better way to binarize an image

I have an image:

pic = Import[
"http://note.youdao.com/yws/public/resource/\
2c5a562224240edcf1631fcd39810f56/55FA538207424C0A98AB548724994AE1"]


that I want to binarize. When I try to do it in Mathematica, the results is

My code is

bin = MorphologicalBinarize[pic // ColorNegate, {0.4, 0.6}]


I tried this many times. The result was always bad. I want to binarize the image so I can count the lattice (including that adjacent to the border). Can anyone help me?

• Can you try reposting your first image to imgur? – J. M.'s discontentment Jul 5 '15 at 14:22
• From the looks of your output image (please repost the original though) you should try LocalAdaptiveBinarize. – user484 Jul 5 '15 at 19:51
• @J.M. Can I repost my picture to imgur when I'm commenting? – yode Dec 10 '15 at 6:10
• Sure, why not?${}$ – J. M.'s discontentment Dec 10 '15 at 6:12
• How to do it?Or where is the tutorial? – yode Dec 10 '15 at 6:14

As suggested by Rahul, LocalAdaptiveBinarize is your friend:

LocalAdaptiveBinarize[pic, 15]


In order to count things, you can use the MorphologicalComponents command. Here we colorize it to get a visualization of how well the morphological processing segments the data:

MorphologicalComponents[LocalAdaptiveBinarize[pic, 15]] // Colorize


Of course, MorphologicalComponents is very versatile and can help gather statistics about your image and the segmentation.

• +1, I would also suggest a bigger radius so you don't get the extra noise in the middle of the pieces. – user484 Jul 6 '15 at 15:05