java - java中的图像处理和比较

标签 java image image-processing image-comparison

我需要比较两个相似类型的图像,但在 JAVA 中具有不同的尺寸,即一个具有低背景宽度,另一个具有高背景宽度。 而且图像的大小也不同。 我附上了两个文件。 其中人和 T 恤是相同的,所以我需要将结果显示为图像是相同的。 但是,当我进行像素级图像匹配时,由于背景宽度不同,它不会显示真实结果。 然后我尝试删除背景,然后比较它仍然是同样的问题。 请附上图片和代码。 请帮忙 图片链接: Image one & Image two

代码:

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);
    }
}

最佳答案

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

然后将图像大小调整为较低级别,例如 256x256 或类似的值。 不,图像中只有对象,因为颜色已经移出。 因此,您可以进行逐像素比较,并保持 84-90% 的阈值将为您提供所需的预期结果。

直接按像素进行图像比较是行不通的,因为图像大小不同,背景空间也不同,对象大小也不同。

关于java - java中的图像处理和比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34100306/

相关文章:

mysql - 将图像存储在 MySQL 数据库中

java - 如何在 Java 中对 URL/URI 中的默认虚拟主机 %2F 进行编码,使其不会更改为斜杠而无法工作

java - 下载由 JavaScript 更改的网页的 HTML

java 捕获网络摄像头图像

c - 如何像素化二进制 (P6) PPM 文件

algorithm - Canny边缘检测算法——实现问题

image-processing - 无法在 google colab 上使用 PIL 显示图像

java - 在 Android 中以编程方式发送电子邮件

java - 分支预测: Does avoiding "else" branch for simple operations makes code faster (Java example)?

html - div 中的图像在图像下方有额外的空间