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.

    就是给二维数组做8方向均值平缓

    class Solution(object):
        def imageSmoother(self, M):
            """
            :type M: List[List[int]]
            :rtype: List[List[int]]
            """
            dires = [[-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1]]
            matrix = []
            n = len(M)
            m = len(M[0])
            for i in range(n):
                matrix.append([0] * m)
            for i in range(n):
                for j in range(m):
                    cnt = 1
                    total_sum = M[i][j]
                    for dire in dires:
                        new_i = i + dire[0]
                        new_j = j + dire[1]
                        if new_i >= 0 and new_i < n and new_j >= 0 and new_j < m:
                            cnt += 1
                            total_sum += M[new_i][new_j]
                    matrix[i][j] = int(floor(total_sum / cnt))
            return matrix
                    
  • 相关阅读:
    github的使用
    QPalette的用法
    QTimer的用法
    QStatusBar的用法
    QWhatsThis的用法
    QString::​arg的用法
    qt中ui的 使用介绍
    安全协议IPSEC
    安全协议ssl
    对称加密和非对称加密
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13260285.html
Copyright © 2011-2022 走看看