MATLAB: Index exceeds matrix dimensions error on reading predicting image files

imreadindex

Hi
I am running a program to test the similarity between predicted images (after training ) and original ground truth images but my program is giving following error:
Index exceeds matrix dimensions.
Error in liang_for_other_3 (line 14)
nam=pics(ii).name;
I am also attaching my program here.
img='/home/SHussain/unet-master-zhixuhao/data/membrane/test/img/';
cls='/home/SHussain/cls/';
tp2=[];
fn2=[];
fp2=[];
jac2=[];
ac2=[];
pics=dir(fullfile(img,'*_predict.bmp'));
clss=dir(fullfile(cls,'*.bmp'));
n1=numel(clss);
ii=0;
for i=1:n1
ii=ii+1;
nam=pics(ii).name;
tr = fullfile(cls, clss(i).name);
%tr=strcat(clss(ii).folder,'\',int2str(i-1),'.bmp');
tr2 = fullfile(img, pics(i).name);
%tr2=strcat(pics(ii).folder,'\',int2str(i-1),'._predict.png');
if exist(tr2)==0||exist(tr)==0
continue;
end
imL=imread(tr);
ims=imread(tr2);
ims=im2uint8(ims);
if(length(size(ims))==3)
ims=rgb2gray(ims);
end
%ims=findtarget2(ims);
ims=imresize(ims,size(imL));
%imwrite(ims,str2);
ims(ims<128)=0;
ims(ims>=128)=255;
imL(imL==1)=255;
tp2(i)=TP(imL,ims);
fn2(i)=FN(imL,ims);
fp2(i)=FP(imL,ims);
jac2(i)=Jaccard(imL,ims);
cha=[];
cha=double(imL)-double(ims);
z1=[];
z1=find(cha==0);
ac2(i)=(size(z1,1)*size(z1,2))/(size(imL,1)*size(imL,2));
end
TPP=mean(tp2);
FPP=mean(fp2);
FNP=mean(fn2);
JACD=mean(jac2);
ACC=mean(ac2);
% subplot(2,1,1);
% imshow(imL);
% subplot(2,1,2);
% imshow(ims);
Please guide me what is the peoblem with my code?

Best Answer

I don't see anything surprising about that error. You have written
pics=dir(fullfile(img,'*_predict.bmp'));
So pics is the list of all bitmap files ending in _predict in the img directory. (Really, the variable img contains a directory name? What a misleading variable name!)
clss=dir(fullfile(cls,'*.bmp'));
So clss is the list of all bitmap files in another directory
n1=numel(clss);
n1 is the number of bitmap files in the clss directory. A better name such as clss_count would make that clearer
for i=1:n1
Ok, we're iterating over the number of bitmap files in clss.
ii = ii + 1; %what a poor variable name!
nam=pics(ii).name;
But we're asking for the iith _predict bitmap file in the img directory. I see absolutely no reason why there would be the same number of files found in each directory.
I also don't see the point of ii which is always going to be equal to i.