MATLAB Answers

0

How can a code run on a single image perfectly, but the same code on an entire database crashes? Where am I going wrong?

Asked by Andreea Stancu on 21 Jun 2019
Latest activity Commented on by Jan
on 24 Jun 2019
Accepted Answer by Jan
When I run the code on an image database it gives this error and I don't know why, because when I run the test on a single image it works perfectly.
The code looks like this:
for i=1:80
imag_open(i).normal=imopen(M_filt1(i).normal, ones(20));
imag_close(i).normal=imclose(imag_open(i).normal, ones(20));
[imag_etich(i).normal, maxL(i).normal]=bwlabel(imag_close(i).normal); %line 67
minim(i).normal=min(imag_etich(i).normal(:))
hist_etich(i).normal=hist(imag_etich(i).normal(:),0:maxL(i).normal)
[valmax(i).normal,posmax(i).normal]=max(hist_etich(i).normal(2:end))
etich(i).normal=posmax(i).normal
M_filt1(i).normal=255*double(imag_etich(i).normal==etich(i).normal);
end
%% Displayed error
Field assignment to a non-structure array object.
Error in script_sat (line 67) [imag_etich(i).normal, maxL(i).normal]=bwlabel(imag_close(i).normal);
Before this I did a saturation segmentation to obtain the masks, here the mask is M_filt1(i).normal.
I am trying to put some labels on every mask to find every object from the image, so I can scrap the unnecessary pixels outside my lesion. In imag_etich(i).normal I want to save the labeled image, and in maxL(i).normal I wish to save the number of labels from the image.
Help a girl out if you can, pleaase.
Best regards

  0 Comments

Sign in to comment.

2 Answers

Answer by Jan
on 21 Jun 2019
 Accepted Answer

Use the debugger to find the reason of the problem. Let Matlab stop, when the error occurs:
dbstop if error
Then run the code again until it stops at the error. Then check the used variables:
whos image_etich
whos maxL
One of them is not the assumed struct array and assigning a field must fail.
You do not show how these variables are initialized, so I cannot guess, what exactly the resaon of this behaviour is.

  3 Comments

I will take it from the begining. My database has 200 images which can be split into three sets, first two sets have 80 images and the third set has 40 images. This is how I loaded the database in the program and how I read the first set (I did the same for the others too)
tabel=readtable('C:\Users\Andreea\Desktop\licenta\PH2Dataset\PH2Dataset\PH2_dataset.xlsx');
tabel(1:11, :) = []; %eliminarea capetelor de tabel
for i=2:size(tabel, 1);
imag_etich=i;
poza.nume=tabel(:,1);
end
db=cell2struct(table2array(poza.nume),'nume',200);%obtinerea structurii
for i = 1:size(tabel,1)
filenames(i).path = sprintf('C:\\Users\\Andreea\\Desktop\\licenta\\PH2Dataset\\PH2Dataset\\PH2_Dataset_images\\%s\\%s_Dermoscopic_Image\\%s.bmp',db(i).nume,db(i).nume,db(i).nume);
end
for i=1:size(filenames,2) %s-a incarcat tabelul excel care continea toate informatiile referitoare la imagini
imag(i).data=imread(filenames(i).path);
end
for i = 1:80
imdb(i).normal = imag(i).data;
end
Afterwards, I did a segmentation on a saturation threshold like this
for i = 1:80
procim(i).normal_hsv= rgb2hsv(imdb(i).normal);
M1(i).normal=double(255*(procim(i).normal_hsv(:,:,2)>0.5));
M_filt(i).normal=imclose(M1(i).normal, ones(10));
M_filt1(i).normal=imopen(M_filt(i).normal, ones(10));i,
end
And now, I got to the labeling part where I get the error from before:
for i=1:80
imag_open(i).normal=imopen(M_filt1(i).normal, ones(20));
imag_close(i).normal=imclose(imag_open(i).normal, ones(20));
[imag_etich(i).normal, maxL(i).normal]=bwlabel(imag_close(i).normal);
minim(i).normal=min(imag_etich(i).normal(:))
hist_etich(i).normal=hist(imag_etich(i).normal(:),0:maxL(i).normal)
[valmax(i).normal,posmax(i).normal]=maxL(hist_etich(i).normal(2:end))
etich(i).normal=posmax(i).normal
M_filt1(i).normal=255*double(imag_etich(i).normal==etich(i).normal);
end
In imag_etich(i).normal there should be the labelled image and in maxL(i).normal, the number of labells found in the picture.
I did what you suggested and this was the result
whos imag_etich
Name Size Bytes Class Attributes
imag_etich 1x1 8 double
K>> whos maxL
For maxL I don't get anything, it doesn't even appear in the workspace
If image_etich is a scalar double, you cannot assign imag_etich(i).normal . Defining the field normal works for structs only.

Sign in to comment.


Answer by Guillaume
on 21 Jun 2019

As Jan said you haven't preassigned any of the structures you're using (plus you're also growing your structure arrays in loops, which is not efficient but not relevant to the error). So if any of your structure has been used to store anything else, you'll see the error you get, e.g if at any point you've done:
imag_open = 5;
then
imag_open(i).nomal = ...
will result in the error.
You could solve the problem by creating all your structures at the beginning of your program, e.g.:
imag_open = struct('normal', cell(1, 80));
%etc, for each structure
which would also create them with the correct size (as long as 80 is the correct size, you're not consistent on that!) and hence avoid the array growing problem as well.
Personally, I'd ditch the structures and simply add variables to your original tables. You also need to learn to manipulate tables. There's rarely any need for table2array. In your case, you need to use {} or . indexing to access the content of the table rather than portions of the table.
Finally, there's a serious problem with your because maxL seems to be use both as a function and variable:
[imag_etich(i).normal, maxL(i).normal] = ... %maxL is a structure array with one field
%...
[valmax(i).normal,posmax(i).normal]=maxL(hist_etich(i).normal(2:end)) %maxL used as a function here

  0 Comments

Sign in to comment.