龙空技术网

C语言实现BMP图像处理:直方图均衡化

大众科普 38

前言:

现在朋友们对“直方图均衡化处理的作用”大约比较关切,你们都想要学习一些“直方图均衡化处理的作用”的相关内容。那么小编也在网摘上网罗了一些有关“直方图均衡化处理的作用””的相关文章,希望我们能喜欢,你们快快来学习一下吧!

计算步骤:

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;}
效果展现

原图

直方图均衡化的结果

编译环境

VS2013

标签: #直方图均衡化处理的作用 #用c语言写出直方图均衡的主要步骤 #c语言读取bmp像素