Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions questions/190_gamma_correction/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Problem

You are given a grayscale image represented as a 2D matrix and a gamma value.

Each pixel ranges from 0 to 255.

Apply gamma correction using:

new_pixel = 255 * (pixel / 255) ^ gamma

Round to nearest integer.

Return the corrected image.

### Edge Cases

Return -1 if:

- Image is empty
- Rows are inconsistent
- Pixel values are invalid
- Gamma <= 0

8 changes: 8 additions & 0 deletions questions/190_gamma_correction/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"input": {
"img": [[0, 128], [192, 255]],
"gamma": 2.0
},
"output": [[0, 64], [144, 255]],
"reasoning": "Each pixel is transformed using the gamma correction formula."
}
115 changes: 115 additions & 0 deletions questions/190_gamma_correction/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
## Solution Explanation

In this problem, we are given a grayscale image represented as a 2D matrix and a positive real number `gamma`.
Each value in the matrix represents a pixel intensity between `0` (black) and `255` (white).

Our goal is to apply **gamma correction** to every pixel in the image and return the corrected image.

---

### Intuition

Human eyes do not perceive brightness linearly.
Gamma correction is used to adjust image brightness in a nonlinear way so that images look more natural.

- If `gamma > 1`, the image becomes darker.
- If `gamma < 1`, the image becomes brighter.

Each pixel is transformed independently using a mathematical formula.

---

### Mathematical Formula

For each pixel value `p`, the corrected value is computed as:

$$
new\_pixel = 255 \times \left(\frac{p}{255}\right)^\gamma
$$

Where:

- `p` is the original pixel value
- `gamma` is the given correction value
- `new_pixel` is the transformed pixel value

After computing this value:

1. Round it to the nearest integer.
2. Clip it to the range `[0, 255]`.

---

### Step-by-Step Approach

We follow these steps:

#### 1. Validate Input

Before processing, we check:

- The image is not empty.
- All rows have the same length.
- All pixel values are in `[0, 255]`.
- `gamma > 0`.

If any condition fails, return `-1`.

---

#### 2. Normalize Pixel Values

Each pixel `p` is first converted to the range `[0, 1]`:

$$
normalized = \frac{p}{255}
$$

This makes the power operation mathematically stable.

---

#### 3. Apply Gamma Correction

We raise the normalized value to the power `gamma`:

$$
corrected = normalized^\gamma
$$

Then scale it back to `[0, 255]`:

note: maybe not this
$$
scaled = 255 \times corrected
$$

---

#### 4. Round and Clip

The scaled value is:

- Rounded to the nearest integer
- Clipped so that:

$$
0 \le new\_pixel \le 255
$$

This ensures valid pixel values.

---

#### 5. Build Output Image

We repeat the above steps for every pixel and store the results in a new 2D matrix.

This matrix is returned as the final output.

---

### Example Walkthrough

Consider the input:

16 changes: 16 additions & 0 deletions questions/190_gamma_correction/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "190",
"title": "GAMMA CORRECTION",
"difficulty": "easy",
"category": "Computer Vision",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/shinieaggarwal72",
"name": "Shinie"
}
]

}
26 changes: 26 additions & 0 deletions questions/190_gamma_correction/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def apply_gamma_correction(img, gamma):

if not img or gamma <= 0:
return -1

row_len = len(img[0])

for row in img:
if len(row) != row_len:
return -1
for p in row:
if p < 0 or p > 255:
return -1

result = []

for row in img:
new_row = []
for p in row:
val = 255 * ((p / 255) ** gamma)
val = round(val)
val = min(255, max(0, val))
new_row.append(val)
result.append(new_row)

return result
14 changes: 14 additions & 0 deletions questions/190_gamma_correction/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Implement your function below.

def apply_gamma_correction(img, gamma):
"""
Args:
img (List[List[int]])
gamma (float)

Returns:
List[List[int]] or -1
"""
# TODO: Implement
pass

14 changes: 14 additions & 0 deletions questions/190_gamma_correction/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"test": "apply_gamma_correction([[0,128],[192,255]],2.0)",
"expected_output": "[[0,64],[144,255]]"
},
{
"test": "apply_gamma_correction([],2.0)",
"expected_output": "-1"
},
{
"test": "apply_gamma_correction([[300]],1.5)",
"expected_output": "-1"
}
]