A photo taken in fog or dim light uses only a narrow band of the available brightness range, so everything looks washed out. Histogram equalization stretches the used range so that brightness values are spread as evenly as possible across all 256 levels, which raises contrast. The trick is to map each gray level through the image's own cumulative distribution function (CDF): a level that is above 30% of the pixels maps to about 30% of the way up the output range. The mapping is a lookup table that depends only on the histogram, and it is monotone, so the order of brightness is preserved.
Implement equalize_histogram.
img is a 2-D uint8 array. Compute the histogram of the 256 levels and its cumulative sum cdf.cdf_min be the CDF value at the smallest gray level present and N = img.size. The lookup table is lut[v] = round((cdf[v] - cdf_min) / (N - cdf_min) * 255) for levels v, clipped to [0, 255]. If N == cdf_min (a constant image) return the image unchanged.lut[img] as uint8. Use np.round (halves to even).np.bincount(img.ravel(), minlength=256) is the histogram.
The lowest present level always maps to 0 and the highest to 255.
No results yet
Run your code to check it against the examples, or submit to run every test.