In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
#from pandas import DataFrame, Series # for convenience
import trackpy as tp
import holopy as hp
import os
import sys
import datetime
import numpy as np
#from scalebar import scalebar_folder
import pims
import h5py # not necessary
import hdf5_stack # not necessary
import av
In [2]:
base = '/home/viva/group/vesicles/2015-02-19'
folder = '04.avi' # I'm going to call the movie a "folder" for compatibility with image stacks
df_folder = '01_darkfield.avi'
directory = os.path.join(base,folder) # the avi file is called a "directory" for compatibility
In [3]:
rawimages = pims.Video(directory)
rawimages[100]
Out[3]:
In [4]:
-rawimages[100]
Out[4]:
In [5]:
darkfield_folder = os.path.join(base,df_folder)
df_images = pims.Video(darkfield_folder)
In [6]:
df_images
Out[6]:
In [7]:
df_images[32]
Out[7]:
In [8]:
hp.show(df_images[32])
In [9]:
import warnings
warnings.filterwarnings('ignore')
plt.imshow(df_images[32])
Out[9]:
In [10]:
df = np.median(df_images, axis = 0)
# this returns a bunch of warnings,
# "WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24."
# but it seems to work.
In [11]:
del df_images
In [18]:
#plt.imshow?
In [12]:
plt.imshow(df)
Out[12]:
In [13]:
plt.imshow(-df)
Out[13]:
In [14]:
df
Out[14]:
In [15]:
## one way to separate RGB; I have another below.
r = np.transpose(df)[0]
hp.show(np.transpose(r))
plt.title('red darkfield')
g = np.transpose(df)[1]
hp.show(np.transpose(g))
plt.title('green darkfield')
b = np.transpose(df)[2]
hp.show(np.transpose(b))
plt.title('blue darkfield')
Out[15]:
In [23]:
## sandbox
rawimages
Out[23]:
In [24]:
## sandbox
rawimages[200] - df
Out[24]:
In [25]:
## sandbox
-(rawimages[200] - df)
Out[25]:
In [16]:
num_frames = len(rawimages)
num_frames
Out[16]:
In [31]:
## sandbox
rawimages.frame_shape
Out[31]:
In [24]:
## sandbox
rawimages.filename
Out[24]:
In [25]:
## sandbox
type(rawimages)
Out[25]:
In [26]:
## sandbox
type(rawimages[0])
Out[26]:
In [27]:
## sandbox
type(rawimages[0:1])
Out[27]:
In [47]:
## sandbox
rawimages.get_frame(1).data
Out[47]:
In [33]:
## sandbox
frame10.shape
Out[33]:
In [38]:
## sandbox
rawimages.frame_shape
Out[38]:
In [32]:
## sandbox
rawimages.get_frame(0).shape
Out[32]:
In [ ]:
## sandbox
len(rawimages)
In [33]:
## sandbox
(len(rawimages),) + rawimages.get_frame(0).shape
Out[33]:
In [ ]:
## sandbox
#np.empty((3181,1280, 1024, 3)) # number of frames, x pixels, y pixels, color dimension
np.empty((len(rawimages),) + rawimages.frame_shape, dtype=np.int16)
In [26]:
## sandbox
i=5
np.shape(np.asarray(rawimages.get_frame(i)-df))
Out[26]:
In [17]:
## background subtract the first ten frames of movie
# initialize a multi-dimensional array called frames
try:
del frames
except NameError:
pass
frames = np.empty((len(rawimages),) + rawimages.get_frame(0).shape, dtype=np.int16) #initialization
#frames = np.empty((3181, 1024,1280, 3), dtype=np.int16)
# background subtract each frame
for i in range(0,10):
frames[i] = np.asarray(rawimages.get_frame(i)-df)
# background subtract whole movie
#for i in range(0,len(rawimages)):
# frames[i] = np.asarray(rawimages.get_frame(i)-df)
In [18]:
## This is the second way to display each color of the movie separately.
hp.show(frames[5,:,:,0])
plt.title("Backgrounded frame 5, red")
hp.show(frames[5,:,:,2])
plt.title("Backgrounded frame 5, blue")
Out[18]:
In [19]:
plt.imshow(frames[6,:,:,0])
plt.title("Backgrounded frame 6, red")
Out[19]:
In [20]:
plt.imshow(frames[5,:,:,:])
plt.title("Backgrounded frame 5")
Out[20]:
In [21]:
import time
frames_min = frames.min()
if frames_min < 0:
time1 = time.time()
normalized_frames = frames-frames_min;
elapsed = time.time() - time1
print('Added ' + str(-frames_min) + ' to all frames for normalization. ',
'Process took ' + str(elapsed/60.0) + ' minutes')
else:
normalized_frames = frames;
plt.imshow(normalized_frames[5])
Out[21]:
In [31]:
r0 = np.transpose(frames0)[0]
hp.show(np.transpose(r0))
plt.title('red frame 0')
g0 = np.transpose(frames0)[1]
hp.show(np.transpose(g0))
plt.title('green frame 0')
b0 = np.transpose(frames0)[2]
hp.show(np.transpose(b0))
plt.title('blue frame 0')
Out[31]:
In [32]:
## sandbox
pims.save?
In [33]:
## sandbox
pims.imsave?
In [34]:
## sandbox
pims.export?
# I saw this function in the file pims-master/pims/display.py
pims.export(sequence = df_images, filename = '/home/viva/group/vesicles/2015-02-19/analysis/01copy.avi', format= 'bgr24')
In [ ]: