View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Contrast Limited Adaptive Histogram Equalization (CLAHE)

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.0
4.0 | 6 ratings Rate this file 25 Downloads (last 30 days) File Size: 3.8 KB File ID: #22182 Version: 1.0

Contrast Limited Adaptive Histogram Equalization (CLAHE)

by

 

20 Nov 2008 (Updated )

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

| Watch this File

File Information
Description

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

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.4 (R2007a)
MATLAB Search Path
/
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
11 Aug 2016 Leslie Smith

Peleg,

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

Comment only
03 Mar 2016 Meghana Dinesh

My 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;

Comment only
03 Mar 2016 Meghana Dinesh

Hi,

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.

Comment only
17 Dec 2014 EiGeN

EiGeN (view profile)

 
30 Nov 2014 muhammad  
07 Oct 2014 Portgas Ace

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

Comment only
20 Aug 2014 Seyoung

If 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.

Comment only
12 Jan 2014 Tuhin Sheikh

How 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));

Comment only
28 Nov 2012 Assed Highs

OK, 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.

Comment only
28 Nov 2012 Assed Highs

One 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.

Comment only
28 Nov 2012 Assed Highs

Fixed, 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!

28 Nov 2012 Assed Highs

Ok, 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,

Comment only
28 Nov 2012 Assed Highs

Thanks!

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?

Comment only
01 Oct 2012 Hector

Hector (view profile)

I 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?

Comment only
27 Sep 2012 Leslie Smith

I 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.

Comment only
27 Sep 2012 Pratik Oak

hey guys.. I also got error like this
"subscript should be real positive index"

"Error in ==> runCLAHE at 55
Bin= 1 + LUT(round(Image));"

Comment only
27 Oct 2011 LIAO xj

quite useful! thanks a lot!

17 Jun 2011 Leslie Smith

The 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.

Comment only
17 Jun 2011 Rado

Rado (view profile)

Excuse 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!

Comment only
12 May 2011 Leslie Smith

The "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.

Comment only
12 May 2011 Freyssineta

When 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?

Comment only
11 May 2011 Leslie Smith

I 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.

Comment only
11 May 2011 Freyssineta

hey 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

Comment only
26 Apr 2010 Alireza Saberi

Thank you much, I really was in need to this codes

15 Apr 2009 Idillus  
20 Nov 2008 Leslie Smith

Thanks 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

Comment only
20 Nov 2008 Steve Eddins

Steve Eddins (view profile)

You 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

Comment only
Updates
05 Apr 2016 1.0

Best to use adapthisteq in Matlab's Image processing toolbox

Contact us