Saving Cell Contents to Images in Loop for ImageJ

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!

Answers (1)

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 Comments

Thanks! This looks a lot like the code I've assembled now to get to the point where I've divided the 4D matrix A(m,n,j,k) I started from into the cell array consisting k 3D matrices A_cell{k}=A(m,n,j). Now, I suppose saving a .mat files or images is similar in the loop, but how can I save images from each 3D cell as j indexed images A(m,n)? I'm a little stuck at the loop structure. Also, using imwrite, how does this function know which images to export?
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.
Thanks again for the tips. It's not quite what I am trying to do. I am having trouble reducing the dimensionality of the matrices, and incorporating this in the output file loop. Ideally the images will be saved with index, Image_k_i. In this example there are k=11 images, each with i=16 image sequences, 1_2,1_3...11_1,11_...11_16. Total k*i 256x256 images. Here's an example of what I have:
% 4D mat file data4D=A(256,256,16,11);
for k = 1:size(data,4)
for i=1:size(data,3)
% reduce from 4D to 3D
data3D=squeeze(abs(data4D(:,:,1:1:end,k)));
$reduce from 3D to 2D
data2D=squeeze(data3D(:,:,i));
% save k*i 2D mat files to tiff baseFileName = sprintf('Frame %d.tif',i); fullFileName = fullfile(dir,baseFileName); imwrite(mat2gray(data2D), fullFileName); end end
Got it. Needed a second index in the baseFileName. Did not realize you could do that.
baseFileName = sprintf('Frame %d.tif',k,i);
Thanks!
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.
Hi. This saves the images well, but the name is messy I agree. I'm looking for a filename like Frame 1_Step_1.tif to Frame_k_Step_i. For each frame k there are i steps. Also, do you have experience with the Tiff function? I'm trying to save the tiffs as 32-bit as the ImageJ function is having problems with the 16-bit output. Thanks for the follow-ups!
For the naming I've tried this and it's better:
file_name = strcat('Frame_', num2str(k),'Step_', num2str(i),'.tif');
How about the 32-bit image saving?
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.
Thanks! I posted a question about saving 32-bit tiff images in this context. Please check it out if you have some insight.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 20 Nov 2014

Commented:

on 20 Nov 2014

Community Treasure Hunt

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

Start Hunting!