限制对比度自适应直方图均衡化算法(CLAHE)实现

1. 概述

本篇文章是基于这篇博文写的,然后经过粗略查看之后,将其运用到课题中,这里将此记录下来作为记录,文章中若有错误的地方敬请谅解。

2. 实现代码

bool CLAHE_Algorithm::CLAHE_Process(cv::Mat& src_img)
{
	if (!src_img.data)
	{
		std::cout << "无图像数据" << endl;
		return false;
	}

	int rows(src_img.rows);
	int cols(src_img.cols);

	int img_type = src_img.type();	//图像的类型
	if (CV_8UC1 != img_type && CV_8UC3 != img_type)
	{
		//cout << "输入8位的图像" << endl;
		return false;
	}
	int img_channels = src_img.channels();
	unsigned char* data_array = nullptr;
	unsigned char* data = nullptr;

	if (3 == img_channels)
	{
		std::vector<cv::Mat> splited_img;
		cv::Mat src_img_temp = src_img.clone();
		cv::resize(src_img_temp, src_img_temp, cv::Size(512,512));
		rows=src_img_temp.rows;
		cols=src_img_temp.cols;
		cv::split(src_img_temp, splited_img);
		for (int i = 0; i < 3; i++)
		{
			delete[] data_array;
			data_array = nullptr;

			data_array = new unsigned char[rows*cols];

			cv::Mat& temp = splited_img[i];
			for (int i = 0; i < rows; i++)
			{
				data = temp.ptr<unsigned char>(i);
				for (int j = 0; j < cols; j++)
				{
					data_array[i*rows + j] = *data++;
				}
			}
			this->CLAHE(data_array, cols, rows, 0, 255, 8, 8, 128, 10.0);

			//cv::Mat result(temp.rows, temp.cols, temp.type(), cv::Scalar::all(0));
			for (int i = 0; i < rows; i++)
			{
				data = temp.ptr<unsigned char>(i);
				for (int j = 0; j < cols; j++)
				{
					*data++ = data_array[i*rows + j];
				}
			}
		}
		cv::Mat& blue_img = splited_img[0];	//blue channel
		cv::Mat& green_img = splited_img[1];	//green channel
		cv::Mat& red_img = splited_img[2];	//red channel
		cv::Mat result;
		cv::merge(splited_img, result);
		result.copyTo(src_img);
	}
	else if (1== img_channels)
	{
		data_array = new unsigned char[rows*cols];

		for (int i = 0; i < rows; i++)
		{
			data = src_img.ptr<unsigned char>(i);
			for (int j=0; j<cols; j++)
			{
				data_array[i*rows+j] = *data++;
			}
		}
		this->CLAHE(data_array, cols, rows, 0, 255, 8, 8, 128, 10.0);

		for (int i = 0; i < rows; i++)
		{
			data = src_img.ptr<unsigned char>(i);
			for (int j = 0; j < cols; j++)
			{
				*data++ = data_array[i*rows + j];
			}
		}
	}

	if (data_array)
	{
		delete[] data_array;
		data_array = nullptr;
	}

	return true;
}


//************************************************************************
// 函数名称:    	CLAHE
// 访问权限:    	private 
// 创建日期:		2016/11/27
// 创 建 人:		
// 函数说明:		CLAHE算法的主函数
// 函数参数: 	kz_pixel_t * pImage		输入的图像数据
// 函数参数: 	unsigned int uiXRes		图像X轴上的分辨率
// 函数参数: 	unsigned int uiYRes		图像Y轴上的分辨率
// 函数参数: 	kz_pixel_t Min			输入图像也是输出图像的最小像素值
// 函数参数: 	kz_pixel_t Max			输入图像也是输出图像的最大像素值
// 函数参数: 	unsigned int uiNrX		输入图像在X轴方向上进行分块的数目(min 2, max uiMAX_REG_X)
// 函数参数: 	unsigned int uiNrY		输入图像
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值