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.
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)=0For the points(0,1),(1,0),(1,2),(2,1): floor(5/6)= floor(0.83333333)=0For the point(1,1): floor(8/9)= floor(0.88888889)=0
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)=137For the points(0,1),(1,0),(1,2),(2,1): floor((200+200+50+200+100+100)/6)= floor(141.666667)=141For the point(1,1): floor((50+200+200+200+200+100+100+100+100)/9)= floor(138.888889)=138
For each cell, we want the average of itself and its neighbors. We can check all 3x3 cells around each position, sum their values, count them, and take the floor of the average.
classSolution {
public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
int m = img.size(), n = img[0].size();
vector<vector<int>> ans(m, vector<int>(n));
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
int s =0, c =0;
for (int di =-1; di <=1; ++di) {
for (int dj =-1; dj <=1; ++dj) {
int ni = i + di, nj = j + dj;
if (ni >=0&& ni < m && nj >=0&& nj < n) {
s += img[ni][nj];
++c;
}
}
}
ans[i][j] = s / c;
}
}
return ans;
}
};
classSolution {
publicint[][]imageSmoother(int[][] img) {
int m = img.length, n = img[0].length;
int[][] ans =newint[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int s = 0, c = 0;
for (int di =-1; di <= 1; ++di) {
for (int dj =-1; dj <= 1; ++dj) {
int ni = i + di, nj = j + dj;
if (ni >= 0 && ni < m && nj >= 0 && nj < n) {
s += img[ni][nj];
++c;
}
}
}
ans[i][j]= s / c;
}
}
return ans;
}
}
classSolution {
funimageSmoother(img: Array<IntArray>): Array<IntArray> {
val m = img.size
val n = img[0].size
val ans = Array(m) { IntArray(n) }
for (i in0 until m) {
for (j in0 until n) {
var s = 0var c = 0for (di in -1..1) {
for (dj in -1..1) {
val ni = i + di
val nj = j + dj
if (ni in0 until m && nj in0 until n) {
s += img[ni][nj]
c++ }
}
}
ans[i][j] = s / c
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defimageSmoother(self, img: list[list[int]]) -> list[list[int]]:
m, n = len(img), len(img[0])
ans = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
s = c =0for di in [-1,0,1]:
for dj in [-1,0,1]:
ni, nj = i+di, j+dj
if0<= ni < m and0<= nj < n:
s += img[ni][nj]
c +=1 ans[i][j] = s // c
return ans
impl Solution {
pubfnimage_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let m = img.len();
let n = img[0].len();
letmut ans =vec![vec![0; n]; m];
for i in0..m {
for j in0..n {
letmut s =0;
letmut c =0;
for di in-1..=1 {
for dj in-1..=1 {
let ni = i asisize+ di;
let nj = j asisize+ dj;
if ni >=0&& ni < m asisize&& nj >=0&& nj < n asisize {
s += img[ni asusize][nj asusize];
c +=1;
}
}
}
ans[i][j] = s / c;
}
}
ans
}
}