根据数字顺序从文件夹中的所有png图像制作视频?

时间:2019-03-12 20:52:35

标签: matlab sorting image-processing

我很想从png文件制作视频,并且当制作视频时,应该按顺序排列,将图像test_1放在下一张test_10上,但是我需要 test_1,test_2,test_3 .... 如果我按%sort_nat({images.name})对其进行排序;稍后我会收到错误消息,此类型的变量不支持点索引。 任何评论将不胜感激。 这是脚本:

clear all; clc;close all;
path = 'D:/Neda/Pytorch/U-net/plots_U_Net/CineLoop';
filePattern = fullfile(path, '*.png');
imgfileattrib = dir(filePattern); %attributes
images = {imgfileattrib.name};    %list of images
[~, ind] = sort(str2double(regexprep(images,'[^0-99]',''))); %sorted indices
images = images(ind);             

writerObj = VideoWriter('YourAVI.avi');
writerObj.FrameRate=1;
open(writerObj);

for frameNumber = 1 : length(images)

     baseFileName = images(frameNumber);
     fullFileName = fullfile(path, baseFileName);

     fprintf(1, 'Now reading %s\n', fullFileName);
     thisimage = imread(fullFileName);
     imshow(thisimage);  
     drawnow; 
     writeVideo(writerObj, thisimage);
end
close(writerObj);

1 个答案:

答案 0 :(得分:3)

Remove个非数字字符convert to double,然后应用sort获得正确排序的索引。使用这些索引对images进行排序。

imgfileattrib = dir(filePattern); %attributes
images = {imgfileattrib.name};    %list of images
[~, ind] = sort(str2double(regexprep(images,'[^0-9]',''))); %sorted indices
images = images(ind);             %sorted list