0

我有一个方阵,我需要在每个矩阵位置的邻居之间求平均值,每个矩阵位置的所有邻居都在矩阵本身内。

用一个例子来解释,如果我有一个这样的矩阵:

10 11  1  4
 5  1  6  9
 9  0  2  7
 7  4  9  8

仅在 (2,2)、(2,3)、(3,2) 和 (3,3) 位置计算平均值。

在这些操作之后,最终的矩阵将如下所列:

5.00  4.56
4.78  5.11

对不起,我的解释很混乱,我是 C 语言的初学者,我在做这个算法时遇到了很多困难。

我将感谢所有可以向我解释并向我展示解决方案的人。

4

2 回答 2

0

Since there is no code you show I guess your problem is in the creation of the algorithm itself rather than C.

I would suggest to 1. have a function which averages numbers in a 3x3 matrix. 2. make a loop which goes through your matrix applying this function to appropriate chunks.

Each of these steps is not hard to accomplish and I'm sure you'll find lots of code online which will help you.

于 2013-04-11T23:52:42.683 回答
0

NOTE: It is recommended that you show what you have tried or attempted so far. Usually, a generic open question without any prior attempt gets down-voted. Since, you are beginner and am a lover of image processing, I couldn't help writing this code and posting this solution to help you get started.

As Paulpro commented, your problem is a low-pass filter implementation. In a given data matrix, you will have to find out the points which could be averaged through a 3 x 3 window. Hence, first I will explain how to identify these points. Please find a sample code with comments below which will explain the identification of these points.

// Input Data
int input_array[4][4] = { {10, 11, 1, 4}, {5, 1, 6, 9}, {9, 0, 2, 7}, {7, 4, 9, 8}};

// To calculate the average 
float sum ;

// Row counter, column counter, dimension of the square matrix
int row_ctr, col_ctr, array_dimension;
int row, col;

array_dimension = 4; // Initialization

// Run the loop for the entire array    
for(row_ctr = 0; row_ctr < array_dimension; row_ctr++)
{
    // If the current row's top neighbor is outside the boundary or
    // the bottom neighbor is outside the boundary, PASS and continue with
    // next row
    if(((row_ctr - 1) < 0) || ((row_ctr + 1) >= array_dimension))
    {
        continue;
    }
    // Run the loop for array dimension
    for(col_ctr = 0; col_ctr < array_dimension; col_ctr++)
    {
         // If the current column's left neighbor is outside the boundary or
         // right neighbor is outside the boundary, PASS and continue with
         // next column
         if(((col_ctr - 1) < 0) || ((col_ctr + 1) >= array_dimension))
         {
              continue;
         }
         // Initialize sum to 0.0
         sum = 0.0;
         // Reset to Top Left corner by going (-1, -1) from current position
         row = row_ctr - 1; 
         col = col_ctr - 1;

         sum = input_array[(row+0)][col] + 
                 input_array[(row+0)][(col+1)]+ input_array[(row+0)][(col+2)];
         sum += input_array[(row+1)][col] + 
                  input_array[(row+1)][(col+1)]+ input_array[(row+1)][(col+2)];
         sum += input_array[(row+2)][col] + 
                  input_array[(row+2)][(col+1)]+ input_array[(row+2)][(col+2)];

         // Find the average    
         sum = sum / 9.0;
         printf("Average being found for (%d, %d) is %6.2f\n", row_ctr, col_ctr, sum);
     }
}

When this code is executed the output would be displayed as

Average being found for (1, 1) is   5.00
Average being found for (1, 2) is   4.56
Average being found for (2, 1) is   4.78
Average being found for (2, 2) is   5.11
于 2013-04-11T23:53:36.027 回答