how to use imwrite for an image sequence

Hello, I have been using a special file format that processes image sequence and I have a stack of images in a 3D file format. I have been using a custom matlab code that reads the 3d file and I can open all the images in the sequence using 'imshow' command but I am not sure how to save all the images I have opened sequentially without having to do it manually. How can I call these files I am opening using 'imshow' as a variable? This is my code: A= WURead3D('trialresults_000900.wu'); i=68; for k=1:i figure,imshow(A(:,:,k),[]); title(sprintf('900 # %d',k)); end

 Respuesta aceptada

Image Analyst
Image Analyst el 21 de Ag. de 2015
Try this:
A= WURead3D('trialresults_000900.wu');
folder = pwd; % or whatever.
numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
imshow(A(:,:,k),[]);
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.png', k);
fullFileName = fullfile(folder, baseFileName);
imwrite(A(:,:,k), fullFileName);
end

16 comentarios

bhavya vendra
bhavya vendra el 21 de Ag. de 2015
Editada: bhavya vendra el 21 de Ag. de 2015
Thank you, This works to save all images at once but all my images are black. I have tried to add this to my code and I can see that the image is still there but not bright enough. I don't know what would be the number I need to choose for map? Is there any other way I could make my images show up and not stay black? Also, It is not saving my slices in a separate folder. How can I fix that? Also how can I raise my bitDepth back upto 16 for a tiff image?
map=gray(80); a=A(:,:,k); a=a.*1000; imwrite(a,map, fullFileName);
Set folder equal to where you want the images to live, like
folder = 'c:\users\bhavya\documents\my pictures\900';
or wherever you want them.
As far as the images being black or very dark, check the values as you read them back in and see if they're the same values as what you wrote out. For example, pick pixel (10,10) and check it before saving, and after recalling with imread(). They should be the same value. Neither imwrite() nor imread() will change the pixel values. Perhaps they really are dark, or you're not using the [] option in imshow().
bhavya vendra
bhavya vendra el 21 de Ag. de 2015
I looked into the two files with imshow and imread of the same image and the pixel values are different. What can I do? They show up on imshow but they don't on imwrite. Also, is there a way to save tiff files as 16 bit depth?
So you did
a1 = A(10,10,k)
imwrite(A(:,:,k), fullFileName);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(10,10)
and you find that a1 and a2 are completely different numbers???
bhavya vendra
bhavya vendra el 21 de Ag. de 2015
Editada: Image Analyst el 21 de Ag. de 2015
yeah I got a1=0.0055 and a2=1 using the same code. Why do you think it might be? I am copy pasting the code again so you can see if I have any mistakes.
A= WURead3D('trialresults_000900.wu');
folder = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers'; numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
imshow(A(:,:,k),[])
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.tif',k);
fullFileName = fullfile(folder, baseFileName);
imwrite(A(:,:,k), fullFileName,'Compression','none','Resolution',[96,96]);
end
a1 = A(10,10,k)
imwrite(A(:,:,k), fullFileName);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(10,10)
Image Analyst
Image Analyst el 21 de Ag. de 2015
Editada: Image Analyst el 21 de Ag. de 2015
What is the class of A? Is it uint8, uint16, or double? How about A2drecalled, a1, and a2? Can you post the .wu file and your WURead3D function? Try this code:
A= WURead3D('trialresults_000900.wu');
folder = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers'; numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
imshow(A(:,:,k),[])
a1 = A(end,end,k);
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.tif',k);
fullFileName = fullfile(folder, baseFileName);
imwrite(A(:,:,k), fullFileName,'Compression','none','Resolution',[96,96]);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(end,end);
fprintf('For plane %d, a1=%f, a2=%f\n', k, a1, a2);
end
Run this and paste back here what it spews out to the command window.
bhavya vendra
bhavya vendra el 21 de Ag. de 2015
A is a double, A2drecalled is a uint8,a1 is a double and a2 is uint8. I wasn't able to send the link as the file is too big.
Image Analyst
Image Analyst el 21 de Ag. de 2015
If A is a double, save it in a .mat file, not a .tif file. I'm not sure TIFF can save doubles. It probably converted it to uint8 - that's why is was uint8 when you recalled it from disk.
bhavya vendra
bhavya vendra el 21 de Ag. de 2015
Editada: bhavya vendra el 21 de Ag. de 2015
If I save it as .mat file, how can I get the images out of matlab for post processing? and is there a way to keep my images with the same resolution as before? Also, how can I save it in a matlab file as imwrite doesn't write.m files.
% To store:
save(fullMatFileName, 'A');
% To get back
storedStructure = load(fullMatFileName);
A = storedStructure.A;
Does A have fractional values such that it needs to be saved in double format, or is it integers in which case you can just cast to uint8 or uint16 and save it with imwrite()?
bhavya vendra
bhavya vendra el 21 de Ag. de 2015
Editada: bhavya vendra el 22 de Ag. de 2015
I am not sure what I am doing wrong, it is still giving me a1 and a2 as the numbers I mentioned earlier. Please look at my code below. And yeah, the A values are fractions so I cant convert to uint8 or uint16.
clear all
clc
A= WURead3D('trialresults_000900.wu');
folder = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers';
fullmatfilename = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers\A.mat';
save(fullmatfilename,'A')
storedstructure=load(fullmatfilename);
A=storedstructure.A;
numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
imshow(A(:,:,k),[]);
a1 = A(end,end,k);
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.tif',k);
fullFileName = fullfile(folder, baseFileName);
imwrite(A(:,:,k), fullFileName,'Compression','none','Resolution',[96,96]);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(end,end);
fprintf('For plane %d, a1=%f, a2=%f\n', k, a1, a2);
end
Hey, Thank you so much for all the help.I looked at the matrix values of A and the values were few decimals off from the original image. So I multiplied all matrix values by 10000 which made my images showup with the same contrast with which I started with. My issue now however is if I could find a way to convert them to a 16 bit depth tiff image. Is there anyway to do that? I can work with 8 bit depth but my original images were of 16 bit depth and I would like to keep the quality if there is anyway. Here's the final code right now.
clear all
clc
A= WURead3D('trialresults_000900.wu');
folder = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers';
%basematFileName = sprintf('A %d.mat',A);
% fullmatfilename = 'G:\COSMOS9.15\results\bhavya_growth plate\1microngp_trial1\answers\A.mat';
% save(fullmatfilename,'A')
% storedstructure=load(fullmatfilename);
% A=storedstructure.A;
numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
imshow(A(:,:,k),[]);
a1 = A(end,end,k);
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.tif',k);
fullFileName = fullfile(folder, baseFileName);
colormap default
a=(A(:,:,k));
map=gray(256);
a=a.*10000;
imwrite(a,map,fullFileName,'Compression','none','Resolution',[96,96]);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(end,end);
fprintf('For plane %d, a1=%f, a2=%f\n', k, a1, a2);
end
Instead of arbitrarily scaling them by multiplying by 1000, scale them by multiplying by the max for that class:
a = intmax(class(a)) * a / max(a(:))
Afterwards, cast to uint16 if you want:
a = uint16(a);
bhavya vendra
bhavya vendra el 25 de Ag. de 2015
Editada: bhavya vendra el 25 de Ag. de 2015
I tried it but I keep getting this error
Error using intmax (line 40) Invalid class name.
Error in Finalcopy_savewuimagestotiff (line 24) a = intmax(class(a))*a/ max(a(:));
Also, I just realized all my images that I have written has a black shadow at the top and bottom. Please look at the attached image. When I view the image in the custom viewer for .wu files, I don't see that there. Do you know what I can do to fix that?
Image Analyst
Image Analyst el 25 de Ag. de 2015
What is class(a)? I'm assuming it will be either 'uint8' or 'uint16' because you're using imwrite() but it appears that it might be 'double', in which case you can't use imwrite on "a" - you'd have to convert it first.
Is there another function I could use instead of imwrite that would perform the same task on 'double' images? I converted the .wu files to .mat first and performed the task and it still shows that my matrix A is a double and my a is also a double. Please look at my code and see if I am not doing something right.
clear all
clc
A= WURead3D('resultstrial2sample1.wu');
folder = 'C:\Users\bvendra\Desktop\research\results\bhavya_growth plate\1microngp_trial2sample1\tiffstackafterCOSMOS';
basematFileName = sprintf('A %d.mat',A);
fullmatfilename = 'C:\Users\bvendra\Desktop\research\results\bhavya_growth plate\1microngp_trial2sample1\tiffstackafterCOSMOS\A.mat';
save(fullmatfilename,'A')
storedstructure=load(fullmatfilename);
A=storedstructure.A;
numSlices = size(A, 3);
numRows = ceil(sqrt(numSlices));
for k = 1 : size(A, 3)
subplot(numRows, numRows, k);
a=(A(:,:,k));
imshow(a,[]);
a1 = A(end,end,k);
drawnow;
caption = sprintf('900 # %d', k);
title(caption, 'FontSize', 30);
baseFileName = sprintf('Slice %d.tif',k);
fullFileName = fullfile(folder, baseFileName);
%colormap default
map=gray(256);
%a = intmax('double')*a/ max(a(:));
%a = uint16(a);
imwrite(a,map,fullFileName,'Compression','none','Resolution',[96,96]);
A2Drecalled = imread(fullFileName);
a2 = A2Drecalled(end,end);
fprintf('For plane %d, a1=%f, a2=%f\n', k, a1, a2);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Ag. de 2015

Comentada:

el 26 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by