%% Video Writing Script
% Created by Charles T Pett
% Date created: 2022-09-02

% Description: Takes image sequence recorded by high-speed camera and
% converts into an mp4 video file.
% Need to be one directory outside of the directory that contains the image
% files.

clear; clc; close all
% Count number of images in Image directory
imFolderInfo = dir('Run 1');
subtract = 2;
numIm = numel(imFolderInfo) - subtract;
filename = 'Run1';

% Video write setup
vid = VideoWriter(filename,'MPEG-4');
fps = 6;
vid.FrameRate = fps;
vid.Quality = 100;
open(vid)

for i = subtract+1:numIm
    % Read in tiff images from pco dimax high-speed camera and adjust the
    % contrast to resolve the image because original range was [0 4025]
    im{i-subtract} = imadjust(imread(imFolderInfo(i).name));
    % crop{i-2} = imcrop(im{i-2}, [0 0 500 500]);
    % binary{i-2} = imbinarize(crop{i-2});
    
    imshow(im{i-subtract})
    counter = i - subtract
    
    % Converting uint16 images to uint8 class and writing the video frame 
    % by frame.
    uint8{i-subtract} = im2uint8(im{i-subtract});
    writeVideo(vid,uint8{i-subtract})

end

% End video writing
close(vid)
