# Turning an image to binary

• 01-17-2010, 01:09 AM
morghul
Turning an image to binary
hello,
What i need is to turn an image of handwritten digits written with dark colour on a light background. I need to have white background and black digits, so I can prepare a vector for neural network.
How should I do that? What algorithms should I use?
Thanks
• 01-17-2010, 02:16 AM
travishein
i would guess a good start would be to 'poster-ize' the image sample, that is to reduce its color depth

I remember something from my academic years where scanning each pixel and observe the color of the adjacent pixels and if the majority of them are darker then this pixel becomes black and if the majority are lighter, this one becomes white. have a look at the "GIMP" (a desktop application,but they have some fun transforms such as this posterize feature.

another thing to consider today, is how much granularity do we want to quantize the image in. back in the day when digital cameras only had a resolution of about 100 to 300 pixels, it was straight forward to create a recursive algorithm, or even a bunch of for() loops to scan each pixel in the image and it would work within a reasonable amount of time. Now that the cameras of today have thousands of pixels by thousands of pixels, the amount of processing time required to transform an entire image pixel by pixel can become quite huge.

One approach is to create a 'software pixel', where you block the image out in groups of pixels, say 4x4 . and take an average value of all the pixels to mean the value of the one pixel. Now for hand writing this might need some playing with to find what the good amount of grouping pixels is with the resolution of your input digital image, but the the coarser the quantized input data is into the neural network, the easier it is to start working with it right, and the less chance of it intepreting small hand writing artifacts as parts of the characters.

Now, for specific code or algorithms, I don't have any for these yet, but hoping these high level concepts are a good place to start for ideas.
• 01-17-2010, 03:12 AM
morghul

What I do at the moment is:
1. change the image colors to grey scale
2.check pixel by pixel if it's darker then neighbours (to set frames around digits)
3.cut each digit
4.check each pixel if it's lighter than average of 10 pixels taken form the image.
If it is then change it's colour to white, else change it to black

It doesn't work slow, but the problem is when I have a high resolusion photo with noise then my algorithm fails.
• 01-17-2010, 08:47 PM
travishein
hmm, that removal of noise from the image is a bit out of my expertise.

There seems to be a lot of work in this field, certainly no shortage of patents