version 1.0.0.0 (3.8 KB) by
Leslie Smith

Best to read the reference in "Graphics Gems IV", Academic Press, 1994 pages 474-485

This is an image contrast enhancement algorithm that overcomes limitations in standard histogram equalization (HE). The two primary features is adaptive HE (AHE), which divides the images into regions and performs local HE, and the contrast limited AHE (CLAHE), which reduces noise by partially reducing the local HE. Bilinear interpolation is used to avoid visibility of region boundaries.

"Contrast Limited Adaptive Histogram Equalization"

by Karel Zuiderveld, karel@cv.ruu.nl

in "Graphics Gems IV", Academic Press, 1994

Ported by Leslie Smith

Leslie Smith (2021). Contrast Limited Adaptive Histogram Equalization (CLAHE) (https://www.mathworks.com/matlabcentral/fileexchange/22182-contrast-limited-adaptive-histogram-equalization-clahe), MATLAB Central File Exchange. Retrieved .

Created with
R2007a

Compatible with any release

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

DGMI was looking for something like this to remove some of my toolbox's dependency on IPT. This is definitely better than some of the other CLAHE implementations I've tried on the FEX, but I found I was spending way too much time trying to debug it. I just ended up making my own implementation and adding it to the MIMT. If anybody wants a CLAHE tool that matches syntax and behavior of adapthisteq(), try that.

Habtamu AbrahamNrX - Number of contextial regions in the X direction (min 2, max uiMAX_REG_X)

What is contextial region?

Padhu CatSelva KarnaHow can i clear this ? MR Leslie Smith

Error in runCLAHE (line 25)

Bin=1+LUT(round(Image));

xiuwei zhangLeslie SmithPeleg,

Let me first say that Matlab provides a CLAHE implementation called adapthisteq and it is much more robust than mine. If there is any way you can use it, I recommend using it.

Second, the error message is because some of your Image values are 0. I recommend you set Image = 1 + Image; and try again. Matlab doesn’t allow 0 as an index.

Third, contextial regions means you break up the image into patches and run histogram equalization within these sub-regions rather than on the whole image, which helps it to work better.

Fourth, I wrote this about 10 years ago and don’t really remember it.

Fifth, I hope your work goes well.

Best regards,

Leslie

Meghana DineshMy image is a 800 X 1200(width) gray scale image (8bpp).

My input parameters are:

XRes=800;

YRes=1200;

Min=0;

Max=247;;

NrX=8;

NrY=8;

NrBins=256;

Cliplimit=0.01;

Meghana DineshHi,

I have tried a lot to run this code, especially considering Assed Highs's comments. This is what I did:

1. Replaced "Bin = 1 + LUT(round(Image));" by

"Bin=1+LUT(round(Image+1));"

Error:

In makeHistogram.m, the following line gives me an error saying index exceeds matrix dimensions:

"bin=Bin(1+(i-1)*XSize:i*XSize,1+(j-1)*YSize:j*YSize);"

2. Replaced "LUT=makeLUT(Min,Max,NrBins);

% avgBin = NrPixels/NrBins;

Bin=1+LUT(round(Image));"

by

"LUT=makeLUT(Min,Max,NrBins);

avgBin = NrPixels/NrBins;

LUT2=zeros(255,1);

[x]=size(LUT);

LUT2(1:x(1),1)=LUT;

Bin=1+LUT2(round(Image));"

Error:

Subscript indices must be either real positive integers or logicals.

Kindly help.

EiGeNmuhammadPortgas Acecan't seem to make it work. Can you give me a sample on how to use this?

SeyoungIf error occured "runCLAHE.m" at 53,

Replace "Bin= 1 + LUT(round(Image));" for:

"Bin=1+LUT(round(Image+1));"

and check the function arguements one more time.

Tuhin SheikhHow to modify the following codes for Gaussian specification ?

case 'rayleigh', % suitable for underwater imagery

% pdf = (t./alpha^2).*exp(-t.^2/(2*alpha^2))*U(t)

% cdf = 1-exp(-t.^2./(2*alpha^2))

hconst = 2*alpha^2;

vmax = 1 - exp(-1/hconst);

val = vmax*(histSum/numPixInTile);

val(val>=1) = 1-eps; % avoid log(0)

temp = sqrt(-hconst*log(1-val));

mapping = min(selectedRange(1)+temp*valSpread,...

selectedRange(2));

Assed HighsOK, my last comment haha, I don't know what I was thinking about using "adapthisteq" function. SOLUTION:

- Add the following code to runCLAHE.m:

LUT=makeLUT(Min,Max,NrBins);

avgBin = NrPixels/NrBins;

LUT2=zeros(graylevels,1);

[x]=size(LUT);

LUT2(1:x(1),1)=LUT;

Bin=1+LUT2(round(Image));

graylevels is the color depth of used image. If it's 8 bit, just replace "graylevels" for 255.

- If you still having the error, change "Bin=1+LUT2(round(Image));" for "Bin=1+LUT2(round(Image+1))" to avoid pixels with 0 value.

Assed HighsOne last comment, just to clariry. To avoid "index out of bounds":

- Add the following code to runCLAHE.m:

LUT=makeLUT(Min,Max,NrBins);

avgBin = NrPixels/NrBins;

LUT2=zeros(graylevels,1);

[x]=size(LUT);

LUT2(1:x(1),1)=LUT;

Bin=1+LUT2(round(Image));

graylevels is the color depth of used image. If it's 8 bit, just replace "graylevels" for 255.

- Right before calling runCLAHE.m, add:

"image=adapthisteq(image);"

- If you still having the error, change "Bin=1+LUT2(round(Image));" for "Bin=1+LUT2(round(Image+1))" to avoid pixels with 0 value (if there's still one after applying adapthisteq function).

That should work.

Assed HighsFixed, it was my fault.

Just make sure that "Max" and "Min" are double because if some of those are integers, maphistogram.m will make and empty map.

Works perfect, thanks for sharing!

Assed HighsOk, forget what I said in my last comment. Add this to the code:

LUT=makeLUT(Min,Max,NrBins);

avgBin = NrPixels/NrBins;

LUT2=zeros(graylevels,1);

[x]=size(LUT);

LUT2(1:x(1),1)=LUT;

Bin=1+LUT2(round(Image));

graylevels is the color depth of used image. If it's 8 bit, just replace "graylevels" for 255.

Anyway, the function always returns an empty matrix. I would try to fix it.

Thanks,

Assed HighsThanks!

The error is related to the value of the pixel. Whenever you got a pixel with 0 value you will get that error because Matlab does not consider it as an index value.

Replace "Bin= 1 + LUT(round(Image));" for:

"Bin=1+LUT(round(Image+1));"

It will fix the error.

On the other hand, whenever I try this algorithm, the output is a matrix with the same size of the original image but with zeros. Someone knows what the problem is?

HectorI am reviewing the article of Zuiderveld, but I don't what do you want with the line:

Bin= 1 + LUT(round(Image));"

maybe resizing the LUT array?

Leslie SmithI think the error might be related to the image size - when I wrote it I only tested it on even sized images.

Let me again recommend using adapthisteq in the Image Processing Toolbox. It is more robust and doesn't have these limitations.

Pratik Oakhey guys.. I also got error like this

"subscript should be real positive index"

"Error in ==> runCLAHE at 55

Bin= 1 + LUT(round(Image));"

LIAO xjquite useful! thanks a lot!

Leslie SmithThe full argument list is

runCLAHE(Image,XRes,YRes,Min,Max,NrX,NrY,NrBins,Cliplimit,modHist)

The comments in the beginning of the function describe the arguments.

Once again I will suggest you use adapthisteq in the Image processing toolbox because Matlab's routine is more robust.

RadoExcuse me, this may be a stupid question, but can you tell me how to use this function?

I tried to define the variable "Image" to be the path of the image I want to process. Then I removed the comment symbols in front of:

% [XRes,YRes]=size(Image);

% CEimage = Image;

in "runCLAHE.m".

I typed the command:

J=runCLAHE(Image);

The error I got was:

?? Input argument "Cliplimit" is undefined.

Error in ==> runCLAHE at 34

if Cliplimit == 1

Can you help me with that?

Again, sorry for the stupid question and thanks in advance!

Leslie SmithThe "Subscript indices must either be real positive integers or logicals" is basically that the index is out of bounds.

Obviously you are taking a class where the assignment is to create the CLAHE code. IMO, that is what you should do because the goal of a class is the learning, not the grade.

FreyssinetaWhen i execute runCLAHE.m the error is "Subscript indices must either be real positive integers or logicals"

yup i know about adapthistheq function in matlab. But i did'nt allowed to use that function with my lecturer so i must make clahe code manually.

What should i do?

Leslie SmithI suggest you use adapthisteq in the Image processing toolbox because Matlab's routine is more robust.

Although you don't say what the error is, my guess is that it is "index out of bounds" for the look up table LUT. If I remember correctly, runCLAHE needs for the dimensions of the image to be even to work properly. Again, you are likely better off to use adapthisteq.

Freyssinetahey guys.. I got error like this

"Error in ==> runCLAHE at 55

Bin= 1 + LUT(round(Image));"

Do you know the solution?

please help me. I need it

Alireza SaberiThank you much, I really was in need to this codes

IdillusLeslie SmithThanks for the reference. I had looked for and did not find the Matlab CLAHE and was surprised when I thought it did not exist.

Leslie

Steve EddinsYou might want to take a look at adapthisteq, which is in the Image Processing Toolbox. This function also implements CLAHE. See:

http://www.mathworks.com/access/helpdesk/help/toolbox/images/adapthisteq.html