[Python 문제풀이] 17일차

2020. 7. 25. 11:0003. Resources/Python

728x90
반응형

Day_17

Intro

Q23. Box Blur

Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.

The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.

Return the blurred image as an integer, with the fractions rounded down.

제출 코드

import numpy as np

def boxBlur(image):
    Row = len(image)
    Col = len(image[0])

    BlurSumSet = np.zeros((Row-2,Col-2))

    for i in range(1, Row-1, 1):
        for j in range(1, Col-1, 1):
            BlurSum = int((image[i-1][j-1] + image[i-1][j] + image[i-1][j+1] + image[i][j-1] + image[i][j] + image[i][j+1] + image[i+1][j-1] + image[i+1][j] + image[i+1][j+1])/9)
            BlurSumSet[i-1][j-1] = BlurSum

    return BlurSumSet

작성 흐름

import numpy as np

# test case
image = [[7, 4, 0, 1], 
         [5, 6, 2, 2], 
         [6, 10, 7, 8], 
         [1, 4, 2, 0]]

print("Row: %d" % (len(image)))
print("Col: %d" % (len(image[0])))

# 행, 열 사이즈 찾기
Row = len(image)
Col = len(image[0])

# 3x3이면 양쪽 위아래 1개씩 값을 빼야하므로, 각 행, 열 사이즈에서 2씩 뺀 빈 배열 선언
BlurSumSet = np.zeros((Row-2,Col-2))

for i in range(1, Row-1, 1):
    for j in range(1, Col-1, 1):
        print("Row: %d, Col: %d" % (i, j)) # center point 찾기
        # BlurSum 찾기
        BlurSum = int((image[i-1][j-1] + image[i-1][j] + image[i-1][j+1] + image[i][j-1] + image[i][j] + image[i][j+1] + image[i+1][j-1] + image[i+1][j] + image[i+1][j+1])/9)
        # set에 넣기
        BlurSumSet[i-1][j-1] = BlurSum
        print(BlurSum)
print(BlurSumSet)
반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 19일차 (끝)  (0) 2020.07.27
[Python 문제풀이] 18일차  (0) 2020.07.26
[Python 문제풀이] 16일차  (0) 2020.07.24
[Python 문제풀이] 15일차  (0) 2020.07.23
[Python 문제풀이] 14일차  (0) 2020.07.22