Leetcode

661. Image Smoother

Leeter 2021. 10. 17. 19:08

Description:

An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).

'''
[1] in-place)

when smoothing matrix[0][1], if matrix[0][0] have been smoothed, the output of matrix[0][1] would be incorrect
so, It should be implemented out-place
'''

'''
[2] out-place)

output=[[0 for _ in range(len(img[0]))] for _ in range(len(img))]

for i in range(len(img)):
    for j in range(len(img[i])):
        divisor=0
        dividend=img[i][j]
        
        # consider all 8 pixels
        if i-1>=0:
            if j-1>=0:
                dividened+=img[i-1][j-1]
                divisor+=1
                
            if j+1<len(img[i-1]):
                dividend+=img[i-1][j+1]
                divisor+=1
            
            dividend+=img[i-1][j]
            divisor+=1
        
        if i+1<len(img):
            if j-1>=0:
                dividened+=img[i+1][j-1]
                divisor+=1
                
            if j+1<len(img[i+1]):
                dividend+=img[i+1][j+1]
                divisor+=1
            
            dividend+=img[i+1][j]
            divisor+=1
        
        if j-1>=0:
            dividend+=img[i][j-1]
            divisor+=1
        if j+1<len(img[i]):
            dividend+=img[i][j+1]
            divisor+=1
        
        output[i][j]=dividend//divisor
            
return output
'''

'''
[3] out-place - optimization - not implemented)

moving the smoothing filter like sliding window
then, need not to calculate all 8 pixels nearby. it means that this solution take shorter time than not optimization method.

'''
class Solution:
    def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
        output=[[0 for _ in range(len(img[0]))] for _ in range(len(img))]

        for i in range(len(img)):
            for j in range(len(img[i])):
                divisor=1
                dividendd=img[i][j]

                # consider all 8 pixels
                if i-1>=0:
                    if j-1>=0:
                        dividendd+=img[i-1][j-1]
                        divisor+=1

                    if j+1<len(img[i-1]):
                        dividendd+=img[i-1][j+1]
                        divisor+=1

                    dividendd+=img[i-1][j]
                    divisor+=1

                if i+1<len(img):
                    if j-1>=0:
                        dividendd+=img[i+1][j-1]
                        divisor+=1

                    if j+1<len(img[i+1]):
                        dividendd+=img[i+1][j+1]
                        divisor+=1

                    dividendd+=img[i+1][j]
                    divisor+=1

                if j-1>=0:
                    dividendd+=img[i][j-1]
                    divisor+=1
                if j+1<len(img[i]):
                    dividendd+=img[i][j+1]
                    divisor+=1

                output[i][j]=dividendd//divisor

        return output