Click here to Skip to main content
13,045,790 members (47,302 online)
Rate this:
Please Sign up or sign in to vote.
See more:
please i need the best java code to convert gray image to binary image

i tried alot of examples but it is not accurate like for example the output of paint when save the photo as mono bitmap
Posted 28-Apr-13 3:27am
Sergey Alexandrovich Kryukov 28-Apr-13 13:01pm
This combination of words makes no sense. All images are "binary".
Zoltán Zörgő 28-Apr-13 16:38pm
But it does! 1 bit color depth images are called binary - "mono" makes less sense here.
Sergey Alexandrovich Kryukov 28-Apr-13 19:49pm
Yes, I already found some documentation using this kind of jargon, thank you.
Zoltán Zörgő 29-Apr-13 0:24am
I have left out a link. Please see updated answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think you need to convert the image to black and white by some threshold or similar criterion.

If you think at it just a bit, you will understand that this kind if filter cannot be more or less "accurate". It converts one image to another one according some threshold value, but this is you who can decide what threshold to choose.

[EDIT #1]

More exactly, the word "inaccurate" it totally inapplicable here. If you knew the final "perfect" result of some image transformation, you could compare it with the results you got, but there is no a "perfect" unambiguous transformation. By making a black-and-white image, you always lose some information. What exactly to lose, is a philosophical question. It depends on the input image and some criteria which can be quite hard to formulate.


[EDIT #2]

You can use, for example, AForge.Imaging.Filters.Threshold:[^].

This is from the well-known open-source library for .NET called AForge.NET:[^],[^].

For the threshold filter, please see[^].

You can find it in the JScience library:[^].

Zoltán Zörgő 28-Apr-13 16:45pm
Since OP asked for Java solution, I doubt he/she will be happy with a .net one...
Sergey Alexandrovich Kryukov 28-Apr-13 19:43pm
Thank you, Zoltán.
I've made this mistake because I recently was answering on image filtering for .NET.
Anyway, I think the most important part of the question is answered.
Sergey Alexandrovich Kryukov 28-Apr-13 19:49pm
Fixed. Scratched .NET, added Java library reference.
Thank you again,
Zoltán Zörgő 29-Apr-13 0:22am
Yes, it is fixed now :) Worth a 5!
Thank you, Zoltán.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

As you asked for java solution, I can recommend OpenCV[^]. And here you have a good tutorial about converting an image to binary (black and white) using thresholding, with some theory too:[^]. You will notice, that there is no best method, you will have to choose the most suitable for you. Note, that the samples there are not in Java, but it will be easy to do the same in java since both use OpenCV.

Binarization is in general a special type of image segmentation[^], and there are other, more complex aspects of that than thresholding. Please note also, that binarization can be a morphological operation too, like skeleton extraction.

If you don't want OpenCV, look here:[^]

[update: important link was missing]
Sergey Alexandrovich Kryukov 28-Apr-13 19:50pm
My 5.
Zoltán Zörgő 29-Apr-13 0:21am
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100