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





  • 相关阅读:
    cocos2d-x simpleGame 0
    cocos2d-x 下的HelloWorld
    cocos2d-x windows 配置
    算术入门之加减乘除
    计算摄氏温度
    输出倒三角图案
    厘米换算英尺英寸
    多文件模块的学生信息库系统
    GPS数据处理
    单词长度
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7430241.html
Copyright © 2011-2022 走看看