Как сравнить два 2D-массива разных размеров в 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

выходное изображение C размера N x N — это сходство 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. Я сделал их как двумерные массивы.

Это то, что я сделал до сих пор

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

Решение

каков твой код или алгоритм? У вас 9 в матрице, так

0 0 1
0 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 )

}

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector