如何识别其他图像?

时间:2018-10-30 17:59:02

标签: java image image-processing screenshot

所以我对Java还是很陌生,所以我不确定这样做的方法实际上是一个好主意,但是基本上我正在尝试检查另一个图像中一个图像的实例。因此,为了测试是否可行,我有一个200x148 jpg,从中获取字节,然后从窗口的屏幕截图中获取字节,并从中获取字节,然后进行比较。

现在,由于通常第一张图片不会出现在该屏幕截图中,因此我在照片应用中将其打开,并在程序休眠时将其放入(在抓取屏幕截图之前)。是的,我可以通过查看屏幕快照来确认第一张图像在屏幕截图中。但是,当我比较字符串时(检查带有图像1的所有字节数据的String是否位于具有图像2的所有字节数据的String中),结果为负。

这是到目前为止我要使用的代码:

public static void main(String[] args) throws IOException, HeadlessException, AWTException, InterruptedException  {
     // Get the first image
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ImageIO.write(ImageIO.read(new File("profile.jpg")), "jpg", baos);
     byte[] bytes = baos.toByteArray();

     String bytes1S = "";
     for (int i = 0; i < bytes.length; i++) {
         bytes1S += bytes[i];
     }
     // Give yourself enough time to open the other image
     TimeUnit.SECONDS.sleep(6);
     // Take the screenshot
     BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
     ImageIO.write(image, "jpg", new File("screenshot.jpg"));
     baos = new ByteArrayOutputStream();
     ImageIO.write(ImageIO.read(new File("screenshot.jpg")), "jpg", baos);
     byte[] bytes2 = baos.toByteArray();
     String bytes2S = "";
     for (int i = 0; i < bytes2.length; i++) {
         bytes2S += bytes2[i];
     }
     // Check if the second String of bytes contains the first String of bytes.
     if (bytes2S.contains(bytes1S))
         System.out.println("Yes");
     else
         System.out.println("No");

}

作为参考,这是第一张图片,以及它拍摄的屏幕截图:

First image

Screenshot

为什么没有在屏幕截图中检测到第一张图片,为什么有更好的方法(最好没有其他库)?

1 个答案:

答案 0 :(得分:0)

蛮力方法是简单地将两个图像都作为BufferedImage对象加载,然后逐像素浏览“主”图像,然后查看是否可以在其中找到“子图像”。 / p>

我已经实现了一段时间,并将下面的代码作为MCVE发布。

但是注释:当将图像另存为JPG文件时,它们将被压缩,并且这种压缩是有损。这意味着即使像素在屏幕上相等,像素也不会具有完全相同的颜色。在下面的示例中,使用“阈值”定义了像素的不同程度,以实用的方式解决了这一问题。但这有点武断,也不是那么可靠。 (更强大的解决方案将需要更多的精力。)

强烈建议将图像另存为PNG文件。他们使用无损压缩。因此,对于PNG文件,您可以在下面的代码中设置threshold=0

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.function.IntBinaryOperator;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FindImageInImage
{
    public static void main(String[] args) throws Exception
    {
        BufferedImage mainImage = 
            ImageIO.read(new URL("https://i.stack.imgur.com/rEouF.jpg"));
        BufferedImage subImage = 
            ImageIO.read(new URL("https://i.stack.imgur.com/wISyn.jpg"));

        int threshold = 100;
        Point location = findImageLocation(
            mainImage, subImage, threshold);
        System.out.println("At " + location);

        SwingUtilities.invokeLater(() -> showIt(mainImage, subImage, location));
    }

    private static void showIt(
        BufferedImage mainImage, BufferedImage subImage, Point location)
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.getContentPane().add(new JPanel()
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawImage(mainImage, 0, 0, null);
                if (location != null)
                {
                    g.setColor(Color.RED);
                    g.drawRect(location.x, location.y, 
                        subImage.getWidth(), subImage.getHeight());
                }
            }
        });
        f.setSize(1500, 800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }


    static Point findImageLocation(
        BufferedImage mainImage, 
        BufferedImage subImage, 
        int threshold)
    {
        return findImageLocation(mainImage, subImage, (rgb0, rgb1) -> 
        {
            int difference = computeDifference(rgb0, rgb1);
            if (difference > threshold)
            {
                return 1;
            }
            return 0;
        });
    }

    private static int computeDifference(int rgb0, int rgb1)
    {
        int r0 = (rgb0 & 0x00FF0000) >> 16;
        int g0 = (rgb0 & 0x0000FF00) >> 8;
        int b0 = (rgb0 & 0x000000FF);

        int r1 = (rgb1 & 0x00FF0000) >> 16;
        int g1 = (rgb1 & 0x0000FF00) >> 8;
        int b1 = (rgb1 & 0x000000FF);

        int dr = Math.abs(r0 - r1);
        int dg = Math.abs(g0 - g1);
        int db = Math.abs(b0 - b1);

        return dr + dg + db;
    }

    static Point findImageLocation(
        BufferedImage mainImage, 
        BufferedImage subImage, 
        IntBinaryOperator rgbComparator)
    {
        int w = mainImage.getWidth();
        int h = mainImage.getHeight();
        for (int x=0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                if (isSubImageAt(mainImage, x, y, subImage, rgbComparator))
                {
                    return new Point(x, y);
                }
            }
        }
        return null;
    }

    static boolean isSubImageAt(
        BufferedImage mainImage, int x, int y, 
        BufferedImage subImage, 
        IntBinaryOperator rgbComparator)
    {
        int w = subImage.getWidth(); 
        int h = subImage.getHeight();
        if (x + w > mainImage.getWidth())
        {
            return false;
        }
        if (y + h > mainImage.getHeight())
        {
            return false;
        }
        for (int ix=0; ix < w; ix++)
        {
            for (int iy = 0; iy < h; iy++)
            {
                int mainRgb = mainImage.getRGB(x + ix, y + iy);
                int subRgb = subImage.getRGB(ix, iy);
                if (rgbComparator.applyAsInt(mainRgb, subRgb) != 0)
                {
                    return false;
                }
            }
        }
        return true;
    }

}