Array indices must be positive integers or logical values Error

rgb = inp(:,:,[1 1 1]);
red = rgb(:,:,1);
red(tumorOutline)= 255;
green = rgb(:,:,2);
green(tumorOutline)= 0;
blue = rgb(:,:,3);
blue(tumorOutline)= 0;
tumorOutlineInserted(:,:,1) = red;
tumorOutlineInserted(:,:,2) = green;
tumorOutlineInserted(:,:,3) = blue;
axes(handles.axes6);
imshow(tumorOutlineInserted);
Why this code give error that array in
dices must be positive integers or logical values
Error in line 3

댓글 수: 3

... because ‘TumorOutline’ is not a logical index or a positive integer.
It is not obvious what it is.
Jan
Jan 2021년 6월 29일
편집: Jan 2021년 6월 29일
Please post the complete error message. I would reveal, which line is falling. Either Star Strider's idea is the problem, or this line, if you have redefined "axes" as a local variable:
axes(handles.axes6);
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global brainImg
[filename, pathname] = uigetfile({'*.jpg'; '*.bmp'; '*.tif'; '*.gif'; '*.png'; '*.jpeg'}, 'Load Image File');
if isequal(filename,0)||isequal(pathname,0)
warndlg('Press OK to continue', 'Warning');
else
brainImg = imread([pathname filename]);
axes(handles.axes1);
imshow(brainImg);
axis off
helpdlg(' Image loaded successfully ', 'Alert');
end
[m n c] = size(brainImg);
if c == 3
brainImg = rgb2gray(brainImg);
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global brainImg inp
num_iter = 10;
delta_t = 1/7;
kappa = 15;
option = 2;
inp = anisodiff(brainImg,num_iter,delta_t,kappa,option);
inp = uint8(inp);
inp=imresize(inp,[256,256]);
if size(inp,3)>1
inp=rgb2gray(inp);
end
axes(handles.axes2);
imshow(inp);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global inp h tumor_label tumor
%% thresholding
sout=imresize(inp,[256,256]);
t0=60;
th=t0+((max(inp(:))+min(inp(:)))./2);
for i=1:1:size(inp,1)
for j=1:1:size(inp,2)
if inp(i,j)>th
sout(i,j)=1;
else
sout(i,j)=0;
end
end
end
%% Morphological Operation
label=bwlabel(sout);
stats=regionprops(logical(sout),'Solidity','Area','BoundingBox');
density=[stats.Solidity];
area=[stats.Area];
high_dense_area=density>0.6;
max_area=max(area(high_dense_area));
tumor_label=find(area==max_area);
tumor=ismember(label,tumor_label);
if max_area>100
axes(handles.axes3);
imshow(tumor);
else
h = msgbox('No Tumor!!','status');
%disp('No tumor');
return;
end
%% Bounding box
box = stats(tumor_label);
wantedBox = box.BoundingBox;
axes(handles.axes4);
imshow(inp);
hold on;
rectangle('Position',wantedBox,'EdgeColor','y');
hold off;
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global erodedImage tumor inp
dilationAmount = 5;
rad = floor(dilationAmount);
[r,c] = size(tumor);
filledImage = imfill(tumor, 'holes');
for i=1:r
for j=1:c
x1=i-rad;
x2=i+rad;
y1=j-rad;
y2=j+rad;
if x1<1
x1=1;
end
if x2>r
x2=r;
end
if y1<1
y1=1;
end
if y2>c
y2=c;
end
erodedImage(i,j) = min(min(filledImage(x1:x2,y1:y2)));
end
end
%figure
%imshow(erodedImage);
%title('eroded image','FontSize',20);
tumorOutline (erodedImage)=0; // error line
axes(handles.axes5);
imshow(tumorOutline);
rgb = inp(:,:,[1 1 1]);
red = rgb(:,:,1);
red(tumorOutline)= 255; //error line
green = rgb(:,:,2);
green(tumorOutline)= 0;
blue = rgb(:,:,3);
blue(tumorOutline)= 0;
tumorOutlineInserted(:,:,1) = red;
tumorOutlineInserted(:,:,2) = green;
tumorOutlineInserted(:,:,3) = blue;
axes(handles.axes6);
imshow(tumorOutlineInserted);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 6월 30일
You can't assign certain locations of tumorOutline to be zero when you haven't defined the array.
tumorOutline (erodedImage)=0; // error line
Assuming erodedImage is a logical image of true and false, you're saying that you want the true parts of erodedImage to be black in tumorOutline. Essentially you want tumorOutline to be black where erodedImage is not black. However is the first element is at, say (30, 50) what is it supposed to do with the prios elements, and where is (30,50) in the image? It has not seen tumorOutline before so it has no idea how many rows or columns it has. To fix that particular error, you can declare it somehow, like
tumorOutline = true(size(erodedImage)); % Declare in advance so it knows the size of tumorOutline.
tumorOutline (erodedImage)=0; % No error now;
However there may be other errors and it still may not do what you want due to errors in your algorithm.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by