This article illustrates a Java comparison of two picture similarity methods. Share to everyone for your reference. Specifically as follows:
Summary:
Importjava.awt.image.BufferedImage;
Importjava.io.File;
Importjavax.imageio.ImageIO;
/*** Compare the similarity of two pictures * @authorGuihua **/publicclassbmploader{//changed into binary code
PUBLICSTATICSTRING[][]GETPX (Stringargs) {int[]rgb=newint[3];
Filefile=newfile (args);
Bufferedimagebi=null;
Try
The main code is as follows:
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO; /** * Compare the similarity of two pictures * @author Guihua * */public class Bmploader {//change into binary code public static string[][] GETPX (String a
RGS) {int[] RGB = new INT[3];
File File = new file (args);
BufferedImage bi = null;
try {bi = imageio.read (file);
catch (Exception e) {e.printstacktrace ();
int width = bi.getwidth ();
int height = bi.getheight ();
int minx = Bi.getminx ();
int miny = Bi.getminy ();
string[][] list = new String[width][height]; for (int i = Minx i < width; i++) {for (int j = miny; j < height; j +) {int pixel = Bi.getrgb (i, J)
;
Rgb[0] = (Pixel & 0xff0000) >> 16;
RGB[1] = (Pixel & 0xff00) >> 8;
RGB[2] = (pixel & 0xff);
LIST[I][J] = rgb[0] + "," + rgb[1] + "," + rgb[2];
} return list; public static void Compareimage (String imgPath1, StringIMGPATH2) {string[] images = {imgPath1, imgPath2};
if (Images.length = = 0) {System.out.println ("Usage >java bmploader imagefile.bmp");
System.exit (0);
}//Analysis picture similarity begin string[][] List1 = GETPX (images[0));
string[][] List2 = GETPX (images[1));
int xiangsi = 0;
int busi = 0;
int i = 0, j = 0;
For (string[] strings:list1) {if ((i + 1) = = List1.length) {continue; for (int m=0; m<strings.length; m++) {try {string[] value1 = list1[i][j].tostring (). Split ("
,");
string[] value2 = list2[i][j].tostring (). Split (",");
int k = 0; for (int n=0; n<value2.length; n++) {if Math.Abs (Integer.parseint (value1[k))-Integer.parseint (Value2[k)
) < 5) {xiangsi++;
else {busi++;
catch (RuntimeException e) {continue;
} j + +;
} i++;
} List1 = GETPX (images[1]);
List2 = GETPX (Images[0]);
i = 0;
j = 0;
For (string[] strings:list1) {if ((i + 1) = = List1.length) {continue; for (int m=0; m<strings.length; m++) {try {string[] value1 = list1[i][j].tostring (). Split ("
,");
string[] value2 = list2[i][j].tostring (). Split (",");
int k = 0; for (int n=0; n<value2.length; n++) {if Math.Abs (Integer.parseint (value1[k))-Integer.parseint (Value2[k)
) < 5) {xiangsi++;
else {busi++;
catch (RuntimeException e) {continue;
} j + +;
} i++;
} String Baifen = "";
try {Baifen = (double.parsedouble (Xiangsi + "")/double.parsedouble ((Busi + xiangsi) + "")) + "");
Baifen = baifen.substring (Baifen.indexof (".") + 1, Baifen.indexof (".") + 3); catch (Exception e) {Baifen = "0 ";
} if (Baifen.length () <= 0) {Baifen = "0";
} if (busi = = 0) {baifen= "100";
System.out.println ("Similar number of pixels:" + Xiangsi + "not similar to the number of pixels:" + busi + "similarity:" + integer.parseint (baifen) + "%");
public static void Main (string[] args) {bmploader.compareimage ("e:\\12.bmp", "e:\\1.bmp");
}
}
I hope this article will help you with your Java programming.