2016-08-14 4 views
0

Ich implementiere lokale Histogrammentzerrung manuell, aber das Ergebnis ist nicht befriedigend Was ich will und was ich bekomme, ist im Bild, ich habe den Code nach gegebenem Ausdruck implementiert, aber die Ergebnisse sind nicht gleich. HierLokale Histogrammentzerrung manuell

Picture for problem

ist der 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); 

Antwort

1

Ich denke, es mehr als eine Formel für "Local Histogram Equalization" - Sie implementiert eine andere Formel als Ihr wonted Ergebnis.

fand ich eine Implementierung hier: http://angeljohnsy.blogspot.com/2011/06/local-histogram-equalization.html

ich den Code geändert Eingabe zu verwenden.

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

Das Ergebnis sieht aus wie die, die Sie wollten:
enter image description here