Specifications

Histogram, Threshold, and Compare Functions
10-11
10
Example 10-3 Computing and Equalizing the Image Histogram
int example102( void ) {
IplImage *imga;
const int width = 4, height = 4, range = 256;
IplLUT lut = { range+1, NULL,NULL,NULL, IPL_LUT_LOOKUP };
IplLUT* plut = &lut;
__try {
int i;
lut.key = malloc( sizeof(int)*(range+1) );
lut.value = malloc( sizeof(int)*range );
imga = iplCreateImageHeader(
1, 0, IPL_DEPTH_8U, "GRAY", "GRAY",
IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL,
IPL_ALIGN_DWORD, width, height, NULL, NULL,
NULL, NULL);
if( NULL == imga ) return 0;
// Create with filling
iplAllocateImage( imga, 1, 3 );
if( NULL == imga->imageData ) return 0;
// Make the two level data
for( i=0; i<8; i++) ((char*)imga->imageData)[i] = (char)7;
// Initialize the histogram levels
for( i=0; i<=range; i++) lut.key[i] = i;
// Compute histogram
iplComputeHisto( imga, &plut );
// Equalize histogram = rescale range of image data
iplHistoEqualize( imga, imga, &plut );
// Check if an error occurred
if( iplGetErrStatus() != IPL_StsOk ) return 0;
}
__finally {
iplDeallocate( imga, IPL_IMAGE_HEADER | IPL_IMAGE_DATA );
if( lut.key ) free( lut.key );
if( lut.value ) free( lut.value );
}
return IPL_StsOk == iplGetErrStatus();
}