Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Comments
Sergey Alexandrovich Kryukov 28-Apr-13 13:01pm    
This combination of words makes no sense. All images are "binary".
—SA
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.
—SA
Zoltán Zörgő 29-Apr-13 0:24am    
I have left out a link. Please see updated answer.

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.

[END EDIT #1]

[EDIT #2]


You can use, for example, AForge.Imaging.Filters.Threshold:
http://www.aforgenet.com/framework/docs/html/503a43b9-d98b-a19f-b74e-44767916ad65.htm[^].

This is from the well-known open-source library for .NET called AForge.NET:
http://en.wikipedia.org/wiki/Aforge.net[^],
http://www.aforgenet.com/[^].
For the threshold filter, please see org.jscience.computing.ai.vision.Filter:
http://jscience.org/experimental/javadoc/org/jscience/computing/ai/vision/ThresholdFilter.html[^].

You can find it in the JScience library: http://jscience.org/[^].

[END EDIT #2]
—SA
 
Share this answer
 
v3
Comments
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.
—SA
Sergey Alexandrovich Kryukov 28-Apr-13 19:49pm    
Fixed. Scratched .NET, added Java library reference.
Thank you again,
—SA
Zoltán Zörgő 29-Apr-13 0:22am    
Yes, it is fixed now :) Worth a 5!
Sergey Alexandrovich Kryukov 29-Apr-13 0:41am    
Thank you, Zoltán.
—SA
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: http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html[^]. 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: http://zerocool.is-a-geek.net/?p=376[^]

[update: important link was missing]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Apr-13 19:50pm    
My 5.
—SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900