/***************************************************************************
* Func: auto_contrast_stretch *
* *
* Desc: performs basic contrast stretching on an image *
* *
* Params: source - pointer to source image *
* cols - number of columns in the image *
* rows - height of image *
***************************************************************************/
void auto_contrast_stretch(image_ptr source, int cols, int rows)
{
long i; /* loop variable */
long number_of_pixels; /* total number of pixels in image */
long histogram[256]; /* image histogram */
unsigned char LUT[256]; /* Look-up table for point process */
int lowthresh, highthresh; /* lower and upper thresholds */
float scale_factor; /* scaling factor for contrast stretch */
/* compute histogram */
number_of_pixels = (long)cols * rows;
for(i=0; i<256; i++)
histogram[i]=0;
for(i=0; i<number_of_pixels; i++)
histogram[source[i]]++;
/* compute low and high thresholds */
for(i=0; i<256; i++)
if(histogram[i])
{
lowthresh = i;
break;
}
for(i=255; i>0; i--)
if(histogram[i])
{
highthresh = i;
break;
}
printf("Low threshold is %d High threshold is %d\n",
lowthresh,highthresh);
/* compute new LUT */
for(i=0; i<lowthresh; i++)
LUT[i]=0;
for(i=255; i>highthresh; i--)
LUT[i]=255;
scale_factor = 255.0 / (highthresh-lowthresh);
for(i=lowthresh; i<=highthresh; i++)
LUT[i]=(unsigned char)((i - lowthresh) * scale_factor);
/* transfer new image */
for(i=0; i<number_of_pixels; i++)
source[i] = LUT[source[i]];
}
auto_contrast_stretch
最新推荐文章于 2024-01-04 16:49:46 发布