user3206070
user3206070

Reputation: 113

Binarize an image

Cropped Image 1

Cropped Image 2

Binarized Image

I am sorry for image sizes.

Now these two cropped images I have in a dataset. I am going to feed these images into a machine learning algorithm. Before doing that, I want to extract binarized digits and feed the binary images into algorithm, instead of feeding directly. Can you please elaborate how can I achieve this kind of clean binarization ?

I found otsu and other thresholding methods but they were unable to give clear digits in image.

Upvotes: 3

Views: 6543

Answers (2)

fmw42
fmw42

Reputation: 53081

An alternate approach, to what Mark Setchell suggested, in ImageMagick goes over to OpenCv rather straightforward. OpenCV has adaptive thresholding see https://docs.opencv.org/3.3.1/d7/d4d/tutorial_py_thresholding.html and connected components processing see https://docs.opencv.org/3.1.0/d3/dc0/group__imgproc__shape.html#gac2718a64ade63475425558aa669a943a and https://www.pyimagesearch.com/2016/10/31/detecting-multiple-bright-spots-in-an-image-with-python-and-opencv/

1) convert to grayscale
2) stretch image to full dynamic range
3) apply local (adaptive) thresholding
4) optionally use connected components labelling to remove regions smaller than some total number of pixels (area).


convert 2.png \
-colorspace gray \
-auto-level \
-lat 20x20+10% \
2_lat.gif

enter image description here

convert 19.png \
-colorspace gray \
-auto-level \
-negate \
-lat 20x20+5% \
19_lat.gif

enter image description here

Do optional connected components processing here:

convert 2_lat.gif \
-define connected-components:area-threshold=40 \
-define connected-components:mean-color=true \
-connected-components 4 \
2_lat_ccl.gif

enter image description here

convert 19_lat.gif \
-define connected-components:area-threshold=20 \
-define connected-components:mean-color=true \
-connected-components 4 \
19_lat_ccl.gif

enter image description here

To smooth the images, you would likely need to use a raster to vector tool such as potrace.

Upvotes: 4

Mark Setchell
Mark Setchell

Reputation: 207445

I had some success, though I don't know how it will fare with the rest of your images, using a 2-colour quantisation, conversion to greyscale and normalisation.

I just did it at the command line with ImageMagick, as follows:

convert input.png +dither -colors 3 -colors 2 -colorspace gray -normalize -scale 250x result.png

So, it loads an image and disables dithering, so that the subsequent quantisation only results in 2 actual colours rather than dithered mixtures. I then quantise down to 3 colours - still in RGB colourspace - and then further down to 2 colours. Then, I convert those 2 colours to greyscale and normalise them so the darker one becomes black and the lighter one becomes white.

enter image description here

enter image description here

Upvotes: 6

Related Questions