Specifications
Intel
®
Image Processing Library Reference Manual
10-8
10
Example 10-2 Using the Function iplContrastStretch() to Enhance an Image
(continued)
/// allocate LUT's arrays
lut.key = malloc( sizeof(int)*(range+1) );
lut.value = malloc( sizeof(int)*range );
lut.factor = malloc( sizeof(int)*range );
/// make the image with a narrow and shifted range
iplRShiftS( img, img, 4 );
iplAddS( img, img, 4 );
/// compute histogram and find min and max values
for( i=0; i<=range; i++) lut.key[i] = i;
iplComputeHisto( img, &plut );
mn = 0; while( !lut.value[mn] ) mn++;
mx = 255; while( !lut.value[mx] ) mx--;
/// prepare LUT for stretching
lut.interpolateType = IPL_LUT_INTER; /// interpolation
mode, not lookup
lut.num = 2; /// num of key values
lut.key[0] = 0; /// lower value
lut.key[1] = 255; /// upper value
lut.factor[0] = 255 / (mx - mn); /// factor to extend
range
lut.value[0] = -lut.factor[0] * mn; /// value to shift
/// The operation is: x(i) = x(i) * factor + value
iplContrastStretch( img, img, &plut );
/// compute histogram and find min and max values again
lut.num = 257;
lut.key[1] = 1;
iplComputeHisto( img, &plut );
mn = 0; while( !lut.value[mn] ) mn++;
mx = 255; while( !lut.value[mx] ) mx--;
free( lut.factor);
free( lut.value );
free( lut.key );
iplDeallocate( img, IPL_IMAGE_ALL );
}