661. Image Smoother

#coding=utf-8
'''
CreMted on 2018年6月13日

@Muthor: lzs
'''
class Solution:
#98%
# 661. Image Smoother
#     包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) 
#     ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们
    def imageSmoother(self, M):
        """
        :type M: List[List[int]]
        :rtype: List[List[int]]
        """
        r_list=[]
        h=len(M)
        l=len(M[0])
        for i in range(h):
            L_ij=[]
            for j in range(l):
                
                if i+1<h and i-1>=0:
                    if j+1<l and j-1>=0:
                        sum=M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]+M[i][j-1]+M[i][j]+M[i][j+1]+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]
                        avg=(sum//9)
                    elif j+1<l and j-1<0:
                        sum=M[i-1][j]+M[i-1][j+1]+M[i][j]+M[i][j+1]+M[i+1][j]+M[i+1][j+1]
                        avg=(sum//6)
                    elif j+1>=l and j-1>=0:
                        sum=M[i-1][j-1]+M[i-1][j]+M[i][j-1]+M[i][j]+M[i+1][j-1]+M[i+1][j]
                        avg=(sum//6)
                    elif j+1>=l and j-1<0:
                        sum=M[i-1][j]+M[i][j]+M[i+1][j]
                        avg=(sum//3)
                elif i+1<h and i-1<0:
                    if j+1<l and j-1>=0:
                        sum=M[i][j-1]+M[i][j]+M[i][j+1]+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]
                        avg=(sum//6)
                    elif j+1<l and j-1<0:
                        sum=M[i][j]+M[i][j+1]+M[i+1][j]+M[i+1][j+1]
                        avg=(sum//4)
                    elif j+1>=l and j-1>=0:
                        sum=M[i][j-1]+M[i][j]+M[i+1][j-1]+M[i+1][j]
                        avg=(sum//4)
                    elif j+1>=l and j-1<0:
                        sum=M[i][j]+M[i+1][j]
                        avg=(sum//2)
                
                elif i+1>=h and i-1>=0:
                    if j+1<l and j-1>=0:
                        sum=M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]+M[i][j-1]+M[i][j]+M[i][j+1]
                        avg=(sum//6)
                    elif j+1<l and j-1<0:
                        sum=M[i-1][j]+M[i-1][j+1]+M[i][j]+M[i][j+1]
                        avg=(sum//4)
                    elif j+1>=l and j-1>=0:
                        sum=M[i-1][j-1]+M[i-1][j]+M[i][j-1]+M[i][j]
                        avg=(sum//4)
                    elif j+1>=l and j-1<0:
                        sum=M[i-1][j]+M[i][j]
                        avg=(sum//2)
                
                elif i+1>=h and i-1<=0:
                    if j+1<l and j-1>=0:
                        sum=M[i][j-1]+M[i][j]+M[i][j+1]
                        avg=sum//3
                    elif j+1<l and j-1<0:
                        sum=M[i][j]+M[i][j+1]
                        avg=sum//2
                    elif j+1>=l and j-1>=0:
                        sum=M[i][j-1]+M[i][j]
                        avg=sum//2
                    elif j+1>=l and j-1<0:
                        sum=M[i][j]
                        avg=sum//1
                L_ij.append(int(avg))
                avg=0
            r_list.append(L_ij)
        return r_list

办法有点笨,分类别,但是效率高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值