From 9ba5ce73d195d3b9e167e95fe7559a2c361639fc Mon Sep 17 00:00:00 2001 From: shinieaggarwal72 Date: Sun, 25 Jan 2026 16:11:44 +0530 Subject: [PATCH] Add gamma correction problem --- questions/190_gamma_correction/description.md | 23 ++++ questions/190_gamma_correction/example.json | 8 ++ questions/190_gamma_correction/learn.md | 115 ++++++++++++++++++ questions/190_gamma_correction/meta.json | 16 +++ questions/190_gamma_correction/solution.py | 26 ++++ .../190_gamma_correction/starter_code.py | 14 +++ questions/190_gamma_correction/tests.json | 14 +++ 7 files changed, 216 insertions(+) create mode 100644 questions/190_gamma_correction/description.md create mode 100644 questions/190_gamma_correction/example.json create mode 100644 questions/190_gamma_correction/learn.md create mode 100644 questions/190_gamma_correction/meta.json create mode 100644 questions/190_gamma_correction/solution.py create mode 100644 questions/190_gamma_correction/starter_code.py create mode 100644 questions/190_gamma_correction/tests.json diff --git a/questions/190_gamma_correction/description.md b/questions/190_gamma_correction/description.md new file mode 100644 index 00000000..95aa508d --- /dev/null +++ b/questions/190_gamma_correction/description.md @@ -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 + diff --git a/questions/190_gamma_correction/example.json b/questions/190_gamma_correction/example.json new file mode 100644 index 00000000..34abc3c8 --- /dev/null +++ b/questions/190_gamma_correction/example.json @@ -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." +} diff --git a/questions/190_gamma_correction/learn.md b/questions/190_gamma_correction/learn.md new file mode 100644 index 00000000..e149965d --- /dev/null +++ b/questions/190_gamma_correction/learn.md @@ -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: + diff --git a/questions/190_gamma_correction/meta.json b/questions/190_gamma_correction/meta.json new file mode 100644 index 00000000..5c30e44e --- /dev/null +++ b/questions/190_gamma_correction/meta.json @@ -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" + } + ] + +} diff --git a/questions/190_gamma_correction/solution.py b/questions/190_gamma_correction/solution.py new file mode 100644 index 00000000..f578f743 --- /dev/null +++ b/questions/190_gamma_correction/solution.py @@ -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 diff --git a/questions/190_gamma_correction/starter_code.py b/questions/190_gamma_correction/starter_code.py new file mode 100644 index 00000000..00af9eeb --- /dev/null +++ b/questions/190_gamma_correction/starter_code.py @@ -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 + diff --git a/questions/190_gamma_correction/tests.json b/questions/190_gamma_correction/tests.json new file mode 100644 index 00000000..cf7707a5 --- /dev/null +++ b/questions/190_gamma_correction/tests.json @@ -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" + } +]