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]


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[3]:

In [4]:
-rawimages[100]


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[4]:

In [5]:
darkfield_folder = os.path.join(base,df_folder)

df_images = pims.Video(darkfield_folder)

In [6]:
df_images


Out[6]:
<Frames>
Source: /home/viva/group/vesicles/2015-02-19/01_darkfield.avi
Length: 33 frames
Frame Shape: 1280 x 1024

In [7]:
df_images[32]


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[7]:

In [8]:
hp.show(df_images[32])


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.

In [9]:
import warnings
warnings.filterwarnings('ignore')

plt.imshow(df_images[32])


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[9]:
<matplotlib.image.AxesImage at 0x7f642f0bda90>

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.


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.

In [11]:
del df_images

In [18]:
#plt.imshow?

In [12]:
plt.imshow(df)


Out[12]:
<matplotlib.image.AxesImage at 0x7f642f2a5710>

In [13]:
plt.imshow(-df)


Out[13]:
<matplotlib.image.AxesImage at 0x7f642f1b70d0>

In [14]:
df


Out[14]:
array([[[  49.,   27.,  101.],
        [  49.,   27.,  101.],
        [  50.,   28.,  102.],
        ..., 
        [  46.,   25.,   98.],
        [  44.,   26.,   99.],
        [  44.,   25.,   99.]],

       [[  49.,   27.,  101.],
        [  49.,   27.,  101.],
        [  50.,   28.,  102.],
        ..., 
        [  45.,   24.,   97.],
        [  43.,   24.,   98.],
        [  43.,   24.,   98.]],

       [[  49.,   27.,  101.],
        [  49.,   27.,  101.],
        [  50.,   27.,  101.],
        ..., 
        [  45.,   23.,   94.],
        [  43.,   23.,   92.],
        [  43.,   22.,   90.]],

       ..., 
       [[  38.,   16.,   79.],
        [  38.,   15.,   78.],
        [  36.,   15.,   74.],
        ..., 
        [  23.,   13.,   45.],
        [  22.,   15.,   41.],
        [  22.,   16.,   41.]],

       [[  38.,   15.,   82.],
        [  38.,   15.,   81.],
        [  37.,   16.,   75.],
        ..., 
        [  26.,   14.,   47.],
        [  23.,   16.,   45.],
        [  23.,   16.,   46.]],

       [[  38.,   16.,   81.],
        [  38.,   16.,   81.],
        [  37.,   17.,   76.],
        ..., 
        [  28.,   16.,   50.],
        [  26.,   18.,   49.],
        [  26.,   19.,   49.]]])

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]:
<matplotlib.text.Text at 0x7f642ed29ad0>

In [23]:
## sandbox

rawimages


Out[23]:
<Frames>
Source: /home/viva/group/vesicles/2015-02-19/04.avi
Length: 3181 frames
Frame Shape: 1280 x 1024

In [24]:
## sandbox

rawimages[200] - df


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[24]:

In [25]:
## sandbox

-(rawimages[200] - df)


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[25]:

In [16]:
num_frames = len(rawimages)
num_frames


Out[16]:
3181

In [31]:
## sandbox

rawimages.frame_shape


Out[31]:
(1280, 1024, 3)

In [24]:
## sandbox

rawimages.filename


Out[24]:
'/home/viva/group/vesicles/2015-02-19/04.avi'

In [25]:
## sandbox

type(rawimages)


Out[25]:
pims.pyav_reader.PyAVVideoReader

In [26]:
## sandbox

type(rawimages[0])


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[26]:
pims.frame.Frame

In [27]:
## sandbox

type(rawimages[0:1])


Out[27]:
generator

In [47]:
## sandbox

rawimages.get_frame(1).data


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[47]:
<read-write buffer for 0x7f1fb7a9a578, size 3932160, offset 0 at 0x7f1fb7a08630>

In [33]:
## sandbox

frame10.shape


Out[33]:
(1024, 1280, 3)

In [38]:
## sandbox

rawimages.frame_shape


Out[38]:
(1280, 1024, 3)

In [32]:
## sandbox

rawimages.get_frame(0).shape


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[32]:
(1024, 1280, 3)

In [ ]:
## sandbox

len(rawimages)

In [33]:
## sandbox

(len(rawimages),) + rawimages.get_frame(0).shape


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[33]:
(3181, 1024, 1280, 3)

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))


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
Out[26]:
(1024, 1280, 3)

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)


WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.
WARNING:libav.swscaler:No accelerated colorspace conversion found from yuv422p to rgb24.

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]:
<matplotlib.text.Text at 0x7f642eb84610>

In [19]:
plt.imshow(frames[6,:,:,0])
plt.title("Backgrounded frame 6, red")


Out[19]:
<matplotlib.text.Text at 0x7f642f0f6a90>

In [20]:
plt.imshow(frames[5,:,:,:])
plt.title("Backgrounded frame 5")


Out[20]:
<matplotlib.text.Text at 0x7f642ed8a110>

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]:
<matplotlib.image.AxesImage at 0x7f642eb2a450>

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]:
<matplotlib.text.Text at 0x7f1fb7ab4e50>

In [32]:
## sandbox

pims.save?


Object `pims.save` not found.

In [33]:
## sandbox

pims.imsave?


Object `pims.imsave` not found.

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 [ ]: