Equalizing histograms

Using the functions and algorithms that we've learned so far, we can enhance the intensity distribution of images, or, in other words, adjust the brightness of too dark or overly bright images, among many other operations. In computer vision, the histogram-equalization algorithm is used for the exact same reason. This algorithm performs the following tasks:

  • Calculates the histogram of an image
  • Normalizes the histogram
  • Calculates the integral of the histogram
  • Uses the updated histogram to modify the source image

Except the integral part, which is simply calculating the sum of the values in all bins, the rest is what we already performed in this chapter, in one way or another. OpenCV includes a function called equalizeHist that performs all of the mentioned operations and produces an image with an equalized histogram. Let's first see how this function is used and then try an example to see the effect for ourselves.

The following example codes depict how the equalizeHist function is used, which is extremely easy to use and requires no special parameters whatsoever:

Mat equalized; 
equalizeHist(gray, equalized); 

Let's consider for instance that we have the following image, which is extremely overexposed (or bright), and its histogram, which is depicted on the right-hand side:

Using the equalizeHist function, we can get an image with better contrast and brightness. Here are the resulting image and histogram of the preceding example image when its histogram is equalized:

Histogram equalization is quite helpful when we have to deal with images that have the potential to be overexposed (too bright) or underexposed (too dark). For instance, x-ray scan images, where the details are only visible when the contrast and brightness is increased using a powerful backlight, or when we are working with video frames from an environment that can have intensive light changes, are examples of conditions in which histogram equalization can be used to make sure the rest of the algorithms always deal with the same, or just slightly different, brightness and contrast levels.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79