2016-08-14 133 views
0

我手动实现了局部直方图均衡,但结果并不令人满意我想要什么以及所得到的是在图片中,我已经根据给定的表达式实现了代码,但结果仍然不相同。手动局部直方图均衡

Picture for problem

下面是代码:

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

回答

1

我想有一个以上的公式“局部直方图均衡” - 你实现了一个不同的配方比你惯例的结果。

我发现了一个实现此:http://angeljohnsy.blogspot.com/2011/06/local-histogram-equalization.html

我修改为使用您输入的代码。

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

结果看起来像你想要的人:
enter image description here