0

您好,我是 Matlab 的新手。我正在尝试在不使用 histeq 的情况下进行直方图均衡化...

但由于某种原因,我总是得到一个错误:???索引超出矩阵维度。

这是我的代码.................................................. ....谢谢你的帮助

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

1 回答 1

0

这是一个旧帖子,但使用了 OP;而不是 : 在他们的 for 循环中(即 for y=1;256 应该读为 y=1:​​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)
于 2014-06-18T03:28:34.913 回答