Why do I get "Array indices must be positive integers or logical values." Image processing.

Please help me. I dont know what I doing wrong...
clear all, clc
[Iwe, map] = imread('chest-xray_2','bmp');
figure(1)
image([Iwe,map]);
title("oryginal image")
colormap(map);
Iwe=double(Iwe);
[row col deep]=size(Iwe);
IweR=Iwe(:,:,1);
IweG=Iwe(:,:,2);
IweB=Iwe(:,:,3);
Iwe_grey(:,:,1)=(IweR+IweG+IweB)/3;
Iwe_grey(:,:,2)=Iwe_grey(:,:,1);
Iwe_grey(:,:,3)=Iwe_grey(:,:,1);
Iwe_grey=Iwe_grey(:,:,1);
figure(2)
imshow(Iwe_grey);
title("monochrome image");
Iwe_grey=uint8(Iwe_grey);
%%started histogram
histogram = zeros(256,1);
for y=1:col
for x=1:row
b=Iwe_grey(x,y)+1;
histogram(b+1)=histogram(b+1)+1;
end
end
figure(3)
bar(histogram);
%%stacked histogram
cumulative(1)=histogram(1)
for z=2:256
cumulative(z)=cumulative(z-1)+histogram(z)
end
cumulative = cumulative'
%%aligned histogram
for z=1:256
hist_norm(z) = (256*cumulative(z)/cumulative(256));
end
figure(4)
plot(hist_norm);
title('aligned histogram')
for x=1:row
for y= 1:col
if(Iwe_grey(x,y)<0)
Image_out_grey(x,y)=0;
else if (Iwe_grey(y,x) > 255)
else Image_out_grey(y,x)=255*cumulative(Iwe_grey(y,x))/cumulative(255);
end;
end;
end;
end;
Image_out_grey = uint8(Image_out_grey);
figure(5)
imshow(Image_out_grey);
title("the resulting image after aligning the histogram");

 Akzeptierte Antwort

You get that error because any time you use indexing to extract values from a variable, the index number must be integers >= 1. Somewhere in your code (you did not indicate where) you are using an invalid index.
% Good
A=[1:10];
A(3)
ans = 3
% Bad
A(4.3)
Array indices must be positive integers or logical values.

3 Kommentare

hello thanks a lot for the quick response.
Here I am using the wrong index, at the same time I am not able to understand how to improve it.
Error in untitled (line 74)
else Image_out_grey (y_col, x_row) = 255 * cumulative (Iwe_grey (y_col, x_row)) / cumulative (255);
>>
First, identify where you are indexing, and then see if the values being used are integers or not.
Here, I suspect the values in Iwe_grey are not.
Thank U very much! Luckily I won with the problem. Correct code:
clear all, clc
[Iwe, map] = imread('chest-xray_2','bmp');
figure(1)
image([Iwe,map]);
title("Obraz oryginalny")
colormap(map);
Iwe=double(Iwe);
[row col deep]=size(Iwe);
IweR=Iwe(:,:,1);
IweG=Iwe(:,:,2);
IweB=Iwe(:,:,3);
Iwe_grey(:,:,1)=(IweR+IweG+IweB)/3;
Iwe_grey(:,:,2)=Iwe_grey(:,:,1);
Iwe_grey(:,:,3)=Iwe_grey(:,:,1);
Iwe_grey=Iwe_grey(:,:,1);
figure(2)
Iwe_grey=uint8(Iwe_grey);
imshow(Iwe_grey);
title("Obraz monochromatyczny");
%%histogram
histogram = zeros(256,1);
for y_col=1:col
for x_row=1:row
if Iwe_grey(x_row,y_col)>0 && Iwe_grey(x_row,y_col)<=255
b=Iwe_grey(x_row,y_col);
histogram(b)=histogram(b)+1;
end
end
end
figure(3)
bar(histogram);
%obliczanie histogramu skumulowanego
cumulative=histogram;
for z_hist=2:256
cumulative(z_hist)=cumulative(z_hist-1)+histogram(z_hist);
end
figure(4)
plot(cumulative);
title('histogram skumulowany')
Iwe_grey=uint8(Iwe_grey);
for x_row=1:row
for y_col= 1:col
if(Iwe_grey(x_row,y_col)<=0)
Image_out_grey(x_row,y_col)=0;
else if (Iwe_grey(x_row,y_col) >= 255)
Image_out_grey(x_row,y_col)=255;
else Image_out_grey(x_row,y_col)=(255*cumulative(Iwe_grey(x_row,y_col)))/cumulative(255);
end;
end;
end;
end;
Image_out_grey = uint8(Image_out_grey);
figure(5)
imshow(Image_out_grey);
title("Obraz wynikowy po wyrównaniu histogramu ");

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by