繁体   English   中英

Java中的图像处理和比较

[英]Image Processing and comparing in java

我需要比较两种相似类型的图像,但是在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);
    }
}

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

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

由于图像大小不同以及背景空间也不同并且对象大小也不同,因此无法逐像素进行直接图像比较。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM