直方图 - 索引必须是正整数或逻辑值

Histogram - Index must be a positive integer or logical

我做直方图归一化。当我尝试使用另一张图片 lena256.bmp 时,代码是 运行。但是当我尝试使用另一张图片时,它会显示错误:

Attempted to access ; index must be a positive integer or logical.
Error in test2 (line 10)
Histo(a(n,m)+1)=Histo(a(n,m)+1)+1;

代码如下:

a = dicomread('011fp5_256.dcm');
a = double(a);  
a=a/max(a(:)); 
figure; imshow(a);
figure; imhist(a); 
[N, M] = size(a);  
Histo(1:256) = 0;   
for n = 1 : N   
    for m = 1 : M
        Histo(a(n,m)+1) = Histo(a(n,m)+1)+1;  
    end
end
Histo = Histo/(N*M);
figure; plot(Histo);

矩阵索引不能是小数值,因此,您需要将a(n,m)近似为最接近的整数值。

a = dicomread('CT-MONO2-16-ankle.dcm');
a = double(a);  
a=a/max(a(:)); 
figure; imshow(a);
figure; imhist(a); 
[N, M] = size(a);  
Histo(1:256) = 0;   
for n = 1 : N   
    for m = 1 : M
        if a(n,m)+1 ~= floor(a(n,m)+1)%I use this code for find the error
            disp(a(n,m)+1);
        end
        ind = floor(a(n,m)+1);% apprx. to the nearest integer.
        Histo(ind) = Histo(ind)+1;  
    end
end
Histo = Histo/(N*M);
figure; plot(Histo);