0

我正在尝试实现二进制图像匹配算法。我需要生成下面给出的 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 x 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++;
                    }
                }
            }
}
4

2 回答 2

0

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

0 0 1
0 1 1 
1 1 1

完全匹配。这是您必须搜索的坐标对。

于 2017-04-16T11:42:59.930 回答
0
    
    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 )

    }


    

于 2017-04-16T13:09:34.990 回答