本文为大家分享了C语言实现BMP图像直方图均衡化处理的具体代码,供大家参考,具体内容如下
计算步骤:
1)统计各灰度值的概率;
2)计算了累积概率直方图(CDF);
3)取整扩展:Tk = int[ (L-1)*Tk];
#include <Windows.h> #include <stdlib.h> #include <stdio.h> #include <math.h> 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\n", 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; }
实验结果:
实验结果分析:对比原图与实验结果图,原图中,头发和衣领处灰度值较低的地方在结果图中灰度值更低,而原图中,额头中间偏右处较亮,在结果图中更亮,灰度值更大。整体上直方图均衡化拉伸了全局对比度。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/fengxianghui01/article/details/85107273