You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to use imwrite for an image sequence
1 view (last 30 days)
Show older comments
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
Accepted Answer
Image Analyst
on 21 Aug 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 Comments
bhavya vendra
on 21 Aug 2015
Edited: bhavya vendra
on 21 Aug 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);
Image Analyst
on 21 Aug 2015
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
on 21 Aug 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?
Image Analyst
on 21 Aug 2015
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
on 21 Aug 2015
Edited: Image Analyst
on 21 Aug 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
on 21 Aug 2015
Edited: Image Analyst
on 21 Aug 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
on 21 Aug 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
on 21 Aug 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
on 21 Aug 2015
Edited: bhavya vendra
on 21 Aug 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.
Image Analyst
on 21 Aug 2015
% 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
on 21 Aug 2015
Edited: bhavya vendra
on 22 Aug 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
bhavya vendra
on 22 Aug 2015
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
Image Analyst
on 24 Aug 2015
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
on 25 Aug 2015
Edited: bhavya vendra
on 25 Aug 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
on 25 Aug 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.
bhavya vendra
on 26 Aug 2015
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
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)