0

我需要比较两张类似但在 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);
    }
}
4

2 回答 2

1

您可以为这种结构开发自己的算法。正如我所见,您的图像背景是白色的,因此首先您对图像执行背景去除操作。

然后你将你的图像调整到较低的级别,比如 256x256 或类似的东西。不,图像中只有对象,因为颜色已经移出。因此,您可以逐个像素地进行比较,并保持 84-90% 的阈值会给您想要的预期结果。

逐个像素的直接图像比较将不起作用,因为图像的大小不同,背景空间也不同,物体大小也不同。

于 2016-02-18T06:22:05.047 回答
0

在比较图像之前,您需要应用一些图像处理技术,例如直方图。opencv 的 Java API 是用于此类操作的好工具。见链接

于 2015-12-05T09:05:19.920 回答