图像算法之直方图均衡化(灰度图像)

1、函数定义

//直方图均衡化
struct stPGMImage* EqualHist(struct stPGMImage* image);

2、函数实现

struct stPGMImage* EqualHist(struct stPGMImage* image_src)
{
	if (image_src == NULL)
		return NULL;

	int gray[256] = { 0 };
	double gray_prob[256] = { 0 };
	double gray_distribution[256] = { 0 };
	unsigned char gray_equal[256] = { 0 };

	struct stPGMImage* result = (struct stPGMImage*)malloc(sizeof(struct stPGMImage));
	result->height = image_src->height;
	result->width = image_src->width;
	result->maxraw = image_src->maxraw;
	result->type = image_src->type;
	result->data = (unsigned char*)malloc(sizeof(unsigned char) * result->width * result->height);
	memcpy(result->data, image_src->data, image_src->height * image_src->width);
	
	for (int y = 0; y < image_src->height; y++)
	{
		unsigned char* p = (unsigned char*)(result->data + y * result->width);
		for (int x = 0; x < image_src->width; x++)
		{
			unsigned char vaule = p[x];
			gray[vaule]++;
		}
	}

	for (int i = 0; i < 256; i++)
	{
		gray_prob[i] = ((double)gray[i] / (image_src->width * image_src->height));
	}

	gray_distribution[0] = gray_prob[0];
	for (int i = 1; i < 256; i++)
	{
		gray_distribution[i] = gray_distribution[i - 1] + gray_prob[i];
	}

	for (int i = 0; i < 256; i++)
	{
		gray_equal[i] = (unsigned char)((256 - 1) * gray_distribution[i] + 0.5);
	}


	for (int y = 0; y < image_src->height; y++)
	{
		unsigned char* p = (unsigned char*)(result->data + y * result->width);
		for (int x = 0; x < image_src->width; x++)
		{
			p[x] = gray_equal[p[x]];
		}
	}

	return result;
}

3、效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值