Exercise 4. Equalizing an image

An image histogram is a graphical representation of the intensity distribution of an image. It quantifies the number of pixels for each intensity value considered. OpenCV - Histogram equalization Histogram equalization is a method that improves the contrast in an image, in order to stretch out the intensity range. In this example, in equalize.cpp this outcome has been achived by using OpenCV function:equalize_hist:equalizeHist <>.

original equalized

This example was based on the histogram.cpp algorithm and answers to the proposed exercises at agostinhobritojr.github.io.

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

/**  @function main */
int main( int argc, char** argv )
{
  Mat src, dst;

  string source_window = "Source image";
  string equalized_window = "Equalized Image";

  /// Camera 
  VideoCapture cap;

  //start camera
  cap.open(0);

  if(!cap.isOpened()){
    cout << "Camera not available";
    return -1;
  }

  while(1){
    /// capture image
    cap >> src;

    /// Convert to grayscale
    cvtColor( src, src, CV_BGR2GRAY );

    /// Apply Histogram Equalization
    equalizeHist( src, dst );

    /// Display results
    namedWindow( source_window, CV_WINDOW_NORMAL );
    namedWindow( equalized_window, CV_WINDOW_NORMAL );

    imshow( source_window, src );
    imshow( equalized_window, dst );

    if(waitKey(30) >= 0) break;
    }

  // Closes all the windows
  destroyAllWindows();

  return 0;
}

What does this program do?

Create a VideoCapture object
Capture frame-by-frame
Convert the original image to grayscale
Equalize the Histogram by using the OpenCV function EqualizeHist
Display the source and equalized images in a window.

As we can see from the output image, the equalized image has a better distribution of the overall contrast, the furniture behind can be easily observed now. Original image is on the rigth and equalized on the left side equalize