2012-07-27 56 views
0

我一直在尝试几个小时来完成这项工作,但由于某种原因,它没有找到匹配项。你可以用任何图像对它进行测试,但它应该截取屏幕的左上角(1000px×1000px)并在其中找到指定的图像。任何帮助将不胜感激!在Java中的图像中查找图像

 import java.awt.Rectangle; 
     import java.awt.Robot; 
     import java.awt.image.BufferedImage; 
     import java.io.File; 
     import java.io.IOException; 

     import javax.imageio.ImageIO; 


     public class ImageRobotTester { 

      /** 
      * @param args 
      */ 
      public static void main(String[] args) { 
       ImageRobot i = new ImageRobot(); 
       try { 
        BufferedImage settingsImage = ImageIO.read(new File("images/Dex.png")); 
        Robot r = new Robot(); 
        BufferedImage screen = r.createScreenCapture(new Rectangle(0, 0, 1000, 1000)); 

        i.subImage(settingsImage, screen); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      } 

     } 
     import java.awt.image.BufferedImage; 

import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 


public class ImageRobot { 

    public ImageRobot() { 
     // TODO Auto-generated constructor stub 
    } 

    public void subImage(BufferedImage needle, BufferedImage hayStack) 
    { 
     int xMatch = 0; 
     int yMatch = 0; 
     int possMatches = 0; 
     try { 
      for(int j = 0; j < hayStack.getHeight() - needle.getHeight(); j++) 
      { 
       for(int i = 0; i < hayStack.getWidth() - needle.getWidth(); i++) 
       { 
        BufferedImage hayStackSub = hayStack.getSubimage(i, j, needle.getWidth(), needle.getHeight()); 
        if(hayStackSub.equals(needle)) 
        { 
         System.out.println("match!"); 
         xMatch = i; 
         yMatch = j; 
        } 

       } 
      } 
     } 
     catch(ArrayIndexOutOfBoundsException e) 
     { 
      System.out.println("Out of bounds! (" + xMatch + ", " + yMatch + ")"); 
     } 

     System.out.println("(" + xMatch + ", " + yMatch + ")"); 
    } 

} 
+0

是你正在寻找的,而你正在测试它在屏幕上显示的图像?我的意思是,如果你需要找到一个相似的图像,一个像素逐像素匹配没有工作的机会。 – anttix 2012-07-27 03:15:21

+0

'images/Dex.png'一个PNG可能包含半透明的颜色。一定要跳过检查这些像素。 – 2012-07-27 06:11:05

回答

2

纠正我,如果我错了,但你想在这里做一些图像识别? 您并不是在寻找与参考图像逐像素匹配的子图像,而是您正在寻找一个类似的图像?对?在这种情况下,您需要在您的大图像上移动一个小窗口(就像您现在正在使用double for循环一样),但不是使用“equals”,而是使用适当训练的神经网络来告诉如果你正在寻找的图像是在该窗口内。

请参见本教程,了解如何构建基于神经网络影像识别细节:

http://neuroph.sourceforge.net/image_recognition.html

+1

感谢您的回复。我正在寻找确切的图像。就好像试图找到Chrome徽标在我的桌面上的位置等。 – 2012-07-27 03:29:48

+1

由于颜色编码差异,边缘平滑,调整大小等原因,这也不完全匹配。但是,可能存在计算密集度较低的近似算法。例如。 http://en.wikipedia.org/wiki/Principal_component_analysis – anttix 2012-07-27 03:35:33