티스토리 뷰

Codesignal

code signal - 23. boxBlur

터보건 2020. 2. 20. 20:15

Question

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.


Example

For

image = [[1, 1, 1], 
         [1, 7, 1], 
         [1, 1, 1]]

the output should be boxBlur(image) = [[1]].

To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.

For

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

the output should be

boxBlur(image) = [[5, 4], 
                  [4, 4]]

There are four 3 × 3 squares in the input image, so there should be four integers in the blurred output. To get the first value: (7 + 4 + 0 + 5 + 6 + 2 + 6 + 10 + 7) = 47 / 9 = 5.2222 = 5. The other three integers are obtained the same way, then the surrounding integers are cropped from the final result.


Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] array.array.integer image

    An image, stored as a rectangular matrix of non-negative integers.

    Guaranteed constraints: 3 ≤ image.length ≤ 100, 3 ≤ image[0].length ≤ 100, 0 ≤ image[i][j] ≤ 255.

  • [output] array.array.integer

    • A blurred image represented as integers, obtained through the process in the description.

MY_ANSWER

def boxBlur(image):
    row = len(image) -2
    column = len(image[0]) -2
    answer = [[0 for i in range(column)] for j in range(row)]

    for i in range(row):
        for j in range(column):
            answer[i][j] = sum(image[i][j:j+3] + image[i+1][j:j+3] + image[i+2][j:j+3]) // 9

    return answer
  • 3x3 matrix로 input matrix를 stride하는 과정, filter는 없지만 stride하면서 input matrix에서 3x3 크기만큼의 평균값을 출력한다.

  • answer는 기존 matrix보다 행과 열이 2개씩 작아지는 것을 고려하여 빈 리스트를 만들어준다.

    평균을 구하는 과정을 for문으로 반복한다.


Best_ANSWER

def boxBlur(image):
    #print ([[x[i:i+3] for x in image[j:j+3] for i in range(len(image[j])-2)] for j in range(len(image)-2)])
    
    return [[int(sum(sum(x[i:i+3]) for x in image[j:j+3])/9) for i in range(len(image[j])-2)] for j in range(len(image)-2)]
    
  • 빈 리스트를 만드는 과정을 list comprehension으로 간략하게 표현하였다. 아직 실력이 부족해서 한번에 파악하기가 어렵다....

 

'Codesignal' 카테고리의 다른 글

code signal - 25. arrayReplace  (0) 2020.02.23
code signal - 24. Minesweeper  (0) 2020.02.22
code signal - 22. avoidObstacles  (0) 2020.02.20
code signal - 21. isIPv4Address  (0) 2020.02.19
code signal - 20. arrayMaximalAdjacentDifference  (0) 2020.02.19
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함