├── README.md └── fn_CLAHE.m /README.md: -------------------------------------------------------------------------------- 1 | # CLAHE 2 | Contrast Limited Adaptive Histogram Equalization 3 | Change your cliplimit for better outputs. 4 | 5 | The simple histogram method suffers from intensity saturation which results 6 | in information loss, which is not acceptable in the case of medical images. Thus, 7 | whenever we want to retain the details of the image while improving the quality 8 | of the image, CLAHE is one of the best option to go for. In medical images, 9 | where intensity of the images are very dark, CLAHE provides a better image. 10 | 11 | Steps to run the code: 12 | 1. Open MATLAB and go to the path where fn_CLAHE.m is stored. 13 | 2. Give the image name in Run as 'example.jpg' 14 | 3. Run the program and see your result 15 | -------------------------------------------------------------------------------- /fn_CLAHE.m: -------------------------------------------------------------------------------- 1 | % This program is used to perform Contrast Limited Adaptive Histogram 2 | % Equalization to the given image. 3 | % Author : Ashwini Singh-HW 2 Computer Vision 4 | % Input : london.jpg(RGB image) 5 | 6 | function []=fn_CLAHE(filename) 7 | %% First pass: Count equal pixels 8 | data=imread(filename); % filename : 'london.jpg' 9 | data=rgb2gray(data); 10 | t=1; % start index of the window (row) 11 | limit=8; % window size of the contextual area 12 | endt=limit; % end index of the window (row) 13 | eqdata=zeros(size(data,1),size(data,2)); 14 | for x=1:size(data,1) 15 | q=1; % start index of the window (column) 16 | endq=limit; % end index of the window (column) 17 | %% TO move Window to right and bottom, after exceeding the limit 18 | for y=1:size(data,2) 19 | eqdata(x,y)=0; 20 | if (x>t+limit-1) 21 | t=t+limit; 22 | endt=limit+t-1; 23 | end 24 | if (y>q+limit-1) 25 | q=q+limit; 26 | endq=limit+q-1; 27 | end 28 | if (endt>size(data,1)) 29 | % t=t-64; 30 | endt=size(data,1); 31 | end 32 | if (endq>size(data,2)) 33 | % q=q-64; 34 | endq=size(data,2); 35 | end 36 | %% Counting the number of pixels in each contextual area 37 | for i=t:endt 38 | for j=q:endq 39 | 40 | if data(x,y)==data(i,j) 41 | eqdata(x,y)=eqdata(x,y)+1; 42 | end 43 | 44 | end 45 | end 46 | 47 | 48 | end 49 | end 50 | 51 | %% Second Pass: Calculate partial rank, redistributed area and output values. 52 | 53 | output=zeros(size(data,1),size(data,2)); 54 | cliplimit=0.3; % Cliplimit can vary between 0 to 1. 55 | t=1; 56 | endt=limit; 57 | for x=1:size(data,1) 58 | q=1; 59 | endq=limit; 60 | %% TO move Window to right and bottom, after exceeding the limit 61 | for y=1:size(data,2) 62 | 63 | cliptotal=0; 64 | partialrank=0; 65 | if (x>t+limit-1) 66 | t=t+limit; 67 | endt=limit+t-1; 68 | end 69 | if (y>q+limit-1) 70 | q=q+limit; 71 | endq=limit+q-1; 72 | end 73 | if (endt>size(data,1)) 74 | % t=t-64; 75 | endt=size(data,1); 76 | end 77 | if (endq>size(data,2)) 78 | % q=q-64; 79 | endq=size(data,2); 80 | end 81 | 82 | %% For each pixel (x,y), compare with cliplimit and accordingly do the clipping. Calculate partialrank. 83 | for i=t:endt 84 | for j=q:endq 85 | 86 | 87 | if eqdata(i,j)>cliplimit 88 | 89 | incr=cliplimit/eqdata(i,j); 90 | else 91 | incr=1; 92 | 93 | end 94 | cliptotal=cliptotal+(1-incr); 95 | 96 | if data(x,y)>data(i,j) 97 | partialrank=partialrank+incr; 98 | 99 | end 100 | 101 | end 102 | 103 | end 104 | %% New distributed pixel values can be found from redistr and will be incremented by partial rank. 105 | 106 | redistr=(cliptotal/(limit*limit)).*data(x,y); 107 | output(x,y)=partialrank+redistr; 108 | 109 | end 110 | end 111 | figure('name','ASHWINI SINGH','NumberTitle','off') 112 | %da=adapthisteq(data); 113 | imshow([data uint8(output)]); % Concatenate original and CLAHE image 114 | 115 | 116 | end --------------------------------------------------------------------------------