【问题标题】:Quick & fast template matching on screen. Coordinates needed too. Java在屏幕上快速和快速的模板匹配。也需要坐标。爪哇
【发布时间】:2017-01-07 03:39:41
【问题描述】:

我需要一种在屏幕上查找图像的方法。我已经搜索了在 SO 上执行此操作的方法,但有些方法需要很长时间。我需要它快速高效,不需要准确。基本上我打算在屏幕上比较或搜索一个小的像素化图像,例如 11x10 像素。

我还需要一种方法来知道屏幕上小图像的 x 和 y 坐标。

虽然我已经浏览了许多工具,例如 JavaCV 和 OpenCV,但我只是想看看是否有其他方法可以做到这一点。

TL;DR

我需要一种快速的方法来搜索屏幕上的小(11x10 示例)图像并知道它的 x,y 坐标。

【问题讨论】:

    标签: java opencv


    【解决方案1】:

    我想你们很多人都觉得this answer 相关!但它适用于 Windows 和 c++。但我相信你可以很容易地将它转换成任何语言。

    【讨论】:

      【解决方案2】:

      这个问题很老了,但我试图在这里实现完全相同的事情。我发现结合这些答案可以解决问题:

      Convert BufferedImage TYPE_INT_RGB to OpenCV Mat Object

      OpenCV Template Matching example in Android

      您需要进行转换的原因是,当您使用 awt.Robot 类截取屏幕截图时,它的格式为 INT_RGB。匹配的模板示例需要字节,您不能直接从这种类型的图像中获取字节数据。

      这是我对这两个答案的实现,但它不完整。输出全部搞砸了,我认为这可能与 IntBuffer/ByteBuffers 有关。

      -编辑-

      我添加了一个将 INT_RGB 转换为 BYTE_BGR 的新辅助方法。我现在可以使用 matchLoc 在图像上获取模板的坐标。这似乎工作得很好,我可以将它与基于模板为我单击开始菜单的机器人一起使用。

      private BufferedImage FindTemplate() {                     
      
      
          System.out.println("\nRunning Template Matching");
          int match_method = Imgproc.TM_SQDIFF;
      
          BufferedImage screenShot = null;
      
          try {
              Robot rob = new Robot();
              screenShot = rob.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
      
      
          } catch (AWTException ex) {
              Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
          }
      
         if(screenShot == null) return;
      
          Mat img = BufferedImageToMat(convertIntRGBTo3ByteBGR(screenShot));
          String templateFile = "C:\\Temp\\template1.JPG"; 
          Mat templ = Highgui.imread(templateFile);
      
          // / Create the result matrix
          int result_cols = img.cols() - templ.cols() + 1;
          int result_rows = img.rows() - templ.rows() + 1;
          Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
      
          // / Do the Matching and Normalize
          Imgproc.matchTemplate(img, templ, result, match_method);
          Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
          Highgui.imwrite("out2.png", result);
      
          // / Localizing the best match with minMaxLoc
          MinMaxLocResult mmr = Core.minMaxLoc(result);
      
          Point matchLoc;
          if (match_method == Imgproc.TM_SQDIFF
                  || match_method == Imgproc.TM_SQDIFF_NORMED) {
              matchLoc = mmr.minLoc;
          } else {
              matchLoc = mmr.maxLoc;
          }
      
          Graphics2D graphics = screenShot.createGraphics();
          graphics.setColor(Color.red);
          graphics.setStroke(new BasicStroke(3));
          graphics.drawRect(matchLoc.x, matchLoc.y, templ.width(), templ.height());
          graphics.dispose();
      
       return screenShot;
      
      
      }                             
      
      
      private Mat BufferedImageToMat(BufferedImage img){
      
          int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
          ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);
          IntBuffer intBuffer = byteBuffer.asIntBuffer();
          intBuffer.put(data);
      
          Mat mat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
          mat.put(0, 0, byteBuffer.array());
          return mat;
      
      
      }`
      
      private BufferedImage convertIntRGBTo3ByteBGR(BufferedImage img){
          BufferedImage convertedImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
          Graphics2D graphics = convertedImage.createGraphics();
          graphics.drawImage(img, 0, 0, null);
          graphics.dispose();
      
          return convertedImage;
      }
      

      结果:

      模板:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        • 2021-11-09
        • 2011-09-05
        • 1970-01-01
        • 1970-01-01
        • 2023-02-02
        相关资源
        最近更新 更多