मध्यम 0 हल किए गए 0% स्वीकृति

Box Blur

Blur a grayscale image by replacing each pixel with the rounded average of its 3 by 3 neighborhood.

आप क्या सीखेंगे

  • Visit the 3 by 3 neighbors of a grid cell
  • Skip neighbors outside the grid
  • Write results to a new grid so originals are not overwritten

समस्या

अनुवाद दिखाएँ

A box blur softens an image by replacing every pixel with the average of itself and its neighbors. Here the image is a grid of brightness values from 0 to 255. For each pixel, take the 3 by 3 block centered on it and average the pixels of that block that lie inside the image: 9 pixels in the middle, 6 along an edge, 4 in a corner (fewer for images only 1 row or column wide). Always average the original values, never values you have already blurred.

Round the average to the nearest whole number with halves rounded up. With integers only, for a block with sum s and count c that is (2 × s + c) / (2 × c) with integer division.

Implement box_blur(pixels).

Input

pixels: The image, one list per row. All rows have the same length.

Output

Return the blurred image as a grid of the same size. An empty image gives an empty grid.

सीमाएँ

0 ≤ rows ≤ 50, 1 ≤ pixels per row ≤ 50 when there is at least one row

0 ≤ each pixel ≤ 255

उदाहरण

उदाहरण 1
इनपुट:
pixels = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
]

अपेक्षित आउटपुट:
expected = [
    [30, 35, 40],
    [45, 50, 55],
    [60, 65, 70]
]
इनपुट:
std::vector<std::vector<long long>> pixels = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {
    {30, 35, 40},
    {45, 50, 55},
    {60, 65, 70}
};
इनपुट:
long[][] pixels = new long[][] {
    new long[] {10, 20, 30},
    new long[] {40, 50, 60},
    new long[] {70, 80, 90}
};

अपेक्षित आउटपुट:
long[][] expected = new long[][] {
    new long[] {30, 35, 40},
    new long[] {45, 50, 55},
    new long[] {60, 65, 70}
};

व्याख्या

The corner 10 averages 10, 20, 40, 50 to 30. The center averages all nine values to 50. The top middle averages 10, 20, 30, 40, 50, 60 to 35.

उदाहरण 2
इनपुट:
pixels = [
    [0, 255]
]

अपेक्षित आउटपुट:
expected = [
    [128, 128]
]
इनपुट:
std::vector<std::vector<long long>> pixels = {
    {0, 255}
};

अपेक्षित आउटपुट:
std::vector<std::vector<long long>> expected = {
    {128, 128}
};
इनपुट:
long[][] pixels = new long[][] {
    new long[] {0, 255}
};

अपेक्षित आउटपुट:
long[][] expected = new long[][] {
    new long[] {128, 128}
};

व्याख्या

Each pixel's block holds both pixels, and (0 + 255) / 2 = 127.5 rounds up to 128, so [[128, 128]].

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Loop over row and column offsets from −1 to 1 around each pixel.

संकेत 2

Count how many neighbors are inside the grid, because edges and corners have fewer.

संकेत 3

Read from the original grid and write to a separate result grid.

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।