1

I am trying to implement a binary image matching algorithm. And I need to generate the C matrix given below. Given a small pattern image A, I need to match it to large image row-by-row and column-by-column, to find the location where that pattern matches the most.

Given M x M sized pattern A:

0 0 1 
0 1 1 
1 1 1

and N x N sized input image 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

the N x N sized output image C is the similarity of A with B at each row and column of B. Therefore 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

I am at stuck at the point where I need to compare the matrix A with B. I have made them as 2D arrays.

This is what I have done so far

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++;
                    }
                }
            }
}
Wajeeha
  • 13
  • 8
  • Do you already know the algorithm? If no, then it's not a C++ question. If yes, what have you tried so far? Where's your code? – Christian Hackl Apr 16 '17 at 11:31
  • 1
    No. I don't know what approach to use. I am trying to use 3 or 4 nested for loops in order to obtain C matrix in C++ – Wajeeha Apr 16 '17 at 11:50
  • This looks like the job of the XOR operator applied to a suitably transformed form of your input. – Bathsheba Apr 16 '17 at 11:58
  • I first need to do it using for loop and then use XOR and optimize it using bit hacks. – Wajeeha Apr 16 '17 at 12:00
  • Sadly I don't have time to delve deeply. But I think you need to store the input twice in contiguous blocks: one row-wise the other column-wise. I think you can arrange things so the actual traversal is O(N). – Bathsheba Apr 16 '17 at 12:02

2 Answers2

0

whats your code or algorithm? You've 9 in the matrix, so

0 0 1
0 1 1 
1 1 1

matches exactly. There's the coordinate pair you must be searching for.

b.g.
  • 847
  • 8
  • 14
0

    
    typedef int** matrix;

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

    // M &lt= 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 &lt= N-M ; ++i)// iterate through the first elements of every    
      for(int j=0; j &lt= N-M ; ++j)// MxM submatrix
      {    
          int match_count = 0;

          for(int k=0 ; k &lt M ; ++k)//iterate through the submatrix
          for(int l=0 ; l &lt 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 )

    }


    

JPX
  • 93
  • 1
  • 4
  • maybe add a unit test that verifies the above code - that would help to explain the intricate details of the code – serup Apr 21 '22 at 08:26