0

The general histogram equalization formula for an 8-bit image is: h(v) = ((cdf(v) - cdf_min) / (M * N - cdf_min) * 255) where

cdf(v) is the cumulative distribution function. For each pixel v, cdf(v) equals to the number of pixels with values lower or equal to v cdf_min is the minimum non-zero value of the cumulative distribution function M × N are the sizes of the image

I want to calculate cdf(v) which I can do using iterating over all the pixels but I am wondering if there's a way to do it in an efficient way using numpy so that the whole operation is way faster.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • get histogram ("pdf"), then calculate cumulative sum, which gives the CDF. that's it. cumulative sum is O(n) – Christoph Rackwitz Jul 30 '22 at 15:04
  • you didn't google that hard... your question is equivalent to that one: https://stackoverflow.com/questions/10640759/how-to-get-the-cumulative-distribution-function-with-numpy and please review [ask] – Christoph Rackwitz Jul 30 '22 at 15:07

2 Answers2

0

The cdf is the prefix sum of the pdf. You can compute that prefix sum yourself in Python (just 255 iterations), or rely on a ready-made function. https://www.geeksforgeeks.org/prefix-sum-array-python-using-accumulate-function/#:~:text=A%20prefix%20sum%20is%20a,using%20accumulate(iterable)%20method.

0

Just use cdf = np.cumsum(pdf) on your histogram/PDF. That calculates the cumulative sum. It's faster than writing it yourself in python, especially if your histogram is already a numpy array.

All of that has already been asked and answered before on Stack Overflow, so this question ought to be closed as a duplicate of How to get the cumulative distribution function with NumPy?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36