histogram_specification



/***************************************************************************
* Func: histogram_specification *
* *
* Desc: perform histogram specification on an input image *
* *
* Params: buffer - pointer to input image *
* number_of_pixel - total number of pixels in image *
* desired_histogram - desired histogram of output image *
***************************************************************************/

void histogram_specification(image_ptr buffer, unsigned long number_of_pixels,
unsigned long *desired_histogram)
{
float histogram[256]; /* image histogram */
float sum_hist[256]; /* normalized sum of histogram elements */
float scale_factor; /* scaling factor used to normalize hist */
float difference; /* used to determine inverse transform */
unsigned long i; /* loop variable */
unsigned long sum; /* sum used to determine sum of hist */
unsigned char inv_hist[256]; /* inverse histogram indices */
int j; /* loop variable */
int min; /* min difference computing inverse trans */

/* clear histogram to 0 */
for(i=0; i<256; i++)
histogram[i]=0;

/* calculate histogram */
for(i=0; i<number_of_pixels; i++)
histogram[buffer[i]]++;

/* calculate normalized sum of hist */
sum = 0.0;
scale_factor = 255.0 / number_of_pixels;
for(i=0; i<256; i++)
{
sum += histogram[i];
sum_hist[i] = (sum * scale_factor) + 0.5;
}

/* transform image using new sum_hist as a LUT */
for(i=0; i<number_of_pixels; i++)
buffer[i] = (unsigned char) sum_hist[buffer[i]];

/* calculate normalized sum of hist for desired histogram */
sum = 0;
scale_factor = 255.0 / number_of_pixels;
for(i=0; i<256; i++)
{
sum += desired_histogram[i];
sum_hist[i] = sum * scale_factor;
}

/* generate the inverse transform */
for(i=0; i<256; i++)
{
difference = fabs(i - sum_hist[0]);
min = 0;
for(j=0; j<256; j++)
{
if(fabs(i-sum_hist[j]) < difference)
{
difference = fabs(i - sum_hist[j]);
min = j;
}
}
inv_hist[i] = (unsigned char) min;
}

/* transform final image using inv_hist */
for(i=0; i<number_of_pixels; i++)
buffer[i] = inv_hist[buffer[i]];
}



<256><256 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值