ECOE 508, Programming Assignment #1
Histogram Equalization
Due Date: 20 February 2008
This assignment intends to initiate you with OpenCV computer vision library. You will implement basic contrast enhancing techniques, namely gamma correction and histogram equalization in the way we have seen in the class. You will first need to install OpenCV libraries and to read the relevant parts of the OpenCV documentation provided with the installation. Sample program codes in the documentation are indeed very helpful.
Program Specifications
1. Basic Tasks (in order)
Read (or load) the color JPEG image provided by the user;
Convert RGB to YCrCb representation;
Compute and display the intensity (or luminance) histogram;
Perform gamma correction on the intensity (using the gamma value provided by the user) and display the corrected intensity (or luminance) histogram;
Display the corrected color image;
Perform histogram equalization (on the initial image) and display the equalized histogram;
Display the equalized color image;
Save both corrected color images as JPEG files.
2. Requirements and Hints
Provide comments where appropriate in the source code. Good programming style will also be taken into account in grading.
OpenCV provides all necessary basic functions (i.e., image load, save, display and YCrCb conversion) and image data structure types (e.g. Iplimage*). It also provides several histogram manipulation functions, including equalization as well. Please do not use any of the OpenCV histogram functions. Do not use for example the function cvCalcHist. You should implement these yourselves as we did in the class.
For image display, save and load operations, use the provided highgui library. You can find below a sample code extracted from the documentation. That gives an idea about what looks like a program written in OpenCV for histogram computation in HSV color code.
You may think of the histogram plot as a 256xHmax binary (black and white) image, where Hmax is the maximum intensity count in the histogram. (The background pixels are white and the pixels corresponding to the histogram bars are black, for example).
Use cvShowImage, cvNamedWindow and cvWaitKey for image display.
For gamma correction, use an appropriate gamma value of your choice, which would give a satisfactory result.
Use this image as input. Note that the picture is a bit dark and needs enhancement. You may try other images as well.
Submission
You will submit only the source files, i.e., the project files, ready to be compiled and run (Microsoft Visual C/C++). Please try to comply with the announced due date.
You can send me the files via e-mail.
----------------------------------------------------------------------------------------------------------------------
Extracted from the OpenCV documentation provided with the installation
-----------------------------------------------------------------------------------------------------------------------
Calculates histogram of image(s)
void cvCalcHist( IplImage** img, CvHistogram* hist,
int doNotClear=0, const CvArr* mask=0 );
The function cvCalcHist calculates the histogram of one or more single-channel images. The elements of a tuple that is used to increment a histogram bin are taken at the same location from the corresponding input images.
#include <cv.h>
#include <highgui.h>
int main( int argc, char** argv )
{
IplImage* src;
if( argc == 2 && (src=cvLoadImage(argv[1], 1))!= 0)
{
IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
int h_bins = 30, s_bins = 32;
int hist_size[] = {h_bins, s_bins};
float h_ranges[] = { 0, 180 }; /* hue varies from 0 (~0°red) to 180 (~360°red again) */
float s_ranges[] = { 0, 255 }; /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */
float* ranges[] = { h_ranges, s_ranges };
int scale = 10;
IplImage* hist_img = cvCreateImage( cvSize(h_bins*scale,s_bins*scale), 8, 3 );
CvHistogram* hist;
float max_value = 0;
int h, s;
cvCvtColor( src, hsv, CV_BGR2HSV );
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
cvCalcHist( planes, hist, 0, 0 );
/* here you need some code to represent the histogram (hist) as an image (hist_img)*/
cvNamedWindow( "Source", 1 );
cvShowImage( "Source", src );
cvNamedWindow( "H-S Histogram", 1 );
cvShowImage( "H-S Histogram", hist_img );
cvWaitKey(0);
}
}