## IMPORT LIBRARIES
import matplotlib.pyplot as plt
import numpy as np
import cv2
import imutils
import time

foldMain = '/Users/JonBumstead/Desktop/18spaceTimeCamera/STvideos/'
vidNameIn = 'IMG_2927.MOV'
vidNameOutSx = 'Sx.MOV'
vidNameOutSy = 'Sy.MOV'
fileIn  = foldMain + vidNameIn
fileOutSx = foldMain + vidNameOutSx
fileOutSy = foldMain + vidNameOutSy

cap = cv2.VideoCapture(fileIn) # open video object
maxXpix = 400 # maximum number of x-pixels
numFrames = 500 # number of frames to be uploaded from video

ret, frame = cap.read() # read first frame video
temp = imutils.resize(frame, width=maxXpix) # resize image
[nPy,nPx,col]= np.shape(temp) # get dimensions of frame

V = np.zeros((nPy,nPx,3,numFrames)) # Create empty video matrix
V[:,:,:,0] = temp # store first frame in video matrix
p = 1 # initalize counter


## LOAD VIDEO
while (p<numFrames): # while loop for reading frames from video object
    ret, frame = cap.read() # read frame
    V[:,:,:,p] = imutils.resize(frame, width=maxXpix) # store frame in video matrix
    p = p+1

cap.release() # close video object


## CREATE X-TIME VIDEO
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
outSx = cv2.VideoWriter(fileOutSx, fourcc, 20, (numFrames,nPx))
Sx = np.zeros((nPx,numFrames,3)); # initialize X-time matrix

for d in range(0,nPy): # loop for reshaping matrix to space-time matrix
    temp = np.squeeze(V[d,:,:,:])
    
    for p in range(0,numFrames):
        Sx[:,p,:]=temp[:,:,p]
    
    outSx.write(Sx.astype('uint8'))

outSx.release()



## CREATE Y-TIME IMAGES
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
outSy = cv2.VideoWriter(fileOutSy, fourcc, 20, (numFrames,nPy))
Sy = np.zeros((nPy,numFrames,3)); # initialize X-time matrix

for d in range(0,nPx): # loop for reshaping matrix to space-time matrix
    temp = np.squeeze(V[:,d,:,:])
    
    for p in range(0,numFrames):
        Sy[:,p,:]=temp[:,:,p]
    
    outSy.write(Sy.astype('uint8'))

outSy.release()
    
    
    
