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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) { int n = img.size(), m = img[0].size(); vector<vector<int>> res(n,vector<int>(m)); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { int sum = 0, cnt = 0; for(int y = -1; y <= 1; y++) for(int x = -1; x <= 1; x++) { if(0 <= i + y and i + y < n and0 <= j + x and j + x < m) { sum += img[i+y][j+x]; cnt += 1; } } res[i][j] = sum / cnt; } return res; } };