C/C++图像处理2 直方图均衡化

目录

  • 内容
  • 原理
  • 代码
  • 参考及感谢

内容

C语言图像处理直方图均衡化

原理

share_noel/图像处理/数字图像处理-夏良正.pdf
https://blog.csdn.net/qq_41102371/article/details/125646840
数字图像处理-夏良正P144
C/C++图像处理2 直方图均衡化_第1张图片

代码

基于前面的博客C/C++图像处理1 灰度图修正添加处理代码

void Cgraylevel_correction_LXDlg::OnBnClickedCorrection1a()
{
	// TODO:  在此添加控件通知处理程序代码
	if (bmpfilepath1 == "" || bmpfilepath2 == "")
	{
		MessageBox(_T("请先加载原图"));
		return;
	}
	int i, j, a = 0, b = 0, c = 0;
	UpdateData(true);//更新编辑框值到变量
	if (m_p1avalueA == m_p1avalueB)
	{
		MessageBox(_T("A、B不能相等"));
		return;
	}
	//直方图数组清零
	for (i = 0; i < h1; i++)
	{
		gramf1a[i] = 0;
	}
	((CSeries)m_Chart.Series(0)).Clear();
	((CSeries)m_Chart.Series(1)).Clear();

	out_array = allocate_image_array(h1, w1);
	bmheader.height = h1;
	bmheader.width = w1;
	create_allocate_bmp_file(pFileName1a, &bmp_file_header, &bmheader);

	histogram_equalization(in_array1, out_array, h1, w1);	//直方图均衡化

	for (i = 0; i < h1; i++)
	{
		for (j = 0; j < w1; j++)
		{
			gramf1a[out_array[i][j]]++;
		}
	}
	write_bmp_image(pFileName1a, out_array);
	free_image_array(out_array, h1);
	Cgraylevel_correction_LXDlg::showbmp(file1fa, 3);//显示灰度变换图1

	((CSeries)m_Chart.Series(0)).AddXY(m_p1avalueA, m_p1avalueZ1, NULL, 0);//A为横坐标 Z1为纵坐标画连线
	((CSeries)m_Chart.Series(0)).AddXY(m_p1avalueB, m_p1avalueZ2, NULL, 0);//B为横坐标 Z2为纵坐标画连线

}
void calculate_histogram(long height, long width, short **image, unsigned long histogram[])
{
	short k;
	for (int i = 0; i < height; i++){
		for (int j = 0; j < width; j++){
			k = image[i][j];
			histogram[k] = histogram[k] + 1;
		}
	}
}
void histogram_equalization(short** in_array, short** out_array, long height, long width)
{
	unsigned long sum, sum_of_h[GRAY_LEVELS];//#define GRAY_LEVELS          255
	double constant;
	unsigned long histogram[GRAY_LEVELS] = {};

	calculate_histogram(height, width, in_array, histogram);
	sum = 0;
	for (int i = 0; i < GRAY_LEVELS; i++){
		sum = sum + histogram[i];
		sum_of_h[i] = sum;
	}

	constant = (double)(GRAY_LEVELS) / (double)(height*width);
	for (int i = 0, k = 0; i < height; i++){
		for (int j = 0; j < width; j++){
			k = in_array[i][j];
			out_array[i][j] = sum_of_h[k] * constant;
		}
	}
}

原图
C/C++图像处理2 直方图均衡化_第2张图片C/C++图像处理2 直方图均衡化_第3张图片
均衡化后
C/C++图像处理2 直方图均衡化_第4张图片C/C++图像处理2 直方图均衡化_第5张图片

参考及感谢

c语言数字图像处理(四):灰度变换

学习笔记,如有错漏,敬请指正
--------------------------------------------------------------------------------------------诺有缸的高飞鸟202007

你可能感兴趣的:(C语言图像处理,MFC,C++,绘图,C语言,图像处理)