Example 1:
Input: [[1,1,1], [1,0,1], [1,1,1]] Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] Explanation: For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0 For the point (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
Note:
- The value in the given matrix is in the range of [0, 255].
- The length and width of the given matrix are in the range of [1, 150].
给定一个表示图像灰度的二维整数矩阵M,您需要设计一个更平滑的方式,使每个单元格的灰度值成为所有8个周围单元格本身的平均灰度(舍入)。如果一个单元格具有小于8个周围的单元格,那么可以使用尽可能多的单元格。
class Solution(object):
def imageSmoother(self, M):
import math
result = []
for i in range(len(M)):
row = []
for j in range(len(M[i])):
pos = [
[i - 1, j - 1], [i - 1, j], [i - 1, j + 1],
[i, j - 1], [i, j], [i, j + 1],
[i + 1, j - 1], [i + 1, j], [i + 1, j + 1]
]
near = []
for item in pos:
if item[0] >= 0 and item[0] < len(M) and item[1] >= 0 and item[1] < len(M[i]):
near.append(M[item[0]][item[1]])
row.append(int(sum(near) / len(near)))
result.append(row)
return result