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

1 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2019-6-21
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 个评论
Andreea Stancu
Andreea Stancu 2019-6-21
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
Jan
Jan 2019-6-24
If image_etich is a scalar double, you cannot assign imag_etich(i).normal . Defining the field normal works for structs only.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2019-6-21
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

Community Treasure Hunt

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

Start Hunting!

Translated by