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

更新时间:2023-06-07 23:36:17 阅读: 评论:0

限制对⽐度⾃适应直⽅图均衡化算法(CLAHE)实现1. 概述
本篇⽂章是基于这篇写的,然后经过粗略查看之后,将其运⽤到课题中,这⾥将此记录下来作为记录,⽂章中若有错误的地⽅敬请谅解。
2. 实现代码
bool CLAHE_Algorithm::CLAHE_Process(cv::Mat& src_img)
{
if (!src_img.data)
{
std::cout << "⽆图像数据" << endl;
return fal;
}
int rows(ws);
int cols(ls);
int img_type = pe(); //图像的类型
if (CV_8UC1 != img_type && CV_8UC3 != img_type)
{
//cout << "输⼊8位的图像" << endl;
return fal;
}
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_ws;
产品推广的渠道cols=src_ls;
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 ws, ls, pe(), 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);
}
el 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  输⼊图像在Y轴⽅向上进⾏分块的数⽬(min 2, max uiMAX_REG_X)
// 函数参数:  unsigned int uiNrBins 输出图像的有效灰度级数,输出图像同样拥有之前设定好的最⼤和最⼩灰度值,通常选择128既能加快处理速度并且效果不错// 函数参数:  float fCliplimit  图像规范化区间限制,数值越⼤图像对⽐度越⾼(当为1时直接返回输⼊图像)
// 返回值:    int $
//************************************************************************
int CLAHE_Algorithm::CLAHE (kz_pixel_t* pImage, unsigned int uiXRes, unsigned int uiYRes, kz_pixel_t Min, kz_pixel_t Max, unsigned int uiNrX,
unsigned int uiNrY, unsigned int uiNrBins, float fCliplimit)
{
unsigned int uiX, uiY;      /* counters */
unsigned int uiXSize, uiYSize, uiSubX, uiSubY; /* size of context. reg. and subimages */
unsigned int uiXL, uiXR, uiYU, uiYB;  /* auxiliary variables interpolation routine */
unsigned long ulClipLimit, ulNrPixels;  /* clip limit and region pixel count */
kz_pixel_t* pImPointer;      /* pointer to image */
kz_pixel_t aLUT[uiNR_OF_GREY];    /* lookup table ud for scaling of input image */
unsigned long* pulHist, *pulMapArray;  /* pointer to histogram and mappings*/
unsigned long* pulLU, *pulLB, *pulRU, *pulRB; /* auxiliary pointers interpolation */
if (uiNrX > uiMAX_REG_X) return -1;    /* # of regions x-direction too large */
if (uiNrY > uiMAX_REG_Y) return -2;    /* # of regions y-direction too large */
if (uiXRes % uiNrX) return -3;    /* x-resolution no multiple of uiNrX */
if (uiYRes & uiNrY) return -4;    /* y-resolution no multiple of uiNrY */
if (Max >= uiNR_OF_GREY) return -5;    /* maximum too large */
if (Min >= Max) return -6;      /* minimum equal or larger than maximum */
if (uiNrX < 2 || uiNrY < 2) return -7;  /* at least 4 contextual regions required */
if (fCliplimit == 1.0) return 0;    /* is OK, immediately returns original image. */
if (uiNrBins == 0) uiNrBins = 128;    /* default value when not specified */
pulMapArray=(unsigned long *)malloc(sizeof(unsigned long)*uiNrX*uiNrY*uiNrBins); //全部块的直⽅图查询表    if (pulMapArray == 0) return -8;    /* Not enough memory! (try reducing uiNrBins) */
uiXSize = uiXRes/uiNrX; uiYSize = uiYRes/uiNrY;  /* Actual size of contextual regions */
ulNrPixels = (unsigned long)uiXSize * (unsigned long)uiYSize;
if(fCliplimit > 0.0) {      /* Calculate actual cliplimit    */
ulClipLimit = (unsigned long) (fCliplimit * (uiXSize * uiYSize) / uiNrBins);
ulClipLimit = (ulClipLimit < 1UL) ? 1UL : ulClipLimit;
} //计算直⽅图限制值
el ulClipLimit = 1UL<<14;      /* Large value, do not clip (AHE) */
MakeLut(aLUT, Min, Max, uiNrBins);    /* Make lookup table for mapping of greyvalues */
/* Calculate greylevel mappings for each contextual region */
for (uiY = 0, pImPointer = pImage; uiY < uiNrY; uiY++)
{
for (uiX = 0; uiX < uiNrX; uiX++, pImPointer += uiXSize)
{
pulHist = &pulMapArray[uiNrBins * (uiY * uiNrX + uiX)];
MakeHistogram(pImPointer, uiXRes, uiXSize, uiYSize, pulHist, uiNrBins, aLUT); //计算块直⽅图
ClipHistogram(pulHist, uiNrBins, ulClipLimit);        //修剪直⽅图,去⾼填低
MapHistogram(pulHist, Min, Max, uiNrBins, ulNrPixels);      //计算最后的直⽅图的映射lookup表
}
pImPointer += (uiYSize - 1) * uiXRes;          /* skip lines, t pointer */
}
/* Interpolate greylevel mappings to get CLAHE image */
教育改革
for (pImPointer = pImage, uiY = 0; uiY <= uiNrY; uiY++)
{
if (uiY == 0) {                      /* special ca: top row */
uiSubY = uiYSize >> 1;  uiYU = 0; uiYB = 0;
}
el
{
燃气灶开关if (uiY == uiNrY) {                  /* special ca: bottom row */
uiSubY = uiYSize >> 1;    uiYU = uiNrY - 1;    uiYB = uiYU;
}
el {                      /* default values */
龙舟制作
uiSubY = uiYSize; uiYU = uiY - 1; uiYB = uiYU + 1;
}
}
for (uiX = 0; uiX <= uiNrX; uiX++)
{
if (uiX == 0)
{                  /* special ca: left column */
uiSubX = uiXSize >> 1; uiXL = 0; uiXR = 0;
}
el
{
排骨香菇if (uiX == uiNrX)
{              /* special ca: right column */
uiSubX = uiXSize >> 1;  uiXL = uiNrX - 1; uiXR = uiXL;
}
el
{              /* default values */
uiSubX = uiXSize; uiXL = uiX - 1; uiXR = uiXL + 1;
}
}
pulLU = &pulMapArray[uiNrBins * (uiYU * uiNrX + uiXL)];
pulRU = &pulMapArray[uiNrBins * (uiYU * uiNrX + uiXR)];
pulLB = &pulMapArray[uiNrBins * (uiYB * uiNrX + uiXL)];
pulRB = &pulMapArray[uiNrBins * (uiYB * uiNrX + uiXR)];
Interpolate(pImPointer, uiXRes, pulLU, pulRU, pulLB, pulRB, uiSubX, uiSubY, aLUT);
pImPointer += uiSubX;              /* t pointer on next matrix */
}
pImPointer += (uiSubY - 1) * uiXRes; //跳转到下⼀个⾏块的起始位置
}
free(pulMapArray);      /* free space for histograms */
return 0;        /* return status OK */
}
//************************************************************************
// 函数名称:    ClipHistogram
// 访问权限:    private static
// 创建⽇期:  2016/11/27
// 创建⼈:
/
/ 函数说明:  直⽅图修剪This function performs clipping of the histogram and redistribution of bins.
//    The histogram is clipped and the number of excess pixels is counted.Afterwards
//    the excess pixels are equally redistributed across the whole histogram(providing
//    the bin count is smaller than the cliplimit
// 函数参数:  unsigned long * pulHistogram 统计直⽅图数据
// 函数参数:  unsigned int uiNrGreylevels  输出灰度等级
// 函数参数:  unsigned long ulClipLimit  限制值
// 返回值:    void $
//************************************************************************
void CLAHE_Algorithm::ClipHistogram (unsigned long* pulHistogram, unsigned int uiNrGreylevels, unsigned long ulClipLimit) {
unsigned long* pulBinPointer, *pulEndPointer, *pulHisto;
unsigned long ulNrExcess, ulUpper, ulBinIncr, ulStepSize, i;
long lBinExcess;
ulNrExcess = 0;  pulBinPointer = pulHistogram;
for (i = 0; i < uiNrGreylevels; i++)
{ /* calculate total number of excess pixels */
lBinExcess = (long)pulBinPointer[i] - (long)ulClipLimit;
if (lBinExcess > 0)
ulNrExcess += lBinExcess;      /* excess in current bin */
};
/* Second part: clip histogram and redistribute excess pixels in each bin */
ulBinIncr = ulNrExcess / uiNrGreylevels;          /* average binincrement */
ulUpper = ulClipLimit - ulBinIncr;    /* Bins larger than ulUpper t to cliplimit */爱情藏头诗
for (i = 0; i < uiNrGreylevels; i++)
{
if (pulHistogram[i] > ulClipLimit)
pulHistogram[i] = ulClipLimit; /* clip bin */
el
{
//if (pulHistogram[i] > ulUpper)
//{        /* high bin count */
// ulNrExcess -= pulHistogram[i] - ulUpper;
// pulHistogram[i] = ulClipLimit;
/
/}
//应修正为
if (pulHistogram[i] > ulUpper)
{      /* high bin count */
ulNrExcess -= (ulClipLimit - pulHistogram[i]); pulHistogram[i] = ulClipLimit;
}
el
{                    /* low bin count */
ulNrExcess -= ulBinIncr;
pulHistogram[i] += ulBinIncr;
}
}
}
while (ulNrExcess)
{  /* Redistribute remaining excess  */
pulEndPointer = &pulHistogram[uiNrGreylevels]; pulHisto = pulHistogram;
while (ulNrExcess && pulHisto < pulEndPointer)
{
ulStepSize = uiNrGreylevels / ulNrExcess;
if (ulStepSize < 1)
ulStepSize = 1;          /* stepsize at least 1 */
for (pulBinPointer = pulHisto; pulBinPointer < pulEndPointer && ulNrExcess;
pulBinPointer += ulStepSize)
{
if (*pulBinPointer < ulClipLimit)
{
(*pulBinPointer)++;
ulNrExcess--;      /* reduce excess */
}
}
pulHisto++;          /* restart redistributing on other bin location */
}
}
}
//************************************************************************
// 函数名称:    MakeHistogram
// 访问权限:    private static
// 创建⽇期:  2016/11/27
// 创建⼈:
// 函数说明:
// 函数参数:  kz_pixel_t * pImage  输⼊的图像数据
// 函数参数:  unsigned int uiXRes  输⼊图像X轴⽅向的分辨率
// 函数参数:  unsigned int uiSizeX  输⼊图像经过分割之后拆分得到的单个块X轴的分辨率
// 函数参数:  unsigned int uiSizeY  输⼊图像经过分割之后拆分得到的单个块Y轴的分辨率
/
/ 函数参数:  unsigned long * pulHistogram 输出统计直⽅图
// 函数参数:  unsigned int uiNrGreylevels 输出图像的灰度级数
// 函数参数:  kz_pixel_t * pLookupTable 查询表
// 返回值:    void $
//************************************************************************
void CLAHE_Algorithm::MakeHistogram (kz_pixel_t* pImage, unsigned int uiXRes, unsigned int uiSizeX, unsigned int uiSizeY,        unsigned long* pulHistogram, unsigned int uiNrGreylevels, kz_pixel_t* pLookupTable)
/* This function classifies the greylevels prent in the array image into
* a greylevel histogram. The pLookupTable specifies the relationship
* between the greyvalue of the pixel (typically between 0 and 4095) and
* the corresponding bin in the histogram (usually containing only 128 bins).
*/
{
kz_pixel_t* pImagePointer;
unsigned int i;
for (i = 0; i < uiNrGreylevels; i++)
pulHistogram[i] = 0L; /* clear histogram */

本文发布于:2023-06-07 23:36:17,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/897863.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图像   输出   函数   实现
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图