如何对矩阵中的每个值与其在 C 中的周围值进行平均 [关闭]

问题描述 投票:0回答:1

我有一个问题,我打算编写一个程序,该程序采用矩阵/二维数组的元素并将其与周围的值进行平均。

例如矩阵:

1  2  3  4

6  7  8  9

10 11 12 13

会变成:

4  4  5  6 

6  6  7  8 

8  9  10 10 

值 1 与 2,6,7 的平均值等于 4.

我的代码需要处理用户输入给出的任何大小的数组,这使得它变得更加困难。

我完全是 C 语言编码的初学者,所以我将不胜感激 :)

我知道如何计算内部值 (7,8) 的平均值,因为它们周围有 8 个值。我的问题是编写代码来计算外部元素的平均值。

我创建的代码在这里:

#include <stdio.h>

int main() {
int numRow, numCol, rows, cols;
// Collect the height from the user

printf("Enter image height: ");
scanf("%i", &numRow);
// Collect the width from the user

printf("Enter image width: ");
scanf("%i", &numCol);
// Create the input array

int nMatrix[numRow][numCol];
// Initialize the array with image data from user input


for (rows=0; rows<numRow; rows++){
    printf("Enter pixel values for row %i (space separated): ", rows);
    for(cols=0; cols<numCol; cols++){
        scanf("%i", &nMatrix[rows][cols]);
    }
}

printf("\nMatrix:\n");
for (rows = 0; rows<numRow; rows++){
    for (cols = 0; cols<numCol; cols++){
        printf("%i ",nMatrix[rows][cols]);
    }
    printf("\n");
}

// Create the output array */
int bMatrix[numRow][numCol];

// Perfrom the blurring algorithm
int sum = 0, count = 0;
    for (rows = 0; rows < numRow; rows++){
        for (cols=0; cols < numCol; cols++){
            if (rows-1 >= 0 && cols-1 >= 0){
                sum = sum + nMatrix[rows-1][cols-1];  
                count = count +1;
            }
            else if (rows-1>=0){
                sum = sum + nMatrix[rows-1][cols];
                count = count + 1;
            }
            else if (rows-1 >= 0 && cols + 1 <= numCol){
                sum = sum + nMatrix[rows-1][cols+1];
                count = count + 1;
            }
            else if (cols -1 >=0){
                sum = sum + nMatrix[rows][cols-1];
                count = count + 1;
            }
            else if (cols -1 >= 0 && rows -1 >= 0){
                sum = sum + nMatrix[rows-1][cols-1];
                count = count + 1;
            }
            else if (cols -1 >=0 && rows + 1 <= numRow){
                sum = sum + nMatrix[rows+1][cols-1];
                count = count + 1;
            }
            else if (cols + 1 <numCol && rows + 1 <= numRow){
                sum = sum + nMatrix[rows+1][cols+1];
                count = count + 1;
            }
            else if (cols + 1 <= numCol){
                sum = sum + nMatrix[rows][cols+1];
                count = count + 1;
            }
            else if (cols + 1 <= numCol && rows + 1 <= numRow && cols -1 >=0 && rows-1 >= 0){
                bMatrix[rows][cols] = (nMatrix[rows][cols] + nMatrix[rows-1][cols-1] + nMatrix[rows+1][cols+1]+ nMatrix[rows-1][cols+1] +nMatrix[rows+1][cols-1] + nMatrix[rows+1][cols]+nMatrix[rows-1][cols]+nMatrix[rows][cols+1]+nMatrix[rows][cols-1])/9;
            }
        int avg = (sum / count);
        bMatrix[rows][cols] = avg;
        }
    }

printf("\nBlurred Image\n");
    for(rows = 0; rows < numRow; rows++){
        for(cols = 0; cols < numCol; cols++){
            printf("%i ", bMatrix[rows][cols]);
  }
printf("\n");
}
// Display the blurred image data

return 0;

}

c matrix average
1个回答
0
投票

为了避免 VLA,我将使用一维数组而不是二维数组。您需要检查邻居是否不在我们数组的范围之外,对值求和并记住邻居的数量。

static inline int getV(size_t cols, size_t row, size_t col, int *matrix)
{
    return matrix[col + row * cols];
}

static inline void setV(size_t cols, size_t row, size_t col, int *matrix, int val)
{
    matrix[col + row * cols] = val;
}

int *average(size_t rows, size_t cols, int *matrix)
{
    int *result = malloc(rows * cols * sizeof(*result));
    if(result)
    {
        for(size_t row = 0; row < rows; row++)
        {
            for(size_t col = 0; col < cols; col++)
            {
                long long sum = 0;
                int numN = 0;
                if(row) { sum += getV(cols, row - 1, col, matrix); numN++;}
                if(row < rows - 1) { sum += getV(cols, row + 1, col, matrix); numN++;}
                if(col) { sum += getV(cols, row, col - 1, matrix); numN++;}
                if(col < cols - 1) { sum += getV(cols, row, col + 1, matrix); numN++;}
                if(col < cols - 1 && row < rows - 1) { sum += getV(cols, row + 1, col + 1, matrix); numN++;}
                if(col < cols - 1 && row) { sum += getV(cols, row - 1, col + 1, matrix); numN++;}
                if(col && row < rows - 1) { sum += getV(cols, row + 1, col - 1, matrix); numN++;}
                if(col && row) { sum += getV(cols, row - 1, col - 1, matrix); numN++;}
                sum += getV(cols, row, col, matrix);
                setV(cols, row, col, result, sum / (numN + 1));
            }
        }
        memcpy(matrix, result, rows * cols * sizeof(*result));
        free(result);
    }
    return matrix;
}

void print(size_t rows, size_t cols, int *matrix)
{
    for(size_t row = 0; row < rows; row++)
    {
        for(size_t col = 0; col < cols; col++)
        {
            printf("%4d ", getV(cols, row, col, matrix));
        }
        printf("\n");
    }
}


int main(void)
{
    int matr[4 *4] = {
        1,  2,  3,  4,
        6,  7,  8,  9,
        10, 11, 12, 13,
    };
    average(4,4,matr);
    print(4,4,matr);
}
© www.soinside.com 2019 - 2024. All rights reserved.