Conversion to double from struct is not possible with bwconncomp

So have this code but it keeps giving me an error at the bwconncomp line that says, Conversion to double from struct is not possible
for l=1:256 for m=1:256 for n=1:50
if (IMG(l,m,n) > 0.001)
if (( IMG(l,m,n) > 0.001)&&(IMG(l,m,n) < 0.19))
IMG(l,m,n)=1;
else
IMG(l,m,n)=0;
end
end
end
end
end
cc = zeros(256,256,50);
IMGT = zeros(256,256);
for ii = 1:50
IMGT=IMG(:,:,ii);
cc(:,:,ii) = bwconncomp(IMGT);
end
Any idea where the problem can be?

Answers (1)

That's not a good way to binarize if you're simply comparing to a threshold.
Explain exactly what "didn't work for me" means. Do you get an error message?

4 Comments

I got an error message that says,
"Conversion to double from struct is not possible.
Error in ==> Extract_CSF_MD at 35 cc(:,:,ii) = bwconncomp(IMG(:,:,ii));",
knowing that IMG is of type float not double, so I tried to reshape IMG after binarizing, but it still gave same error.
The binarization worked before without the bwconcomp perfectly, so it's not the nested for loops
What did you do in your "binarizing process" such that it turned the image into a structure?
for l=1:256 for m=1:256 for n=1:50
if (IMG(l,m,n) > 0.001)
if (( IMG(l,m,n) > 0.001)&&(IMG(l,m,n) < 0.19))
IMG(l,m,n)=1;
else
IMG(l,m,n)=0;
end
end
end
end
end
So its like a band pass filter to pass some frequencies and turn them into white, and set the rest to black.
That's not going to turn it into a structure. Anyway the whole thing (triple nested for loop) can be done a lot faster simply by doing this single line
IMG = IMG > 0.001 & IMG < 0.19;
No for loops needed at all. You should learn how to write vectorized code.

Sign in to comment.

Asked:

M
M
on 13 Feb 2014

Edited:

M
M
on 17 Feb 2014

Community Treasure Hunt

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

Start Hunting!