모든 챌린지
어려움 0명 해결 정답률 0%

Edge Detection (Sobel Filter)

Run the Sobel filter over a grayscale pixel grid and return the edge strength of every pixel.

배우게 될 내용

  • Visit the 3 × 3 neighborhood of every cell
  • Handle border pixels by clamping coordinates
  • Write results to a new grid so inputs are not overwritten

문제

번역본 보기

A photo editing lab asks you to highlight edges in a grayscale picture. Each pixel is a brightness from 0 (black) to 255 (white). For every pixel, look at its 3 × 3 neighborhood and compute two weighted sums with the Sobel kernels:

Gx = -1  0  1      Gy = -1 -2 -1
     -2  0  2            0  0  0
     -1  0  1            1  2  1

The kernel's center sits on the pixel, its top row on the row above and its left column on the column to the left. Multiply each neighbor by the matching weight and add them up to get gx and gy. When a neighbor falls outside the picture, use the nearest pixel on the border instead (clamp the row to 0 … rows − 1 and the column to 0 … cols − 1). The edge strength is min(255, |gx| + |gy|), so it stays a whole number between 0 and 255. Always read from the original picture, never from pixels you already changed.

Implement edge_detection(image).

Input

image: The picture as a list of rows; every row has the same length.

Output

Return a new grid of the same size where each value is the edge strength of that pixel.

제약 조건

1 ≤ rows, cols ≤ 50

0 ≤ pixel ≤ 255

예시

예시 1
입력:
image = [
    [10, 10, 10],
    [10, 10, 10],
    [10, 10, 10]
]

예상 출력:
expected = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
]
입력:
std::vector<std::vector<long long>> image = {
    {10, 10, 10},
    {10, 10, 10},
    {10, 10, 10}
};

예상 출력:
std::vector<std::vector<long long>> expected = {
    {0, 0, 0},
    {0, 0, 0},
    {0, 0, 0}
};
입력:
long[][] image = new long[][] {
    new long[] {10, 10, 10},
    new long[] {10, 10, 10},
    new long[] {10, 10, 10}
};

예상 출력:
long[][] expected = new long[][] {
    new long[] {0, 0, 0},
    new long[] {0, 0, 0},
    new long[] {0, 0, 0}
};

설명

Every neighborhood is flat, so both sums are 0 everywhere and the result is all zeros.

예시 2
입력:
image = [
    [0, 0, 100],
    [0, 0, 100],
    [0, 0, 100]
]

예상 출력:
expected = [
    [0, 255, 255],
    [0, 255, 255],
    [0, 255, 255]
]
입력:
std::vector<std::vector<long long>> image = {
    {0, 0, 100},
    {0, 0, 100},
    {0, 0, 100}
};

예상 출력:
std::vector<std::vector<long long>> expected = {
    {0, 255, 255},
    {0, 255, 255},
    {0, 255, 255}
};
입력:
long[][] image = new long[][] {
    new long[] {0, 0, 100},
    new long[] {0, 0, 100},
    new long[] {0, 0, 100}
};

예상 출력:
long[][] expected = new long[][] {
    new long[] {0, 255, 255},
    new long[] {0, 255, 255},
    new long[] {0, 255, 255}
};

설명

For the middle pixel the right column adds 100 + 200 + 100 = 400 and the left column adds 0, so gx = 400, gy = 0 and the strength is capped at 255. The right column pixels also get 255 because their missing right neighbors copy the border value 100. The left column pixels only see 0 on both sides, so they stay 0.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Write a helper that returns the pixel at (r, c) after clamping r and c into the grid.

힌트 2

gx only uses the left and right columns of the neighborhood; gy only the top and bottom rows.

힌트 3

Build the output in a separate grid; changing the input as you go breaks later pixels.

예상 복잡도

O(rows × cols) time, O(rows × cols) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.