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.
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?