LeetCode //C - 661. Image Smoother

661. Image Smoother

An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
在这里插入图片描述
Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.
 

Example 1:

在这里插入图片描述

Input: img = [[1,1,1],[1,0,1],[1,1,1]]
Output:[[0,0,0],[0,0,0],[0,0,0]]
Explanation:
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Example 2:

在这里插入图片描述

Input: img = [[100,200,100],[200,50,200],[100,200,100]]
Output: [[137,141,137],[141,138,141],[137,141,137]]
Explanation:
For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138

Constraints:
  • m == img.length
  • n == img[i].length
  • 1 <= m, n <= 200
  • 0 <= img[i][j] <= 255

From: LeetCode
Link: 661. Image Smoother


Solution:

Ideas:

1. Result Matrix Creation: A new matrix is created (result) that will hold the smoothed values.
2. Directions Array: The directions array helps in identifying the 8 surrounding neighbors of any given cell (top-left, top-right, bottom-left, etc.).
3. Traversing the Image: The matrix is traversed cell by cell, and for each cell, its neighbors are evaluated.
4. Summing Neighboring Cells: For each cell, the sum of the current cell and its valid neighbors is calculated.
5. Average Calculation: The average is calculated by dividing the total sum by the number of valid neighbors (including the current cell).
6. Memory Allocation: Memory is allocated dynamically for both the result and column sizes.
7. Return Values: The function returns the smoothed matrix and also updates returnSize and returnColumnSizes.

Code:
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes) {
    
    
    // Create the result matrix
    int** result = (int**)malloc(imgSize * sizeof(int*));
    *returnColumnSizes = (int*)malloc(imgSize * sizeof(int));
    *returnSize = imgSize;

    for (int i = 0; i < imgSize; i++) {
    
    
        result[i] = (int*)malloc(*imgColSize * sizeof(int));
        (*returnColumnSizes)[i] = *imgColSize;
    }

    // Directions for the 8 surrounding neighbors
    int directions[8][2] = {
    
    
        {
    
    -1, -1}, {
    
    -1, 0}, {
    
    -1, 1}, {
    
    0, -1}, {
    
    0, 1}, {
    
    1, -1}, {
    
    1, 0}, {
    
    1, 1}
    };

    // Traverse each pixel of the image
    for (int i = 0; i < imgSize; i++) {
    
    
        for (int j = 0; j < *imgColSize; j++) {
    
    
            int sum = img[i][j];
            int count = 1;

            // Check all 8 neighbors
            for (int k = 0; k < 8; k++) {
    
    
                int ni = i + directions[k][0];
                int nj = j + directions[k][1];
                if (ni >= 0 && ni < imgSize && nj >= 0 && nj < *imgColSize) {
    
    
                    sum += img[ni][nj];
                    count++;
                }
            }

            // Store the result (average rounded down)
            result[i][j] = sum / count;
        }
    }

    return result;
}