Index exceeds matrix dimensions error on reading predicting image files

2 ビュー (過去 30 日間)
Sumaira Hussain
Sumaira Hussain 2019 年 5 月 20 日
編集済み: Guillaume 2019 年 5 月 21 日
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?

採用された回答

Guillaume
Guillaume 2019 年 5 月 20 日
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.
  2 件のコメント
Sumaira Hussain
Sumaira Hussain 2019 年 5 月 21 日
img and cls are the address of directories having predicted images and original groundtruth images. I understand that variable names are poor but that is not the reason behind error. Thankyou very much for reply.
Guillaume
Guillaume 2019 年 5 月 21 日
編集済み: Guillaume 2019 年 5 月 21 日
Again, your iteration count is over all the bmp in clss, but you're using that count to open _predict bmp in img.
There is no guarantee that there are as many images in both directories particularly as the search is more restrictive in img. If there are more images in clss, you will inevitably trigger index exceeds matrix dimension.
You can eat check that it is the problem by looking at the values of
numel(clss)
numel(pics)
If the former is greater than the latter, your iteration will fail.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by