Reputation: 54
I am implementing local histogram equalization manually but the result is not satisfactory What I want and what I am getting is in picture, I have implemented the code according to given expression but still results are not same.
Here is the code:
I=rgb2gray(imread('peppers.png'));
if (isa(I,'uint8'))
I=double(I)/255;
end
if (size(I,3)==3)
I=(I(:,:,1)+I(:,:,2)+I(:,:,3))/3; % average the RGB channels
end
windowsize=17;
% Create an empty array
Ieq = zeros(size(I,1),size(I,2));
% Apply this over a NxN region around each pixel (N is odd)
n = floor(windowsize/2); % <-- N is the half size of the region ie. [-N:N,-N:N]
for r=1+n:size(I,1)-n
for c=1+n:size(I,2)-n
% -- INSERT YOUR CODE BELOW ------------------------------------------
% NOTE: For pixels near the boundary, ensure the NxN neighbourhood is still
% inside the image (this means for pixels near the boundary the pixel may
% not be at the centre of the NxN neighbourhood).
if r-n <=1
fromrow=1;
torow=r+n;
else
fromrow=abs(r-n);
if n+r >= size(I,1)
torow=size(I,1);
else
torow=r+n;
end
end
if c-n <= 1
fromcol=1;
tocol=c+n;
else
fromcol=abs(c-n);
if c+n > size(I,2);
tocol=size(I,2);
else
tocol=c+n;
end
end
neighbour = I(fromrow:torow,fromcol:tocol);
lessoreq=neighbour(neighbour<=I(r,c));
sumofval=sum(lessoreq);
pixval=sumofval/(size(neighbour,1)*size(neighbour,2));
Ieq(r,c)=pixval;
% -- INSERT YOUR CODE ABOVE ------------------------------------------
end
end
imshow(Ieq);
Upvotes: 0
Views: 1334
Reputation: 32094
I guess there is more than one formula for "Local Histogram Equalization" - you implemented a different formula than your wonted result.
I found an implementation here: http://angeljohnsy.blogspot.com/2011/06/local-histogram-equalization.html
I modified the code to use you input.
%http://angeljohnsy.blogspot.com/2011/06/local-histogram-equalization.html
A=rgb2gray(imread('peppers.png'));
figure,imshow(A);
Img=A;
%WINDOW SIZE
M=17;
N=17;
mid_val=round((M*N)/2);
%FIND THE NUMBER OF ROWS AND COLUMNS TO BE PADDED WITH ZERO
in=0;
for i=1:M
for j=1:N
in=in+1;
if(in==mid_val)
PadM=i-1;
PadN=j-1;
break;
end
end
end
%PADDING THE IMAGE WITH ZERO ON ALL SIDES
B=padarray(A,[PadM,PadN]);
for i= 1:size(B,1)-((PadM*2)+1)
for j=1:size(B,2)-((PadN*2)+1)
cdf=zeros(256,1);
inc=1;
for x=1:M
for y=1:N
%FIND THE MIDDLE ELEMENT IN THE WINDOW
if(inc==mid_val)
ele=B(i+x-1,j+y-1)+1;
end
pos=B(i+x-1,j+y-1)+1;
cdf(pos)=cdf(pos)+1;
inc=inc+1;
end
end
%COMPUTE THE CDF FOR THE VALUES IN THE WINDOW
for l=2:256
cdf(l)=cdf(l)+cdf(l-1);
end
Img(i,j)=round(cdf(ele)/(M*N)*255);
end
end
figure,imshow(Img);
figure,
subplot(2,1,1);title('Before Local Histogram Equalization'); imhist(A);
subplot(2,1,2);title('After Local Histogram Equalization'); imhist(Img);
The result looks like the one you wanted:
Upvotes: 1