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 输入图像