Java implements pixel-Level Image File Processing and collection
A friend asked me to help with a small program for image recognition. Because it was used in a specific environment, I decided to implement it in java. First, we use matlab to simulate the recognition algorithm. Because it only recognizes specific combinations of numbers, it is very simple. We give up using complicated recognition algorithms, use the most common recognition algorithm for Pixel comparison. (If the background noise is complicated, you can consider filtering and then identifying it.) Some problems are found when writing java programs. There are not many materials on the Internet about image pixel-level operations, and some of them are not too correct, I would like to share my achievements with you.
Core classes: BufferedImage and ImageIO
The ImageIO class provides an image read/write interface for URL, InputStream, and other operations to obtain image information conveniently.
ImageIO is a standard class in jdk in the javax. imageio. * package. The following methods are provided:
Read () Example: BufferedImage imd = ImageIO. read (new File (file ));
Write () Example: ImageIO. write (imd, "JPEG", new File ("C: // test" + k + ". gif "));
// You can find the jdk doc for specific methods.
The BufferedImage class is a subclass of the Image class. Unlike the Image class, it is created and modified in the memory. You can display it or not display it, this depends on your specific needs. This is because I am used for image recognition, so it does not need to be displayed. You can use ImageIO to read a file to BufferedImage, or write it back to a file. For similar operations, see the previous two methods. For more information, see jdk doc.
Because I want to identify a digital string image similar to identity authentication, I want to separate these numbers and there are different images. Here the BufferedImage class provides a very convenient way.
GetSubimage (int left, int top, int width, int height)
For example, BufferedImage newim [] = new BufferedImage [4];
Newim [0] = imd. getSubimage (4, 0, 10, 18 );
Newim [1] = imd. getSubimage (13, 0, 10, 18 );
Newim [2] = imd. getSubimage (22, 0, 10, 18 );
Newim [3] = imd. getSubimage );
Finally, in order to get the pixel of the image, we need to get the pixel method. There are many methods. Here I will introduce
GetRGB (int x, int y) returns the RGB value of a specific pixel.
For example, pix = new int [10*18]; pix [I * (10) + j] = newim [k]. getRGB (j, I );
Now we get the pixel. We can see that the pixel is a one-dimensional array. If you are not used to it, you can consider storing it in a two-dimensional array and then implement your housekeeping algorithm, what are wavelet transformations and Laplace operators? Come on. How is it convenient? You don't seem to understand anything. I 'd like to give you some source programs, including pixel decomposition and recognition algorithms.
Source code
/*
* Created on 2005-11-29
*
* TODO To change the template for this generated file go
* Window-Preferences-Java-Code Style-Code Templates
*/
Package com. syvin. image;
Import java. awt .*;
Import java. awt. image .*;
Import java. io. FileOutputStream;
Import java. io .*;
Import java. io. InputStream;
Import java.net. URL;
Import javax. imageio .*;
Public class MyImage {
BufferedImage imd; // the image to be recognized
Private int iw, ih; // image width and height
Public final static String path = "D: // jyy // app // tomcat // webapps // userlogon // a.jpg ";
Static public void main (String args []) {
Try {
MyImage app = new MyImage (); // construct a class
String s = app. getImageNum ("C: // .bmp"); // obtain the recognition String
System. out. println ("recognize result" + s );
Byte [] by = s. getBytes ();
File F = new file ("C: // testfile.txt ");
Fileoutputstream Fos = new fileoutputstream (f); // write a result File
FOS. Write ();
FOS. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
// Constructor
Public myimage () throws ioexception {
Super ("image test ");
Try {
} Catch (exception e ){
E. printstacktrace ();
}
}
// Obtain the image Value
Public String getimagenum (string file ){
Stringbuffer sb = new stringbuffer ("");
Try {
Imd = ImageIO. read (new File (file); // read the image using the static ImageIO Method
BufferedImage newim [] = new BufferedImage [4];
Int [] x = new int [4];
// Divide the image into four parts, because the file to be processed has four numbers.
Newim [0] = imd. getSubimage (4, 0, 10, 18 );
Newim [1] = imd. getSubimage (13, 0, 10, 18 );
Newim [2] = imd. getSubimage (22, 0, 10, 18 );
Newim [3] = imd. getSubimage );
For (int k = 0; k <4; k ++ ){
X [k] = 0;
ImageIO. write (newim [k], "JPEG", new File ("C: // test" + k + ". gif "));
This. iw = newim [k]. getWidth (null );
This. ih = newim [k]. getHeight (null );
Pix = new int [iw * ih];
// Because it is a binary image, the method here reads the pixel and converts it to an image array of 0, 1.
For (int I = 0; I <ih; I ++ ){
For (int j = 0; j <iw; j ++ ){
Pix [I * (iw) + j] = newim [k]. getRGB (j, I );
If (pix [I * (iw) + j] =-1)
Pix [I * (iw) + j] = 0;
Else pix [I * (iw) + j] = 1;
X [k] = x [k] + pix [I * (iw) + j];
}
}
// Obtain the matching number.
Int r = this. getMatchNum (pix );
Sb. append (r );
System. out. println ("x =" + x [k]);
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return sb. toString ();
}
// Number template 0-9
Static int [] [] value = {
// Num 0;
{0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
,
0, 0, 1, 1, 1, 0, 0,
,
,
,
,
,
,
,
,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
// NUM 1
{0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
,
,
,
,
,
,
,
,
,
,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
// Num2
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
,
,
,
0, 0, 0, 0, 0,
0, 0, 0, 0,
,
,
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
// Num3
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0,
,
0, 0, 0, 0, 0,
0, 0, 0, 0,
,
0, 0, 0, 0,
,
0, 0, 0, 0, 0,
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, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
,
,
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
// Num8
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
,
,
,
,
,
,
,
,
,
,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
// Num9
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
,
,
,
,
,
,
0, 0, 0, 0, 0,
,
,
,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
}};
// Obtain the minimum entropy from the absolute values of pixel subtraction.
Public int getMatchNum (int [] pix ){
Int result =-1;
Int temp = 100;
Int x;
For (int k = 0; k <= 9; k ++ ){
X = 0;
For (int I = 0; I <pix. length; I ++ ){
X = x + Math. abs (pix [I]-value [k] [I]);
}
/* For (int a = 0; a <18; a ++ ){
For (int B = 0; B <10; B ++ ){
System. out. print (pix [a * 10 + B] + "-" + value [k] [a * 10 + B] + "| ");
}
System. out. println ();
}*/
If (x <temp)
{
Temp = x;
Result = k;
}
}
Return result;
}
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/jinyykiller/archive/2005/12/29/565584.aspx