当前位置:   article > 正文

对比度受限自适应直方图均衡化方法_cliplimit

cliplimit

图像增强技术可具体为时域和频域。 时域增强往往被应用于提高图像的对比度且改进其灰度级, 其原理是在灰度映射转化的基础上, 对像素进行直接性地处理; 频域增强的作用是以强化图像的低、 高频来达到改善图像的平滑性和边缘为主,其原理是通过傅里叶转换的方式提升兴趣区的频率分量。

图像灰度分布不均的问题对于图像处理技术而言是一个影响较大的问题, 为了能解决这个问题, 基于时域增强理论, 采用映射函数当中的非线性函数, 通过转化后使其像素分布均匀。

直方图均衡化方法通过不同的灰度分布方案使转变为灰度值均匀化分布的图像,以眼底图为例,利用直方图提取灰度映射曲线,再对其进行转换,从而提升亮度。但该方法仍有瑕疵,即无法增强其对比度,且在此过程当中,一并将噪声信号也同时放大了。以 AHE (Adaptive Histogtam Equalization)为例,该方法将图像利用网格线切割为数量众多的小格子区域, 并对每一个格子进行均衡化处理。但该方法会导致图像失真, 且其微噪也会因此而放大。 因此, 这里提出了 对比度受限制自适应直方图 (Contrast limited Adaptive Histogtam EqualizationCLAHE) 即对每一个划分单元进行对比度的限制处理,这也是该方法与传统的AHE的不同之处。

CLAHE 通过限制局部直方图的高度来限制噪声放大和局部对比度增强。该方法将图像划分为多个子区域; 然后对每个子区域的直方图进行分类。 再对每个子区域分别进行直方图均衡化, 最后通过对每个像素进行插值运算来获得变换后的灰度值,以此实现对比度受限自适应直方图均衡化图像增强。

CLAHE 方法原理:
CLAHE AHE 不同的地方是增加对比度限幅,这就可以克服 AHE 的过度放大噪声的问题;

综上所述,改变最大的映射函数斜率Smax 及相应的最大直方图高度Hmax,可获得不同增强效果的图像。也就是说限制CDF的斜率就相当于限制Hist的幅度。

因此我们需要对子块中统计得到的直方图进行裁剪,使其幅值低于某个上限,当然裁剪掉的部分又不能扔掉,我们还需要将这部分裁剪值均匀地分布在整个灰度区间上,以保证直方图总面积不变,如下图:

可以看到,这时直方图又会整体上升了一个高度,貌似会超过我们设置的上限。其实在具体实现的时候有很多解决方法,你可以多重复几次裁剪过程,使得上升的部分变得微不足道,或是用另一种常用的方法:

设裁剪值为ClipLimit,求直方图中高于该值的部分的和totalExcess,此时假设将totalExcess均分给所有灰度级,  求出这样导致的直方图整体上升的高度L=totalExcess/N,以upper= ClipLimit-L为界限对直方图进行如下处理:

(1)若幅值高于ClipLimit,直接置为ClipLimit;

(2)若幅值处于Upper和ClipLimit之间,将其填补至ClipLimit;

(3)若幅值低于Upper,直接填补L个像素点;

经过上述操作,用来填补的像素点个数通常会略小于totalExcess,也就是还有一些剩余的像素点没分出去,这个剩余来自于(1)(2)两处。这时我们可以再把这些点均匀地分给那些目前幅值仍然小于ClipLimit的灰度值。

  1. % total number of pixels overflowing clip limit in each bin
  2. totalExcess = sum(max(imgHist - clipLimit,0));
  3. % clip the histogram and redistribute the excess pixels in each bin
  4. avgBinIncr = floor(totalExcess/numBins);
  5. upperLimit = clipLimit - avgBinIncr; % bins larger than this will be
  6. % set to clipLimit
  7. % this loop should speed up the operation by putting multiple pixels
  8. % into the "obvious" places first
  9. for k=1:numBins
  10. if imgHist(k) > clipLimit
  11. imgHist(k) = clipLimit;
  12. else
  13. if imgHist(k) > upperLimit % high bin count
  14. totalExcess = totalExcess - (clipLimit - imgHist(k));
  15. imgHist(k) = clipLimit;
  16. else
  17. totalExcess = totalExcess - avgBinIncr;
  18. imgHist(k) = imgHist(k) + avgBinIncr;
  19. end
  20. end
  21. end
  22. % this loops redistributes the remaining pixels, one pixel at a time
  23. k = 1;
  24. while (totalExcess ~= 0)
  25. %keep increasing the step as fewer and fewer pixels remain for
  26. %the redistribution (spread them evenly)
  27. stepSize = max(floor(numBins/totalExcess),1);
  28. for m=k:stepSize:numBins
  29. if imgHist(m) < clipLimit
  30. imgHist(m) = imgHist(m)+1;
  31. totalExcess = totalExcess - 1; %reduce excess
  32. if totalExcess == 0
  33. break;
  34. end
  35. end
  36. end
  37. k = k+1; %prevent from always placing the pixels in bin #1
  38. if k > numBins % start over if numBins was reached
  39. k = 1;
  40. end
  41. end

CLAHE和AHE中另一个重要的问题:插值

将图像进行分块处理,若每块中的像素点仅通过该块中的映射函数进行变换,则会导致最终图像呈块状效应:

为了解决这个问题,我们需要利用插值运算,也就是每个像素点出的值由它周围4个子块的映射函数值进行双线性插值得到

https://img-blog.csdn.net/20151102123121200

上图中,为了求蓝色像素点处的值,需要利用它周围四个子块的映射函数分别做变换得到四个映射值,再对这四个值做双线性插值即可。

当然对于边界处的像素点则不是通过四个子块进行插值,如上图红色像素点直接以一个子块的映射函数做变换,绿色像素则以两个子块做映射函数做线性插值。这里讲的边界处像素是指落在图像左上角,左下角、右上角,右下角的四个子块中心像素点围成的四边形之外的像素。如下图,将图像分为8x8子块,边界像素即落在灰色区域的像素点。

CLAHE算法的源代码参考:

  1. /*
  2. * ANSI C code from the article
  3. * "Contrast Limited Adaptive Histogram Equalization"
  4. * by Karel Zuiderveld, karel@cv.ruu.nl
  5. * in "Graphics Gems IV", Academic Press, 1994
  6. *
  7. *
  8. * These functions implement Contrast Limited Adaptive Histogram Equalization.
  9. * The main routine (CLAHE) expects an input image that is stored contiguously in
  10. * memory; the CLAHE output image overwrites the original input image and has the
  11. * same minimum and maximum values (which must be provided by the user).
  12. * This implementation assumes that the X- and Y image resolutions are an integer
  13. * multiple of the X- and Y sizes of the contextual regions. A check on various other
  14. * error conditions is performed.
  15. *
  16. * #define the symbol BYTE_IMAGE to make this implementation suitable for
  17. * 8-bit images. The maximum number of contextual regions can be redefined
  18. * by changing uiMAX_REG_X and/or uiMAX_REG_Y; the use of more than 256
  19. * contextual regions is not recommended.
  20. *
  21. * The code is ANSI-C and is also C++ compliant.
  22. *
  23. * Author: Karel Zuiderveld, Computer Vision Research Group,
  24. * Utrecht, The Netherlands (karel@cv.ruu.nl)
  25. */
  26. #ifdef BYTE_IMAGE
  27. typedef unsigned char kz_pixel_t; /* for 8 bit-per-pixel images */
  28. #define uiNR_OF_GREY (256)
  29. #else
  30. typedef unsigned short kz_pixel_t; /* for 12 bit-per-pixel images (default) */
  31. # define uiNR_OF_GREY (4096)
  32. #endif
  33. /******** Prototype of CLAHE function. Put this in a separate include file. *****/
  34. int CLAHE(kz_pixel_t* pImage, unsigned int uiXRes, unsigned int uiYRes, kz_pixel_t Min,
  35. kz_pixel_t Max, unsigned int uiNrX, unsigned int uiNrY,
  36. unsigned int uiNrBins, float fCliplimit);
  37. /*********************** Local prototypes ************************/
  38. static void ClipHistogram (unsigned long*, unsigned int, unsigned long);
  39. static void MakeHistogram (kz_pixel_t*, unsigned int, unsigned int, unsigned int,
  40. unsigned long*, unsigned int, kz_pixel_t*);
  41. static void MapHistogram (unsigned long*, kz_pixel_t, kz_pixel_t,
  42. unsigned int, unsigned long);
  43. static void MakeLut (kz_pixel_t*, kz_pixel_t, kz_pixel_t, unsigned int);
  44. static void Interpolate (kz_pixel_t*, int, unsigned long*, unsigned long*,
  45. unsigned long*, unsigned long*, unsigned int, unsigned int, kz_pixel_t*);
  46. /************** Start of actual code **************/
  47. #include <stdlib.h> /* To get prototypes of malloc() and free() */
  48. const unsigned int uiMAX_REG_X = 16; /* max. # contextual regions in x-direction */
  49. const unsigned int uiMAX_REG_Y = 16; /* max. # contextual regions in y-direction */
  50. /************************** main function CLAHE ******************/
  51. int CLAHE (kz_pixel_t* pImage, unsigned int uiXRes, unsigned int uiYRes,
  52. kz_pixel_t Min, kz_pixel_t Max, unsigned int uiNrX, unsigned int uiNrY,
  53. unsigned int uiNrBins, float fCliplimit)
  54. /* pImage - Pointer to the input/output image
  55. * uiXRes - Image resolution in the X direction
  56. * uiYRes - Image resolution in the Y direction
  57. * Min - Minimum greyvalue of input image (also becomes minimum of output image)
  58. * Max - Maximum greyvalue of input image (also becomes maximum of output image)
  59. * uiNrX - Number of contextial regions in the X direction (min 2, max uiMAX_REG_X)
  60. * uiNrY - Number of contextial regions in the Y direction (min 2, max uiMAX_REG_Y)
  61. * uiNrBins - Number of greybins for histogram ("dynamic range")
  62. * float fCliplimit - Normalized cliplimit (higher values give more contrast)
  63. * The number of "effective" greylevels in the output image is set by uiNrBins; selecting
  64. * a small value (eg. 128) speeds up processing and still produce an output image of
  65. * good quality. The output image will have the same minimum and maximum value as the input
  66. * image. A clip limit smaller than 1 results in standard (non-contrast limited) AHE.
  67. */
  68. {
  69. unsigned int uiX, uiY; /* counters */
  70. unsigned int uiXSize, uiYSize, uiSubX, uiSubY; /* size of context. reg. and subimages */
  71. unsigned int uiXL, uiXR, uiYU, uiYB; /* auxiliary variables interpolation routine */
  72. unsigned long ulClipLimit, ulNrPixels;/* clip limit and region pixel count */
  73. kz_pixel_t* pImPointer; /* pointer to image */
  74. kz_pixel_t aLUT[uiNR_OF_GREY]; /* lookup table used for scaling of input image */
  75. unsigned long* pulHist, *pulMapArray; /* pointer to histogram and mappings*/
  76. unsigned long* pulLU, *pulLB, *pulRU, *pulRB; /* auxiliary pointers interpolation */
  77. if (uiNrX > uiMAX_REG_X) return -1; /* # of regions x-direction too large */
  78. if (uiNrY > uiMAX_REG_Y) return -2; /* # of regions y-direction too large */
  79. if (uiXRes % uiNrX) return -3; /* x-resolution no multiple of uiNrX */
  80. if (uiYRes % uiNrY) return -4; /* y-resolution no multiple of uiNrY */
  81. if (Max >= uiNR_OF_GREY) return -5; /* maximum too large */
  82. if (Min >= Max) return -6; /* minimum equal or larger than maximum */
  83. if (uiNrX < 2 || uiNrY < 2) return -7;/* at least 4 contextual regions required */
  84. if (fCliplimit == 1.0) return 0; /* is OK, immediately returns original image. */
  85. if (uiNrBins == 0) uiNrBins = 128; /* default value when not specified */
  86. pulMapArray=(unsigned long *)malloc(sizeof(unsigned long)*uiNrX*uiNrY*uiNrBins);
  87. if (pulMapArray == 0) return -8; /* Not enough memory! (try reducing uiNrBins) */
  88. uiXSize = uiXRes/uiNrX; uiYSize = uiYRes/uiNrY; /* Actual size of contextual regions */
  89. ulNrPixels = (unsigned long)uiXSize * (unsigned long)uiYSize;
  90. if(fCliplimit > 0.0) { /* Calculate actual cliplimit */
  91. ulClipLimit = (unsigned long) (fCliplimit * (uiXSize * uiYSize) / uiNrBins);
  92. ulClipLimit = (ulClipLimit < 1UL) ? 1UL : ulClipLimit;
  93. }
  94. else ulClipLimit = 1UL<<14; /* Large value, do not clip (AHE) */
  95. MakeLut(aLUT, Min, Max, uiNrBins); /* Make lookup table for mapping of greyvalues */
  96. /* Calculate greylevel mappings for each contextual region */
  97. for (uiY = 0, pImPointer = pImage; uiY < uiNrY; uiY++) {
  98. for (uiX = 0; uiX < uiNrX; uiX++, pImPointer += uiXSize) {
  99. pulHist = &pulMapArray[uiNrBins * (uiY * uiNrX + uiX)];
  100. MakeHistogram(pImPointer,uiXRes,uiXSize,uiYSize,pulHist,uiNrBins,aLUT);
  101. ClipHistogram(pulHist, uiNrBins, ulClipLimit);
  102. MapHistogram(pulHist, Min, Max, uiNrBins, ulNrPixels);
  103. }
  104. pImPointer += (uiYSize - 1) * uiXRes; /* skip lines, set pointer */
  105. }
  106. /* Interpolate greylevel mappings to get CLAHE image */
  107. for (pImPointer = pImage, uiY = 0; uiY <= uiNrY; uiY++) {
  108. if (uiY == 0) { /* special case: top row */
  109. uiSubY = uiYSize >> 1; uiYU = 0; uiYB = 0;
  110. }
  111. else {
  112. if (uiY == uiNrY) { /* special case: bottom row */
  113. uiSubY = (uiYSize+1) >> 1; uiYU = uiNrY-1; uiYB = uiYU;
  114. }
  115. else { /* default values */
  116. uiSubY = uiYSize; uiYU = uiY - 1; uiYB = uiYU + 1;
  117. }
  118. }
  119. for (uiX = 0; uiX <= uiNrX; uiX++) {
  120. if (uiX == 0) { /* special case: left column */
  121. uiSubX = uiXSize >> 1; uiXL = 0; uiXR = 0;
  122. }
  123. else {
  124. if (uiX == uiNrX) { /* special case: right column */
  125. uiSubX = (uiXSize+1) >> 1; uiXL = uiNrX - 1; uiXR = uiXL;
  126. }
  127. else { /* default values */
  128. uiSubX = uiXSize; uiXL = uiX - 1; uiXR = uiXL + 1;
  129. }
  130. }
  131. pulLU = &pulMapArray[uiNrBins * (uiYU * uiNrX + uiXL)];
  132. pulRU = &pulMapArray[uiNrBins * (uiYU * uiNrX + uiXR)];
  133. pulLB = &pulMapArray[uiNrBins * (uiYB * uiNrX + uiXL)];
  134. pulRB = &pulMapArray[uiNrBins * (uiYB * uiNrX + uiXR)];
  135. Interpolate(pImPointer,uiXRes,pulLU,pulRU,pulLB,pulRB,uiSubX,uiSubY,aLUT);
  136. pImPointer += uiSubX; /* set pointer on next matrix */
  137. }
  138. pImPointer += (uiSubY - 1) * uiXRes;
  139. }
  140. free(pulMapArray); /* free space for histograms */
  141. return 0; /* return status OK */
  142. }
  143. void ClipHistogram (unsigned long* pulHistogram, unsigned int
  144. uiNrGreylevels, unsigned long ulClipLimit)
  145. /* This function performs clipping of the histogram and redistribution of bins.
  146. * The histogram is clipped and the number of excess pixels is counted. Afterwards
  147. * the excess pixels are equally redistributed across the whole histogram (providing
  148. * the bin count is smaller than the cliplimit).
  149. */
  150. {
  151. unsigned long* pulBinPointer, *pulEndPointer, *pulHisto;
  152. unsigned long ulNrExcess, ulUpper, ulBinIncr, ulStepSize, i;
  153. long lBinExcess;
  154. ulNrExcess = 0; pulBinPointer = pulHistogram;
  155. for (i = 0; i < uiNrGreylevels; i++) { /* calculate total number of excess pixels */
  156. lBinExcess = (long) pulBinPointer[i] - (long) ulClipLimit;
  157. if (lBinExcess > 0) ulNrExcess += lBinExcess; /* excess in current bin */
  158. };
  159. /* Second part: clip histogram and redistribute excess pixels in each bin */
  160. ulBinIncr = ulNrExcess / uiNrGreylevels; /* average binincrement */
  161. ulUpper = ulClipLimit - ulBinIncr; /* Bins larger than ulUpper set to cliplimit */
  162. for (i = 0; i < uiNrGreylevels; i++) {
  163. if (pulHistogram[i] > ulClipLimit) pulHistogram[i] = ulClipLimit; /* clip bin */
  164. else {
  165. if (pulHistogram[i] > ulUpper) { /* high bin count */
  166. ulNrExcess -= pulHistogram[i] - ulUpper; pulHistogram[i]=ulClipLimit;
  167. }
  168. else { /* low bin count */
  169. ulNrExcess -= ulBinIncr; pulHistogram[i] += ulBinIncr;
  170. }
  171. }
  172. }
  173. while (ulNrExcess) { /* Redistribute remaining excess */
  174. pulEndPointer = &pulHistogram[uiNrGreylevels]; pulHisto = pulHistogram;
  175. while (ulNrExcess && pulHisto < pulEndPointer) {
  176. ulStepSize = uiNrGreylevels / ulNrExcess;
  177. if (ulStepSize < 1) ulStepSize = 1; /* stepsize at least 1 */
  178. for (pulBinPointer=pulHisto; pulBinPointer < pulEndPointer && ulNrExcess;
  179. pulBinPointer += ulStepSize) {
  180. if (*pulBinPointer < ulClipLimit) {
  181. (*pulBinPointer)++; ulNrExcess--; /* reduce excess */
  182. }
  183. }
  184. pulHisto++; /* restart redistributing on other bin location */
  185. }
  186. }
  187. }
  188. void MakeHistogram (kz_pixel_t* pImage, unsigned int uiXRes,
  189. unsigned int uiSizeX, unsigned int uiSizeY,
  190. unsigned long* pulHistogram,
  191. unsigned int uiNrGreylevels, kz_pixel_t* pLookupTable)
  192. /* This function classifies the greylevels present in the array image into
  193. * a greylevel histogram. The pLookupTable specifies the relationship
  194. * between the greyvalue of the pixel (typically between 0 and 4095) and
  195. * the corresponding bin in the histogram (usually containing only 128 bins).
  196. */
  197. {
  198. kz_pixel_t* pImagePointer;
  199. unsigned int i;
  200. for (i = 0; i < uiNrGreylevels; i++) pulHistogram[i] = 0L; /* clear histogram */
  201. for (i = 0; i < uiSizeY; i++) {
  202. pImagePointer = &pImage[uiSizeX];
  203. while (pImage < pImagePointer) pulHistogram[pLookupTable[*pImage++]]++;
  204. pImagePointer += uiXRes;
  205. pImage = &pImagePointer[-(int)uiSizeX]; /* go to bdeginning of next row */
  206. }
  207. }
  208. void MapHistogram (unsigned long* pulHistogram, kz_pixel_t Min, kz_pixel_t Max,
  209. unsigned int uiNrGreylevels, unsigned long ulNrOfPixels)
  210. /* This function calculates the equalized lookup table (mapping) by
  211. * cumulating the input histogram. Note: lookup table is rescaled in range [Min..Max].
  212. */
  213. {
  214. unsigned int i; unsigned long ulSum = 0;
  215. const float fScale = ((float)(Max - Min)) / ulNrOfPixels;
  216. const unsigned long ulMin = (unsigned long) Min;
  217. for (i = 0; i < uiNrGreylevels; i++) {
  218. ulSum += pulHistogram[i]; pulHistogram[i]=(unsigned long)(ulMin+ulSum*fScale);
  219. if (pulHistogram[i] > Max) pulHistogram[i] = Max;
  220. }
  221. }
  222. void MakeLut (kz_pixel_t * pLUT, kz_pixel_t Min, kz_pixel_t Max, unsigned int uiNrBins)
  223. /* To speed up histogram clipping, the input image [Min,Max] is scaled down to
  224. * [0,uiNrBins-1]. This function calculates the LUT.
  225. */
  226. {
  227. int i;
  228. const kz_pixel_t BinSize = (kz_pixel_t) (1 + (Max - Min) / uiNrBins);
  229. for (i = Min; i <= Max; i++) pLUT[i] = (i - Min) / BinSize;
  230. }
  231. void Interpolate (kz_pixel_t * pImage, int uiXRes, unsigned long * pulMapLU,
  232. unsigned long * pulMapRU, unsigned long * pulMapLB, unsigned long * pulMapRB,
  233. unsigned int uiXSize, unsigned int uiYSize, kz_pixel_t * pLUT)
  234. /* pImage - pointer to input/output image
  235. * uiXRes - resolution of image in x-direction
  236. * pulMap* - mappings of greylevels from histograms
  237. * uiXSize - uiXSize of image submatrix
  238. * uiYSize - uiYSize of image submatrix
  239. * pLUT - lookup table containing mapping greyvalues to bins
  240. * This function calculates the new greylevel assignments of pixels within a submatrix
  241. * of the image with size uiXSize and uiYSize. This is done by a bilinear interpolation
  242. * between four different mappings in order to eliminate boundary artifacts.
  243. * It uses a division; since division is often an expensive operation, I added code to
  244. * perform a logical shift instead when feasible.
  245. */
  246. {
  247. const unsigned int uiIncr = uiXRes-uiXSize; /* Pointer increment after processing row */
  248. kz_pixel_t GreyValue; unsigned int uiNum = uiXSize*uiYSize; /* Normalization factor */
  249. unsigned int uiXCoef, uiYCoef, uiXInvCoef, uiYInvCoef, uiShift = 0;
  250. if (uiNum & (uiNum - 1)) /* If uiNum is not a power of two, use division */
  251. for (uiYCoef = 0, uiYInvCoef = uiYSize; uiYCoef < uiYSize;
  252. uiYCoef++, uiYInvCoef--,pImage+=uiIncr) {
  253. for (uiXCoef = 0, uiXInvCoef = uiXSize; uiXCoef < uiXSize;
  254. uiXCoef++, uiXInvCoef--) {
  255. GreyValue = pLUT[*pImage]; /* get histogram bin value */
  256. *pImage++ = (kz_pixel_t ) ((uiYInvCoef * (uiXInvCoef*pulMapLU[GreyValue]
  257. + uiXCoef * pulMapRU[GreyValue])
  258. + uiYCoef * (uiXInvCoef * pulMapLB[GreyValue]
  259. + uiXCoef * pulMapRB[GreyValue])) / uiNum);
  260. }
  261. }
  262. else { /* avoid the division and use a right shift instead */
  263. while (uiNum >>= 1) uiShift++; /* Calculate 2log of uiNum */
  264. for (uiYCoef = 0, uiYInvCoef = uiYSize; uiYCoef < uiYSize;
  265. uiYCoef++, uiYInvCoef--,pImage+=uiIncr) {
  266. for (uiXCoef = 0, uiXInvCoef = uiXSize; uiXCoef < uiXSize;
  267. uiXCoef++, uiXInvCoef--) {
  268. GreyValue = pLUT[*pImage]; /* get histogram bin value */
  269. *pImage++ = (kz_pixel_t)((uiYInvCoef* (uiXInvCoef * pulMapLU[GreyValue]
  270. + uiXCoef * pulMapRU[GreyValue])
  271. + uiYCoef * (uiXInvCoef * pulMapLB[GreyValue]
  272. + uiXCoef * pulMapRB[GreyValue])) >> uiShift);
  273. }
  274. }
  275. }
  276. }

参考:

[1] https://wenku.baidu.com/view/03c54c02760bf78a6529647d27284b73f342368b.html

[2] https://blog.csdn.net/u010839382/article/details/49584181

[3] https://en.wikipedia.org/wiki/Adaptive_histogram_equalization

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/78880
推荐阅读
相关标签
  

闽ICP备14008679号