現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
how to use imwrite for an image sequence
2 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
Image Analyst
2015 年 8 月 21 日
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 件のコメント
bhavya vendra
2015 年 8 月 21 日
編集済み: bhavya vendra
2015 年 8 月 21 日
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
2015 年 8 月 21 日
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
2015 年 8 月 21 日
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
2015 年 8 月 21 日
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
2015 年 8 月 21 日
編集済み: Image Analyst
2015 年 8 月 21 日
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
2015 年 8 月 21 日
編集済み: Image Analyst
2015 年 8 月 21 日
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
2015 年 8 月 21 日
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
2015 年 8 月 21 日
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
2015 年 8 月 21 日
編集済み: bhavya vendra
2015 年 8 月 21 日
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
2015 年 8 月 21 日
% 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
2015 年 8 月 21 日
編集済み: bhavya vendra
2015 年 8 月 22 日
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
2015 年 8 月 22 日
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
2015 年 8 月 24 日
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
2015 年 8 月 25 日
編集済み: bhavya vendra
2015 年 8 月 25 日

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
2015 年 8 月 25 日
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
2015 年 8 月 26 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
