auto_contrast_stretch


/***************************************************************************
* 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]];
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值