# Thread: Turning an image to binary

1. Member
Join Date
Nov 2009
Posts
6
Rep Power
0

## 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

2. 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.

3. Member
Join Date
Nov 2009
Posts
6
Rep Power
0

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.

4. 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

Keith Price Bibliography Noise Removal, Impulse Noise

some of them even older.

My thoughts, based on your current approach to average out pixels, what if you supplied a second step of averaging again after the first step, where you apply a weighting to the dark pixels based on how many continuous adjacent ones are. the theory being that smaller isolated spots might be the noise, and not part of a continous pen stroke that is part of a character.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•