Flask接口签名sign原理与实例代码浅析
596
2023-07-31
JAVA比较两张图片相似度的方法
本文实例讲述了java比较两张图片相似度的方法。分享给大家供大家参考。具体如下:
摘要:
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
/***比较两张图片的相似度*@authorGuihua**/publicclassBMPLoader{//改变成二进制码
publicstaticString[][]getPX(Stringargs){int[]rgb=newint[3];
Filefile=newFile(args);
BufferedImagebi=null;
try
主要代码如下:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
* 比较两张图片的相似度
* @author Guihua
*
*/
public class BMPLoader {
// 改变成二进制码
public static String[][] getPX(String args) {
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, String imgPath2){
String[] images = {imgPath1, imgPath2};
if (images.length == 0) {
System.out.println("Usage >java BMPLoader ImageFile.bmp");
System.exit(0);
}
// 分析图片相似度 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 try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int UhWaUVcAZyk = 0; for (int n=0; 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 try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; 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("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"); } public static void main(String[] args){ BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp"); } } 希望本文所述对大家的java程序设计有所帮助。
try {
String[] value1 = list1[i][j].toString().split(",");
String[] value2 = list2[i][j].toString().split(",");
int UhWaUVcAZyk = 0;
for (int n=0; 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 try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; 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("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"); } public static void main(String[] args){ BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp"); } } 希望本文所述对大家的java程序设计有所帮助。
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 try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; 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("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"); } public static void main(String[] args){ BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp"); } } 希望本文所述对大家的java程序设计有所帮助。
try {
String[] value1 = list1[i][j].toString().split(",");
String[] value2 = list2[i][j].toString().split(",");
int k = 0;
for (int n=0; 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("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"); } public static void main(String[] args){ BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp"); } } 希望本文所述对大家的java程序设计有所帮助。
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("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%");
}
public static void main(String[] args){
BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp");
}
}
希望本文所述对大家的java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~