【问题标题】:How do I look for a match of a smaller 2D array inside a bigger 2D array?如何在较大的 2D 数组中查找较小的 2D 数组的匹配项?
【发布时间】:2017-10-08 11:23:12
【问题描述】:

我的任务是创建一个图像数据库,其中包含多种类型的图像对象。每个图像都由一个二维像素阵列组成。我被要求创建这个方法: MatchPattern(Image subimage, Image image):这个函数返回一个整数,表示一个子图像在一个图像中重复了多少次。

我写的代码如下:

public int MatchPattern(Image subimage, Image image) {
    if(image.getClass().equals(subimage.getClass())){
        int numOfMatches = 0;

        int imgWidth = image.getWidth();
        int imgHeight = image.getHeight();

        int subimgWidth = subimage.getWidth();
        int subimgHeight = subimage.getHeight();

        if (imgWidth < subimgWidth || imgHeight < subimgHeight)
            return 0;
        for (int i = 0; i < imgHeight; i++) {
            for (int j = 0; j < imgWidth; j++) {

                int subHeightIndex = 0;
                int subWidthIndex = 0;

                Pixel imagePix = image.getImg()[i][j];
                Pixel subimgPix = subimage.getImg()[subHeightIndex][subWidthIndex];

                if( (imagePix.compareTo(subimgPix)==0) && ((imgWidth-j)>=subimgWidth) && ((imgHeight-i)>=subimgHeight) ){
                    boolean matchFlag = true;
                    for (int k = 0; k < subimgHeight; k++) {
                        if(matchFlag == false)
                            break;
                        for (int l = 0; l < subimgWidth; l++) {
                            matchFlag = (image.getImg()[i+k][j+l] == subimage.getImg()[k][l]);
                            if (matchFlag == false)
                                break;
                        }
                    }
                    if(matchFlag == true)
                        numOfMatches++;
                }                
            }
        }
    return numOfMatches;   
    }
    return 0;

但是,每当我运行该方法时,它总是返回一个等于 0 的匹配数。有人能指出我正确的方向吗? 提前谢谢你。

【问题讨论】:

  • 为什么不在if 语句中做一个简单的System.out.println 看看控制是否进入内部?基本上,通过调试器并检查控制的去向,如果它没有去预期的地方,那么反映为什么会这样......?等等。最终,您将缩小问题范围并找出问题的原因。
  • 假设您在同一问题上关注post。找到更好的算法可能会有所帮助。还有这个post和这个example
  • 学习使用调试器
  • Aominè,我试过这样做,但 System.out.println 都没有打印出来... RajithPemabandu,谢谢,我现在就去看看。 ScaryWombat,我对 Java 还比较陌生,因此我不知道调试器是如何工作的。
  • 如果您使用 NetBeans 或 Eclipse 或其他 IDE,您将有一个可以使用的调试器。基本上,不是“运行”程序,而是“在调试模式下运行”,您可以通过单击要停止的行号来设置断点。调试器将运行您的程序并在您设置的断点处停止。您可以使用断点检查变量的值,并且可以一次将调试器单步执行一行,以查看程序的执行情况。这非常有用。

标签: java arrays


【解决方案1】:

不应该将“k”与“subimgWidth”进行比较,将“l”与“subimgHeight”进行比较吗?

此外,不要将像素与“==”进行比较,而是将它们与“.equals”或“.compareTo”进行比较,就像在另一个 if 语句中所做的那样。


算法还有其他一些问题。我用一些示例输入对此进行了测试,它似乎运行良好:

   public int matchPattern(Image subimage, Image image)
   {
      int numOfMatches = 0;

      int imgWidth = image.getWidth();
      int imgHeight = image.getHeight();

      int subimgWidth = subimage.getWidth();
      int subimgHeight = subimage.getHeight();

      if (imgWidth < subimgWidth || imgHeight < subimgHeight)
      {
         return 0;
      }

      for (int i = 0; i <= imgWidth - subimgWidth; i++)
      {
         for (int j = 0; j <= imgHeight - subimgHeight; j++)
         {
            int subHeightIndex = 0;
            int subWidthIndex = 0;

            Pixel imagePix = image.getImg()[i][j];
            Pixel subimgPix = subimage.getImg()[subHeightIndex][subWidthIndex];

            if ((imagePix.compareTo(subimgPix) == 0) && ((imgWidth - j) >= subimgWidth) && ((imgHeight - i) >= subimgHeight))
            {
               boolean matchFlag = true;
               for (int k = 0; k < subimgWidth; k++)
               {
                  for (int l = 0; l < subimgHeight; l++)
                  {
                     matchFlag = 0 == (image.getImg()[i + k][j + l].compareTo(subimage.getImg()[k][l]));
                     if (!matchFlag)
                     {
                        break;
                     }
                  }
               }
               if (matchFlag)
               {
                  numOfMatches++;
               }
            }
         }
      }
      return numOfMatches;
   }

【讨论】:

  • 我修复了 if 语句中的比较,并增加了 subimgWidth 和 subimgHeight,但我仍然遇到同样的问题
  • @Mahoufo,您可能需要编辑您的问题,以便我们可以看到您的代码现在的样子。当我们只能看到过时的版本时,很难对您的代码提供帮助。
  • 试试我刚刚上传的代码。总的来说,如果您没有使用 IDE,请确保您开始使用一个。 Eclipse 是免费的。如果您已经在使用 IDE,请确保您了解如何使用调试器。使用 IDE 和调试器,您将能够在几分钟内解决此问题。在这种情况下,每个人都会这样做。
猜你喜欢
  • 2020-02-20
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 2014-10-29
  • 2018-08-06
相关资源
最近更新 更多