Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java image
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 4:27am
Comments
Sergey Alexandrovich Kryukov at 28-Apr-13 13:01pm
   
This combination of words makes no sense. All images are "binary".
—SA
Zoltán Zörgő at 28-Apr-13 16:38pm
   
But it does! 1 bit color depth images are called binary - "mono" makes less sense here.
Sergey Alexandrovich Kryukov at 28-Apr-13 19:49pm
   
Yes, I already found some documentation using this kind of jargon, thank you.
—SA
Zoltán Zörgő at 29-Apr-13 0:24am
   
I have left out a link. Please see updated answer.
Rate this: bad
good
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.
 
[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
  Permalink  
v3
Comments
Zoltán Zörgő at 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 at 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 at 28-Apr-13 19:49pm
   
Fixed. Scratched .NET, added Java library reference.
Thank you again,
—SA
Zoltán Zörgő at 29-Apr-13 0:22am
   
Yes, it is fixed now :) Worth a 5!
Sergey Alexandrovich Kryukov at 29-Apr-13 0:41am
   
Thank you, Zoltán.
—SA
Rate this: bad
good
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: 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]
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 28-Apr-13 19:50pm
   
My 5.
—SA
Zoltán Zörgő at 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
0 OriginalGriff 200
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 169
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
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