이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)
