1

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);
Dima
  • 38,860
  • 14
  • 75
  • 115
Ali P
  • 519
  • 6
  • 21
  • What are the names of the jpg files? I'm asking because how do you know that the order in which you read the files in is the correct frame order? For example, if your files are named something like frame1.jpg,frame2.jpg,…,frame10.jpg,frame11.jpg, etc. when you read in the list of files and write to the video object, the images may be ordered as frame1.jpg, frame10.jpg, frame11.jpg,…,frame19.jpg,frame2.jpg etc. So this may be why every tenth image is out of place. Remove the semi-colon from `fullFileName = fullfile(myFolder, baseFileName)` and re-run the script and see what the file order is. – Geoff Jun 19 '14 at 18:47
  • Also - you've named and assigned the length of the number of files as follows `size = length(jpegFiles);`. `size` is a built-in MATLAB function, so giving a variable the same name will be a problem if you ever (in later lines of the code) try to use the `size` function. Please rename this to `numOfFiles`. – Geoff Jun 19 '14 at 18:49
  • there is also a problem with `dir`, it takes input in order as given by OS which may not be sorted by name. I suggest you check your `jpegfiles` cell to see if the files are read in correct order . – Nishant Jun 19 '14 at 18:50
  • @GeoffHayes you are right. The files are out of order at every 10th frame. Is there any suggestion on how to load the file then? Thank you so much btw. – Ali P Jun 19 '14 at 21:42
  • @user2441667 - how many files/frames do you have? If they are named similar to `image1.jpg` through to `image99.jpg`, you could manually pad the single digit images with a `0`: `image01.jpg,image02.jpg,` etc. Then they will be sorted in the correct order when you read in the list of files (and if not, you could use the *sort* function to do it for you). – Geoff Jun 19 '14 at 21:55

2 Answers2

0

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!

Geoff
  • 1,603
  • 11
  • 8
0

If you have the Computer Vision System Toolbox you can use the vision.VideoFileWriter object. You can simply feed images into its step() method one at a time, and they will be written to the video file as video frames. Note that the images must all be the same size and have the same data type.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • Hi; the vision.VideoFileWriter will it allow me to use images to make the video? Seems like it uses existing videos. Im sorry am I totally new to this and really confused. Thanks – Ali P Jun 20 '14 at 17:59