Search code examples
matlabimage-processingvideo-streamingvideo-processingmatlab-cvst

frame to video conversion matlab


So I just started with image processing/computer vision in MATLAB.

So my first task is to convert a series of images(frames) into a video. So I went through online sources (MATLAB website more specifically) to get a way to do it.

So the one that I implemented is http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html which solved the problem for me.

However, when I play it, the video seems jumpy in some places. Like it would bring a different frame in the middle and make the whole video jumpy for that split second. It happens a couple of places in the video.

Any anyone knows why this happens?

Thanks

PS below is the code I use:

myFolder = 'C:\Users\owner\Desktop\MATLAB GUI\Color\Color'; %Specify Directory
filePattern = fullfile(myFolder, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
size = length(jpegFiles); % length of the size of the file

outputVideo = VideoWriter(fullfile(myFolder,'video1.avi'));
outputVideo.FrameRate = 30;
open(outputVideo);


for i = 1:length(jpegFiles) %load all the files in the directory
  j = i; %%accumulating the number of jpegfiles into handles.j
  baseFileName = jpegFiles(i).name;
  fullFileName = fullfile(myFolder, baseFileName);
  %fprintf(1, 'Now reading %s\n', fullFileName); %filename of image
  imageArray = imread(fullFileName); %image being read
  %imageArray = rgb2gray(imageArray);
  imagecell{i} = imageArray; %storing the images in imagecells
  writeVideo(outputVideo,imagecell{i});
end

close(outputVideo);
video1 = VideoReader(fullfile(myFolder,'video1.avi'));

mov(video1.NumberOfFrames) = struct('cdata',[],'colormap',[]);

for ii = 1:video1.NumberOfFrames
    mov(ii) = im2frame(read(video1,ii));
end

set(gcf,'position', [150 150 video1.Width video1.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 video1.Width video1.Height])

image(mov(1).cdata,'Parent',gca);
axis off;

movie(mov,1,video1.FrameRate);

Solution

  • Given that there may be too many files to be renamed (padded with zeros) here is a quick function that will do it for you: you just need to provide the directory/folder where the images are stored, the padding (if less 100 files, then padding can be 2; if less than 1000 files, then padding can be 3; etc.), and a common pattern. The code assumes that there is a common pattern in each file (like 'frame' or 'image') that when removed, leaves just the number:

     renameFiles(directory,padSize,fileNamePattern)
    
        filePattern = fullfile(directory, '*.jpg') %identify jpg files
        jpegFiles   = dir(filePattern) %use dir to list jpg files
    
        for k=1:size(jpegFiles)
    
            % get the source file that will be moved/renamed
            fileSrc = jpegFiles(k).name;
    
            % get the parts of the file
            [path,name,ext] = fileparts(fileSrc);
    
            % where does the pattern fit in the name?
            idx = strfind(name,fileNamePattern);
    
            % remove the pattern from the name
            if idx==0
                % pattern does not exist in file so skip it
                continue;
            elseif idx==1
                % pattern is at the beginning of name so remove it
                frameNumberStr = name(length(fileNamePattern)+1:end);
            else
                % pattern is at the end of name so remove it
                frameNumberStr = name(1:idx-1);
            end
    
            % get the number of digits
            numDigits = length(frameNumberStr);
    
            % create the new file name
            paddedName = [fileNamePattern repmat('0',1,padSize-numDigits) frameNumberStr];
            fprintf('%s\n',paddedName);
    
            % only move if the destination is different
            if strcmp(paddedName,name) ~= 1
    
                % set the destination file
                fileDest = fullfile(directory,[paddedName ext]);
    
                % move the file
                movefile(fileSrc, fileDest,'f');
            end
        end
    end
    

    An example - if all files have the common pattern of 'frame' and there are less than 1000 files, then run this function as

    rename(pwd,3,'frame')
    

    All files that were named as frame1.jpg or frame99.jpg are now named as frame001.jpg and frame099.jpg.

    Hope this helps!