繁体   English   中英

如何在C ++中比较两个不同大小的2D数组?

[英]How to compare two 2D arrays of different sizes in C++?

我正在尝试实现二进制图像匹配算法。 我需要生成下面给出的C矩阵。 给定较小的图案图像A,我需要将其与大图像逐行和逐列匹配,以找到该图案最匹配的位置。

给定M x M尺寸的图案A:

0 0 1 
0 1 1 
1 1 1

和N x N大小的输入图像B:

0 0 0 0 0 1 0 0 
0 0 0 0 1 1 0 0 
0 0 0 1 1 1 0 0 
0 0 0 0 0 0 0 0 
1 1 1 0 0 0 1 0 
0 0 0 0 0 0 1 0 
0 0 0 1 1 1 1 0 
0 0 0 0 0 0 0 0

N×N大小的输出图像C是A在B的每一行和每一列与B的相似度。因此C:

0 0 0 0 0 0 0 0
0 3 4 6 9 4 2 0
0 3 4 6 4 1 1 0
0 6 6 4 2 2 3 0
0 4 3 2 3 5 5 0
0 2 2 4 6 8 5 0
0 3 4 5 4 5 2 0
0 0 0 0 0 0 0 0

我处于需要比较矩阵A和B的问题上。我将它们制成2D数组。

这是我到目前为止所做的

for (int i = 0;  i <3 ; i++)
    {
        for (int j = 0; j<3; j++)
        {

            for (int k = 0; k < 8; i++)
            {    
                for (int s = 0; s < 8; j++)
                {
                    if (A[i][j] == B[k][s])
                    {
                       count++;
                    }
                }
            }
}

您的代码或算法是什么? 矩阵中有9个,所以

0 0 1
0 1 1 
1 1 1

完全匹配。 您必须在寻找坐标对。


    typedef int** matrix;

    struct position
    {
       int x;
       int y;
       position(int _x,int _y){ x = _x ; y= _y; }
    }

    // M <= N
    //checks all MxM submatrices  in NxN matrix B 
    //and compares them with NxN matrix A
    position f(matrix A, int M , matrix B , int N)
    { 
      int best_match_first_row     = -1;
      int best_match_first_column  = -1;
      int best_match_counted = -1;

      for(int i=0; i <= N-M ; ++i)// iterate through the first elements of every    
      for(int j=0; j <= N-M ; ++j)// MxM submatrix
      {    
          int match_count = 0;

          for(int k=0 ; k < M ; ++k)//iterate through the submatrix
          for(int l=0 ; l < M ; ++l)//and compare elements with matrix A
                 if( A[k][l] == B[i+k][j+l] ) ++match_count; //count if it matches

          if(match_count > best_match_counted) //if we have a better match than before
          {
                  best_match_counted = match_count; //store the new count as best.
                  best_match_first_row     = i;     //store the position of the
                  best_match_first_column  = j;     //first element in the submatrix  
          }

      }
      //returns the position of the first element of the most matching submatrix
       return position( best_match_first_row , best_match_first_column )

    }


    

暂无
暂无

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

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