Saving Cell Contents to Images in Loop for ImageJ
Afficher commentaires plus anciens
Hi,
I have a 4D data set A(m,n,j,k). In a for loop the data is "squeezed" into a cell A_cell to make k 3D cell entries each A_cell{k} = A(m,n,j). Each cell entry is saved as a .mat in the home directory within the loop.
Now in the loop(or not), I need to open either each cell or each saved .mat file, generate j tif images for each of the cell/.mat entries/files, and then save these images as tiff with numbered names back into the home directory.
I am using this function to generate ImageJ-readable images from the 4D data sets.
Can somebody please help with this?
Thanks!
Réponses (1)
Image Analyst
le 20 Nov 2014
Should be very easy with this http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F and this http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F. Inside the loop, create an outputfilename with sprintf() and fullfile (like the FAQ shows) and then get the image from the cell array with braces
thisImage = myCellArray{index};
Then call imwrite
baseFileName = sprintf('..........
fullFileName = fullfile(folder, baseFileName);
imwrite(thisImage, fullFileName);
9 commentaires
Avigdor
le 20 Nov 2014
Image Analyst
le 20 Nov 2014
Something like
folder = pwd; % Or whatever you want.
for k = 1 : size(A, 4)
% Extract kth frame (a color image) from the 4D image.
thisImage = A(:,:,:,k);
% Create filename.
baseFileName = sprintf('Frame %d', k);
fullFileName = fullfile(folder, baseFileName);
% Write to disk.
imwrite(thisImage, fullFileName);
end
I don't see why it's necessary to mess around with cells if you already have a 4D matrix.
Avigdor
le 20 Nov 2014
Avigdor
le 20 Nov 2014
Image Analyst
le 20 Nov 2014
But that's not right , as I'm sure you figured out. You should have (in most cases) the same number of %d as you do variables. For example, this:
k=10;
i = 2;
baseFileName = sprintf('Frame %d.tif',k,i)
gives:
baseFileName =
Frame 10.tifFrame 2.tif
which is NOT a good filename. If you have a number k, and a number i, what do you want the filename to look like.
Avigdor
le 20 Nov 2014
Avigdor
le 20 Nov 2014
Image Analyst
le 20 Nov 2014
I would use sprintf but include the Step part:
file_name = sprintf('Frame_%d, Step_%d.tif', k, i);
It looks simpler to me and more like other languages you're familiar with like C and Java.
Avigdor
le 20 Nov 2014
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!