[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppOpenCvHistogramEqualization

(C++ OpenCV) Histogram equalization

Technique to maximize contrast in an image.

// Disclaimer: This code employs bad programming style
// Please improve. I (Bilderbikkel) cannot do so, as I do not have
// OpenCV installed on my computer anymore.
  1. include <cv.h>
void histogramEqualize(const IplImage * const pSource, IplImage * pTarget) { assert(pSource!=NULL); assert(pTarget!=NULL); assert(pSource->nChannels ==1); assert(pTarget->nChannels ==1); assert(pSource->width == pTarget->width); assert(pSource->height == pTarget->height); CvHistogram *hist; uchar lut[256]; double lut1[256]; CvMat* lut_mat; int hist_size = 256; float range_0[]={0,256}; float* ranges[] = { range_0 }; int high=0; int low =0; float hist_value = 0.0; hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1); lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( lut_mat, lut, 0 ); //cvCalcHist( &pSource, hist, 0, NULL ); cvCalcHist( const_cast<IplImage**>(&pSource), hist, 0, NULL ); //CUMULATIVE ARRAY lut1[0] = 0; for(int index = 0; index != hist_size; ++index) { hist_value = cvQueryHistValue_1D(hist,index); lut1[index+1]= lut1[index] + hist_value; } //CALCULATE THE NEW LUT float scale_factor; scale_factor = 255.0f / (float) (pSource->width * pSource->height); for (int index=0; index!=256; ++index) { lut[index]= (unsigned char)((float)lut1[index]*scale_factor); } //PERFORM IT ON THE CHANNEL cvLUT( pSource, pTarget, lut_mat ); cvReleaseMat( &lut_mat); cvReleaseHist(&hist); }


Code links

'Histogram equalization' links



last edited (December 18, 2006) by bilderbikkel, Number of views: 3119, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.