TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Histogram Equalization

Computer Vision
Medium

Histogram equalization improves image contrast by redistributing pixel intensities so they span the full [0, 255] range more uniformly. It is one of the most commonly used image enhancement techniques.

Given a 2D grayscale image with integer pixel values in [0, 255], apply histogram equalization and return the transformed image.

Algorithm

  1. Compute the histogram: count the frequency of each intensity value (0 through 255).
  2. Compute the cumulative distribution function (CDF): cdf[i] = sum of hist[0] through hist[i].
  3. Find cdf_min, the smallest non-zero value in the CDF (the CDF of the darkest pixel that actually appears).
  4. Map each pixel value v to a new value using:
new_val=round(cdf[v]−cdfmintotal_pixels−cdfmin×255)new\_val = round\left(\frac{cdf[v] - cdf_{min}}{total\_pixels - cdf_{min}} \times 255\right)new_val=round(total_pixels−cdfmin​cdf[v]−cdfmin​​×255)

If all pixels have the same value (total_pixels equals cdf_min), map every pixel to 0.

Loading visualization...

Examples

Input: image = [[0, 1], [2, 3]]

Output: [[0, 85], [170, 255]]

Explanation: Four equally frequent intensities are spread across the full output range.

Input: image = [[100, 100], [100, 100]]

Output: [[0, 0], [0, 0]]

Hint 1

Build a 256-bin histogram, then convert it into a cumulative count array.

Hint 2

Use the first positive cumulative count as the lower endpoint of the mapping.

Requirements

  • Build a 256-bin histogram and its cumulative sum
  • Use round() (not floor or ceil) when mapping to new values
  • Handle the edge case where all pixels are the same value

Constraints

  • Image has at least one pixel
  • Pixel values are integers in [0, 255]
  • Return a 2D list of integers with the same shape as input
  • Time limit: 300 ms
Try Similar Problems
Image HistogramColor To GrayscaleGaussian Blur KernelConv2d Image FilteringSobel Edge Detection

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Histogram Equalization

Computer Vision
Medium

Histogram equalization improves image contrast by redistributing pixel intensities so they span the full [0, 255] range more uniformly. It is one of the most commonly used image enhancement techniques.

Given a 2D grayscale image with integer pixel values in [0, 255], apply histogram equalization and return the transformed image.

Algorithm

  1. Compute the histogram: count the frequency of each intensity value (0 through 255).
  2. Compute the cumulative distribution function (CDF): cdf[i] = sum of hist[0] through hist[i].
  3. Find cdf_min, the smallest non-zero value in the CDF (the CDF of the darkest pixel that actually appears).
  4. Map each pixel value v to a new value using:
new_val=round(cdf[v]−cdfmintotal_pixels−cdfmin×255)new\_val = round\left(\frac{cdf[v] - cdf_{min}}{total\_pixels - cdf_{min}} \times 255\right)new_val=round(total_pixels−cdfmin​cdf[v]−cdfmin​​×255)

If all pixels have the same value (total_pixels equals cdf_min), map every pixel to 0.

Loading visualization...

Examples

Input: image = [[0, 1], [2, 3]]

Output: [[0, 85], [170, 255]]

Explanation: Four equally frequent intensities are spread across the full output range.

Input: image = [[100, 100], [100, 100]]

Output: [[0, 0], [0, 0]]

Hint 1

Build a 256-bin histogram, then convert it into a cumulative count array.

Hint 2

Use the first positive cumulative count as the lower endpoint of the mapping.

Requirements

  • Build a 256-bin histogram and its cumulative sum
  • Use round() (not floor or ceil) when mapping to new values
  • Handle the edge case where all pixels are the same value

Constraints

  • Image has at least one pixel
  • Pixel values are integers in [0, 255]
  • Return a 2D list of integers with the same shape as input
  • Time limit: 300 ms
Try Similar Problems
Image HistogramColor To GrayscaleGaussian Blur KernelConv2d Image FilteringSobel Edge Detection

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.