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.
CodePudding user response:
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 prefix sum is a,using accumulate(iterable) method.
CodePudding user response:
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?