Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Histogram Equalization is one of the fundamental tools in the image processing toolkit. Itâs a technique for adjusting the pixel values in an image to enhance the contrast by making those intensities more equal across the board. Typically, the histogram of an image will have something close to a normal distribution, but equalization aims for a uniform distribution. In this article, weâre going to program a histogram equalizer in python from scratch. If you want to see the full code, Iâve included a link to a Jupyter notebook at the bottom of this article. Now, if youâre ready, letâs dive in!
Before anything, we have to do some setup. Letâs import the libraries weâll be using throughout the program, load in the image, and display it:
For the purposes of this tutorial, weâre using a grayscale image since each pixel in a grayscale image represents only one valueâââthe intensity. I think this makes the math easier to reason about since we only have to care about one value. For comparison, in an RGB color image, each pixel contains three values (Red/Green/Blue). Due to how weâre reading in and processing the image, you can still run a color image through this programâââand I encourage you to so you can see what kind of output youâd get!
The image weâll be using is a washed-out x-ray. We can pretend that weâre radiologists that want to equalize the x-ray to better see some of the details.
In order to process the image, we have to first read it in as an array. However, numpy will automatically return a multi-dimensional array, so we flatten it to a one-dimensional array:
In the flattened array, we have an intensity value for every pixel. The values will range from 0 (black) to 255 (white). Everything in between can be considered a shade of gray. As you can see from the diagram above, we have a spike of values near zero and not many values over 200.
We can now take our one-dimensional array and compute the histogram for the image based on the frequency of similar intensity values. There are pre-existing functions that will do this for you, but weâre making this from scratch, so letâs write our own!
â ïž Keep in mind that for production environments, you would want to use pre-existing functions since theyâre better optimized, and can handle more use cases.
The mathematical formula from which weâll base our solution is:
Now we have our histogram, and we can take the next step towards equalization by computing the cumulative sum of the histogram. The cumulative sum is exactly as it soundsâââthe sum of all values in the histogram up to that point, taking into account all previous values. Just as above, there are functions that exist to compute this for you, but letâs write our own:
Weâre making progress! We now have the cumulative sum, but as you can see, the values are huge (> 6,000,000). Weâre going to be matching these values to our original image in the final step, so we have to normalize them to conform to a range of 0â255. Hereâs one last formula for us to code up:
Thatâs betterâââour values are now normalized between 0-255. Now, for the grand finale. We can now use the normalized cumulative sum to modify the intensity values of our original image. The code to do this can look a bit confusing if youâve never used numpy before. In fact, itâs anti-climactically simple.
Weâll take all of the values from the flat array and use it as the index to look up related value in the cs array. The result becomes the new intensity value which will be stored in img_new for that particular pixel.
As a final step, we reshape the array to match the original image so we can render the result as an image.
And there we have itâââthe original image has been equalized. Weâre practically radiologists now! Notice the difference in contrast throughout the whole image. The most important thing to remember about histogram equalization is that it adjusts the intensities at a global level, taking into account all pixels. That process works well for images like the one above but may perform poorly on other images.
For example, take the image belowâââit was transformed using the exact same algorithm, however, you can see that it didnât enhance the photo as much as it utterly destroyed it:
Histogram equalization isnât always the perfect tool for the job. But, there are other methods you can use that take neighboring pixels into consideration instead of using the entire image. Stay tuned for the next article where weâll walk through a more localized equalization algorithm.
The full source code (as a Jupyter notebook) for this article can be found here:
torywalker/histogram-equalizer
đ If you found this article helpful and would like to see more, please let me know by leaving some claps! đ Follow for more articles like this!
Histogram Equalization in Python from Scratch was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.