すべてのチャレンジ
難しい 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 つずつ開いてみましょう。

ヒント 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 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。