zoukankan      html  css  js  c++  java
  • 661. Image Smoother 图像平滑化


    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

    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:

    1. The value in the given matrix is in the range of [0, 255].
    2. The length and width of the given matrix are in the range of [1, 150].

    给定一个表示图像灰度的二维整数矩阵M,您需要设计一个更平滑的方式,使每个单元格的灰度值成为所有8个周围单元格本身的平均灰度(舍入)。如果一个单元格具有小于8个周围的单元格,那么可以使用尽可能多的单元格。

    1. class Solution(object):
    2. def imageSmoother(self, M):
    3. import math
    4. result = []
    5. for i in range(len(M)):
    6. row = []
    7. for j in range(len(M[i])):
    8. pos = [
    9. [i - 1, j - 1], [i - 1, j], [i - 1, j + 1],
    10. [i, j - 1], [i, j], [i, j + 1],
    11. [i + 1, j - 1], [i + 1, j], [i + 1, j + 1]
    12. ]
    13. near = []
    14. for item in pos:
    15. if item[0] >= 0 and item[0] < len(M) and item[1] >= 0 and item[1] < len(M[i]):
    16. near.append(M[item[0]][item[1]])
    17. row.append(int(sum(near) / len(near)))
    18. result.append(row)
    19. return result





  • 相关阅读:
    bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    bzoj1016: [JSOI2008]最小生成树计数
    bzoj1051: [HAOI2006]受欢迎的牛
    bzoj1003: [ZJOI2006]物流运输
    bzoj1079: [SCOI2008]着色方案
    bzoj1179: [Apio2009]Atm
    bzoj1877: [SDOI2009]晨跑
    bzoj1821: [JSOI2010]Group 部落划分 Group
    bzoj1305: [CQOI2009]dance跳舞
    bzoj1858: [Scoi2010]序列操作
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7430241.html
Copyright © 2011-2022 走看看