Image binarization Based on Java, java image binarization
I have been unable to process the graph processing capability table of Java for a long time, but it seems that this is not the case. I have used PHP for some applications and involved in verification code identification. Here is a binarization step for images, today, I want to use Java to implement
The java extension package javax. imageio provides us with a class named ImageIO, which provides some static and convenient methods for executing simple encoding and decoding. For details, refer to the API.
The following describes the principles of image binarization:
1. First, obtain the gray value of each pixel.
2. Define a threshold value.
3. Combine the gray value of each pixel with the gray value of the eight neighboring pixels and divide it by 9, and then compare it with the threshold value.
4. If the threshold value is greater than the threshold value, it is set to black and the light rain is white.
Paste the specific code below, and the comments are full.
- Separator is a constant of the File class. Due to its long relationship, the code specification at that time was not the same as the current one. constants must be capitalized, which is a historical issue, you don't have to worry too much (we recommend using separator instead of "/" for cross-platform convenience ).
- GetRGB in BufferedImage is an ARGB. A indicates transparency, R indicates red, G indicates green, and B Indicates blue.
- The parseInt method in Integer of the packaging class. The second optional parameter is "Number of hexadecimal numbers to be processed ".
- There are many algorithms on the Internet about the threshold value. If you are interested, you can study it by yourself. Here I randomly give an intermediate number of 130.
- For the gray value of the image, here we use simple (R + G + B)/3.
1 import java. awt. color; 2 import java. awt. image. bufferedImage; 3 import java. io. file; 4 import java. io. IOException; 5 6 import javax. imageio. imageIO; 7 8 public class ImageTest {9 10 public static void main (String [] args) throws IOException {11 String filename = "D:" + File. separator + "/123.jpg"; // separator is a constant in File. Due to issues left over from java, It is lowercase 12 File file File = new File (filename); 13 BufferedImage bi = Image IO. read (file); 14 // get the height and width of the current image, ARGB15 int h = bi. getHeight (); 16 int w = bi. getWidth (); 17 int rgb = bi. getRGB (0, 0); 18 int arr [] [] = new int [w] [h]; 19 20 // obtain the gray value of each pixel in the image 21 for (int I = 0; I <w; I ++) {22 for (int j = 0; j
Let's see the effect:
Kittler functions can directly use
Compare the RGB value, and of a vertex with the preset value. The value is big, white, and black.
Java binarization processing code (convert binary words of images in JGP format into values)
I don't understand what I mean. Can I explain it?