blob: 969b0318fc1a9c586507be944235fa0e4e6988df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// 661. Image Smoother
#include "leetcode.h"
/*
* 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 (ie. 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 (ie. 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
*/
int **imageSmoother(int **M, int MRowSize, int MColSize, int **columnSizes,
int *returnSize) {
int **ans = calloc(MColSize, sizeof(int));
*returnSize = MColSize;
*columnSizes = calloc(MColSize, sizeof(int));
for (int i = 0; i < MColSize; i++) {
(*columnSizes)[i] = MColSize;
ans[i] = calloc(MColSize, sizeof(int));
}
int cnt;
for (int i = 0; i < MRowSize; i++)
for (int j = 0; j < MColSize; j++) {
cnt = 0;
for (int x = i - 1; x <= i + 1; x++)
for (int y = j - 1; y <= j + 1; y++)
if (x >= 0 && x < MRowSize && y >= 0 && y < MColSize) {
ans[i][j] += M[x][y];
cnt++;
}
ans[i][j] /= cnt;
}
return ans;
}
|