我需要比较两张类似但在 JAVA 中大小不同的图像,即一张背景宽度低,另一张背景宽度高。并且图像的大小也不同。我附上了两个文件。哪个人和 T 恤相同,所以我需要将结果显示为图像相同。但是当我使用像素级图像匹配时,由于背景宽度不同,它并没有向我显示真实结果。然后我试图删除背景,然后比较它仍然是同样的问题。请完善随附的图像和代码。请帮助图片链接: 图片一和 图片二
代码:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JOptionPane;
public class ImageProcesing extends Canvas {
private static int x1 = 0, y1 = 0;
private static int h, w;
private static final Random random = new Random();
private Color mycolor;
BufferedImage img, img1;
public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
h = 512;
w = 512;
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, w, h, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
public BufferedImage scaleImage2(String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, w, h, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
public ImageProcesing() {
try {
this.img = scaleImage(512, 512, "D:\\I Tech Solutions\\Rahul Ratda\\Experiments\\1.jpeg");
this.img1 = scaleImage2("D:\\I Tech Solutions\\Rahul Ratda\\Experiments\\3.jpeg");
} catch (Exception ex) {
Logger.getLogger(ImageProcesing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void paint(Graphics g) {
int n = 0, k = 0, l = 0;
int tp = 0;
super.paint(g);
Color oldColor = new Color(img1.getRGB(0, 0));
for (int x = 0; x < h; x++) {
tp = 0;
w=512;
for (int y = 0; y < w; y++) {
oldColor = new Color(img1.getRGB(y, x));
mycolor = new Color(img.getRGB(y, x));
if (tp == 0 && oldColor.equals(Color.WHITE)) {
continue;
} else {
if (tp == 0) {
tp = 1;
w = w - y;
}
k++;
if ((mycolor.equals(oldColor))) {
g.setColor(mycolor);
g.drawLine(y, x, y, x);
n++;
}
}
}
}
System.out.println("K : "+k+"\n N : "+n);
if (n >= (k * 0.70)) {
System.out.println("Same");
}
else
System.out.println("Not Same");
/* oldColor=new Color(img1.getRGB(0,0));
for(int i = 0 ; i < WIDTH1; i++) {
for(int y = 0; y < HEIGHT1; y++) {
mycolor=new Color(img1.getRGB(i,y));
if((mycolor.equals(oldColor))){
y1++;
g.setColor(mycolor);
g.drawLine(i, y, i, y);
}
else
oldColor=mycolor;
}
}*/
/*if(x1>y1)
{
if(x1*0.6<y1)
JOptionPane.showMessageDialog(null,"Images are More than 60% Equal.");
else
JOptionPane.showMessageDialog(null,"Images are Less than 60% Equal.");
}
else{
if(y1*0.6<x1)
JOptionPane.showMessageDialog(null,"Images are More than 60% Equal.");
else
JOptionPane.showMessageDialog(null,"Images are Less than 60% Equal.");
}*/
}
/* private Color randomColor() {
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}*/
public static void main(String[] args) {
JFrame frame = new JFrame();
System.out.println("width = " + w);
System.out.println("height = " + h);
frame.setSize(1000, 1000);
frame.add(new ImageProcesing());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}