c语言图像压缩算法实现_C语言实现BMP图像处理:直方图均衡化

计算步骤:

1)统计各灰度值的概率;

2)计算了累积概率直方图(CDF);

3)取整扩展:Tk = int[ (L-1)*Tk];

代码

#include #include #include #include  int main(int* argc, char** argv){FILE* fp = fopen("./01.bmp", "rb");if (fp == 0)return 0;BITMAPFILEHEADER fileHead;fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);BITMAPINFOHEADER infoHead;fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);int width = infoHead.biWidth;int height = infoHead.biHeight;int biCount = infoHead.biBitCount; int lineByte = (biCount*width / 8 + 3) / 4 * 4;RGBQUAD* pColorTable;pColorTable = new RGBQUAD[256];fread(pColorTable, sizeof(RGBQUAD), 256, fp); unsigned char* pBmpBuf;pBmpBuf = new unsigned char[lineByte*height];fread(pBmpBuf, lineByte*height, 1, fp);fclose(fp);// 统计概率double st[256] = { 0 };int st1[256] = { 0 };int t;for (int i = 0; i < height; ++i){for (int j = 0; j < width; ++j){t = *(pBmpBuf + i*lineByte + j);st[t]++;}}// 计算累加直方图并完成映射st[0] = st[0] / (width*height);st1[0] = round(double((256 - 1)*st[0]));for (int i = 1; i < 256; ++i){st[i] = st[i] / (width*height);st[i] = st[i] + st[i - 1];st1[i] = int(round(double((256 - 1)*st[i])));printf("st[i] = %d, st1[t] = %d", st[i], st1[i]);}// 新图像的像素填充unsigned char* pBmpBuf1;pBmpBuf1 = new unsigned char[lineByte*height];for (int i = 0; i < height; ++i){for (int j = 0; j < width; ++j){t = *(pBmpBuf + i*lineByte + j);*(pBmpBuf1 + i*lineByte + j) = st1[t];}} FILE* fop = fopen("./imhist.bmp", "wb");if (fop == 0)return 0;fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fop);fwrite(&infoHead, sizeof(BITMAPINFOHEADER), 1, fop);fwrite(pColorTable, sizeof(RGBQUAD), 256, fop);fwrite(pBmpBuf1, lineByte*height, 1, fop);fclose(fop); system("pause");return 0;}

效果展现

e83873312ed5a90c408f05c15be56d1c.png

原图

39849b2d7ac09344f5ebf38220f4b74d.png

直方图均衡化的结果

编译环境

983b37fc8b3bb71baeac7044741c78d8.png

VS2013

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值