creating 3d array from images
10 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to read and copy a series of 5 images into a 3d array, where each image should have a 'thickness' of 13 pixels. When run I get this error:
Undefined function or variable 'B2'.
Error in U (line 20)
I=cat(3,B1,B2,B3,B4,B5);
Why B1 image is copied, but not B2 (and I suppose the rest)? Here is the code. Thanks!
I=zeros(256,256,100);
n=13;
for i=1:5
x=[int2str(i) '.png'];
if exist(x, 'file')
A{i}=imread(x);
B{i}=A{i}(:,:,1);
B{i}=repmat(B{i},[1 1 n]);
else
fprintf('File %s does not exist.\n', x);
end
end
I=cat(3,B1,B2,C3,B4,B5);
0 个评论
采纳的回答
Geoff Hayes
2014-9-9
Xenios - I think that you mean to use B{2} in place of B2 (though I'm not sure how B1 passed - could you have defined it elsewhere?). Try replacing your line
I=cat(3,B1,B2,C3,B4,B5);
with
I=cat(3,B{1},B{2},B{3},B{4},B{5});
Note how the above line also uses B{3} rather than C3.
0 个评论
更多回答(3 个)
David Young
2014-9-9
B2 is the name of a variable, which has not been given a value. It appears that B1 was given a value in some code that isn't shown in your question. Anyway, these variables aren't used - instead you need to use the elements of the cell array B.
Assuming that C3 is just a typo, it looks like you mean, for the final line
I = cat(3, B{1}, B{2}, B{3}, B{4}, B{5});
1 个评论
David Young
2014-9-9
You may also want to consider putting something in the else part of the condition, for when a file is missing - e.g. B{i} = []. For generality and conciseness, you could also replace the final line with
I = cat(3, B{:});
Xen
2014-9-9
2 个评论
Image Analyst
2014-9-9
Xenios, this is not an "Answer" so please respond to whomever you're responding to. I thought the whole reason you were dealing with the complications of cell arrays was that the images aren't the same size. Otherwise there is no reason to mess around with cell arrays. If that's not right, and they are all the same size, don't use cells to store your images and just tack them onto a 3D array immediately after reading in.
I=zeros(256,256,100);
n=13;
for i=1:5
baseFileName = [int2str(i) '.png'];
if exist(baseFileName, 'file')
rgbImage = imread(baseFileName);
redChannel = rgbImage(:,:,1); % Take red channel
volumetricImage = repmat(redChannel,[1 1 n]); % Replicate 13 times.
if i == 1
I = volumetricImage;
else
I = cat(3, I, volumetricImage);
end
else
fprintf('File %s does not exist.\n', baseFileName);
end
end
There's more wrong with the code - I didn't fix everything for you. For example you should not cat() when you've preallocated - what's the point? And you should use dir() to get the filenames known to exist in advance. If you don't do that, and use exist() to check, then at least have a separate counter for inserting into the preallocated array, which would be better than using cat if you preallocate knowing how many files there are (because you used dir()). The separate counter will make sure you don't have gaps in (the very badly-named) I if a file is missing. And so on. Please read the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!