First, i try to load .mat file into my workspace files = dir('*.mat'); for i = 1:numel(files) load(files(i).name);
Second, i want to read the images in work space and save it as following for k=0:10 output_folder = 'C:\Users\Tossaworn\Desktop'; outputFileName = fullfile(output_folder, ['test' num2str(k) '.tif']);
imwrite(IR0000__,outputFileName); end
IR0000__ is 1 image, but i want IR0001__,IR0002__, ...and output test0,1,2,... . Now output can run, but i have no idea to read the input images. please suggest, Thank you.

 채택된 답변

Guillaume
Guillaume 2018년 5월 25일

1 개 추천

Here is how I would do it:
output_folder = 'C:\Users\Tossaworn\Desktop';
files = dir('*.mat');
for fidx = 1:numel(files)
matcontent = load(files(fidx).name); %load into structure
filenumber = regexp(files(fidx).name, '\d+', 'match', 'once'); %get numeric part of filename
imagename = sprintf('IR%s__', filenumber); %build variable name
assert(isfield(matcontent, imagename), 'mat file %s does not contain image %s', files(fidx).name, imagename);
outputfilename = fullfile(output_folder, sprintf('test%s.tif', filenumber));
imwrite(matcontent.(imagename), outputfilename);
end

댓글 수: 5

Oh, many thanks. Its work perfectly.
thank you so much
I have written this code to create multiple .tif to be avi video, but something wrong with my code. Could you help me to solve this?
Guillaume
Guillaume 2018년 5월 29일
편집: Guillaume 2018년 5월 29일
Please don't post screenshots of code, it's impossible to copy and paste from a screenshot.
If your tiff images are 16-bit then imread should already return a uint16 array, hence your conversion to uint16 is redundant. To convert a uint16 image to double use im2double, which rescale the intensities to the range 0-1. So
I = imread(filename);
writeVideo(outputVideo, im2double(I));
Tossawon Ngamnet
Tossawon Ngamnet 2018년 5월 29일
편집: Guillaume 2018년 5월 29일
Sorry for my careless. I did the code follow your suggestion. Its work, but the avi file cannot open. Code below:
outputVideo = VideoWriter('newvideo.avi');
outputVideo.FrameRate = 30;
outputVideo.Quality = 100;
open(outputVideo);
srcFiles = dir('C:\Users\Tossaworn\Desktop\testTIFfiles\*.tif');
for k = 1 : length(srcFiles)
filename = strcat('C:\Users\Tossaworn\Desktop\testTIFfiles\',srcFiles(k).name);
I = imread(filename);
writeVideo(outputVideo, im2double(I));
end
Is that due to unspecified size?
You're missing a
close(outputVideo);
at the end. If that's not the reason for the problem, then I don't know. Try a different video player which may give more insight.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

제품

릴리스

R2017a

질문:

2018년 5월 25일

댓글:

2018년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by