AI_NA
AI_NA

Reputation: 346

make a video from all png images from a folder based on numerical order?

I am tryign to make a video from png files, and they should be in the order while when it make the video, it put image test_1, next test_10, but I need to be test_1, test_2, test_3.... if I sort them by %sort_nat({images.name}); I will get an error later Dot indexing is not supported for variables of this type. any comment would apprecited. here is script:

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);

Upvotes: 0

Views: 68

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Remove non-numeric characters, convert to double and then apply sort to get the correctly sorted indices. Use these indices to 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

Upvotes: 3

Related Questions