我正在尝试实现二进制图像匹配算法。我需要生成下面给出的C矩阵。 给定一个小图案图像A,我需要逐行和逐列地将它与大图像匹配,以找到该图案最匹配的位置。
给定M x M大小的模式A:
0 0 1
0 1 1
1 1 1
和N×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是B的每一行和一列中A与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++;
}
}
}
}
答案 0 :(得分:0)
您的代码或算法是什么?你在矩阵中有9个,所以
0 0 1
0 1 1
1 1 1
完全匹配。您必须搜索的坐标对。
答案 1 :(得分: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 )
}