😎풀이

  1. rows: 행의 높이
  2. cols: 열의 길이
  3. smoother: 부드럽게 변형된 img
  4. 2중 반복하며 주변 셀의 값을 더한다.
    4-1. activeCellSum: 주변 활성화 된 셀의 합
    4-2. activeCellCount: 주변 활성화 된 셀의 수
    4-3. 주변 합 / 주변 수를 통해 현재 셀의 smooth 값을 확인
  5. 부드럽게 변형된 img 반환
function imageSmoother(img: number[][]): number[][] {
    const rows = img.length
    const cols = img[0].length
    const smoother = Array.from({length: rows}, () => Array(cols).fill(0))
    for(let row = 0; row < rows; row++) {
        for(let col = 0; col < cols; col++) {
            let activeCellSum = 0
            let activeCellCount = 0
            if(row - 1 >= 0) {
                if(col - 1 >= 0) {
                    activeCellSum += img[row - 1][col - 1]
                    activeCellCount++
                }
                activeCellSum += img[row - 1][col]
                activeCellCount++
                if(col + 1 < cols) {
                    activeCellSum += img[row - 1][col + 1]
                    activeCellCount++
                }
            }
            if(col - 1 >= 0) {
                activeCellSum += img[row][col - 1]
                activeCellCount++
            }
            activeCellSum += img[row][col]
            activeCellCount++
            if(col + 1 < cols) {
                activeCellSum += img[row][col + 1]
                activeCellCount++
            }
            if(row + 1 < rows) {
                if(col - 1 >= 0) {
                    activeCellSum += img[row + 1][col - 1]
                    activeCellCount++
                }
                activeCellSum += img[row + 1][col]
                activeCellCount++
                if(col + 1 < cols) {
                    activeCellSum += img[row + 1][col + 1]
                    activeCellCount++
                }
            } 
            smoother[row][col] = Math.floor(activeCellSum / activeCellCount)
        }
    }
    return smoother
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글