2013-11-03 59 views
0

你好我是新的Matlab..I我試圖做直方圖equilzation不使用histeq ...Matlab的直方圖equilization不使用內置的功能

,但由於某種原因,我總是得到一個錯誤:???索引超過矩陣尺寸。

這裏是我的代碼.......................................... ........感謝您的幫助

clc 

    I = imread ('Machine-Edge.PNG'); 
    I2 = rgb2gray(I); 
    colormap gray; 

    y = imhist(I2); 


    %using hist eq. built in fn 
    I3= histeq(I2); 
    z= imhist(I3); 

    %my equalization 
    r = size(I2,1); 
    c = size(I2,2); 
    A= zeros(1,256); 

    %counting number of pixels of the image and putting the count in Array A 
    for j=1:r 
    for x=1:c 
     v=I2(j,x); 
     A(v+1)=A(v+1)+1; 
    end 
    end 

    %pi=n/size 
    for y=1;256 
     pi(y)= ((A(y))/(r*c)); 
    end 

    %calculate CI (cumulated pi) 
    ci(1)=pi(1); 
    for yy=2;256 
     ci(yy) = ci(yy-1)+ pi(yy); 
    end 

    %calculate T=range *Ci 
     for b=1;256 
     T(b)=ci(b)*255; 
     end 

    %equilization..replacing each pixel with T value 
    for j=1:r 
     for x=1:c 
      I4(j,x) =T(I2(j,x)); 

      end 
    end 




    vv= imhist(I4); 



    figure 
    subplot(3,2,1) 
    imagesc(I2) 
    subplot(3,2,2) 
    plot(y) 

    subplot(3,2,3) 
    imagesc(I3) 
    subplot(3,2,4) 
    plot(z) 

    subplot(3,2,5) 
    imagesc(I4) 
    subplot(3,2,6) 
    plot(vv) 
+0

錯誤是什麼線路? – Cosades

回答

0

這是一箇舊的帖子,但OP使用;而不是:在它們的for循環中(即對於y = 1;對於y = 1:256,應當讀取256)。正確的代碼如下:

clc 

I = imread ('Machine-Edge.PNG'); 
I2 = rgb2gray(I); 
colormap gray; 

y = imhist(I2); 


%using hist eq. built in fn 
I3= histeq(I2); 
z= imhist(I3); 

%my equalization 
r = size(I2,1); 
c = size(I2,2); 
A= zeros(1,256); 

%counting number of pixels of the image and putting the count in Array A 
for j=1:r 
    for x=1:c 
     v=I2(j,x); 
     A(v+1)=A(v+1)+1; 
    end 
end 

%pi=n/size 
    for y=1:256 
    pi(y)= ((A(y))/(r*c)); 
    end 

    %calculate CI (cumulated pi) 
    ci(1)=pi(1); 
    for yy=2:256 
     ci(yy) = ci(yy-1)+ pi(yy); 
    end 

    %calculate T=range *Ci 
    for b=1:256 
     T(b)=ci(b)*255; 
     end 

    %equilization..replacing each pixel with T value 
    for j=1:r 
     for x=1:c 
      I4(j,x) =T(I2(j,x)); 

     end 
    end 




    vv= imhist(I4); 



    figure 
    subplot(3,2,1) 
    imagesc(I2) 
    subplot(3,2,2) 
    plot(y) 

subplot(3,2,3) 
imagesc(I3) 
    subplot(3,2,4) 
    plot(z) 

    subplot(3,2,5) 
    imagesc(I4) 
    subplot(3,2,6) 
    plot(vv)