【问题标题】:Histogram equalisation code generates indexing error直方图均衡代码生成索引错误
【发布时间】:2019-09-02 18:24:15
【问题描述】:

我在不使用内置函数的情况下实现图像的直方图均衡,特别是 imhisthisteq。我只能使用cumsumhisthistcounts。我有一个测试函数,可以检查我的代码中我添加的不同值以供参考。

这是我的代码:

function eq_img = histeq_contrast(img)
%input image data is assumed to be in range 0..1
image = img;
[m,n] = size(image);
L = 256;
H = histcounts(image(:),(0:256));
H = H.';
[counts] = H;
x = 0:255;
myCDF = cumsum(counts)/(m*n);
eqI = (L-1)*myCDF(double(image)+1);
eqI = uint8(eqI);
histMyOutput = histcounts(eqI,256);
eq_img = histMyOutput;

这是生成的错误消息

Array indices must be positive integers or logical values.
Error in histeq_contrast (line 22)
eqI = (L-1)*myCDF(double(image)+1);
Error in histeq_test (line 16)
I1eq = histeq_contrast(I1);

供参考,我的测试功能是:

%histeq_test test script%

I1 = imread('pout.tif');
I1=im2double(I1);

% damage contrast
I1=0.8*I1;

I1eq = histeq_contrast(I1);
figure
subplot(1,2,1);
imagesc(I1); caxis([0 1]); title('Test Image 1'); axis equal tight
subplot(1,2,2);
imagesc(I1eq); caxis([0 1]); title('Histeq Result');  axis equal tight
colormap(gray);

fprintf(1,'Min/max of input image  1: %3.3f %3.3f\n', min(I1(:)),max(I1(:)) );
fprintf(1,'Min/max of output image 1: %3.3f %3.3f\n', min(I1eq(:)),max(I1eq(:)) );

% damage contrast
I2 = I1*0.25 + 0.25;

I2eq = histeq_contrast(I2);
figure
subplot(1,2,1);
imagesc(I2); caxis([0 1]); title('Test Image 2');  axis equal tight
subplot(1,2,2);
imagesc(I2eq); caxis([0 1]); title('Histeq Result');  axis equal tight
colormap(gray);

fprintf(1,'Min/max of input image  2: %3.3f %3.3f\n', min(I2(:)),max(I2(:)) );
fprintf(1,'Min/max of output image 2: %3.3f %3.3f\n', min(I2eq(:)),max(I2eq(:)) );

【问题讨论】:

  • 对不起,我的错。现已修复。

标签: matlab image-processing histogram indices


【解决方案1】:

您的输入图像是:

I1 = imread('pout.tif');
I1=im2double(I1);

% damage contrast
I1=0.8*I1;

im2double 之后,I1 包含 0-1 范围内的值。在您的 histeq_contrast 函数中,在您收到错误消息的行中,您使用此图像进行索引:

eqI = (L-1)*myCDF(double(image)+1);

因此,如错误消息所示,您在非整数位置建立索引。 (此外,不需要转换为 double,因为它已经是双精度数了。)正确的是:

eqI = (L-1)*myCDF(round(image*255)+1);

或者,不要使用im2double

【讨论】:

  • 代码输出没有任何错误但直方图现在已损坏,编辑问题以反映更改。
  • @user6820366:请不要对问题进行太多更改,以至于我的回答不再回答它。如果你愿意,你可以编辑它来改进它,但不要让给你答案成为一个移动的目标。我已经回滚了编辑。您在更新中错过了*255,这在这里很重要。此外,您的函数输出的是均衡图像的直方图。改为输出eqI。我猜你没有写代码???
  • 抱歉,我不知道编辑,是的我没有写完整的代码,我希望之前得到直方图,然后根据需要将其更改为输出图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2016-11-27
相关资源
最近更新 更多