Exemplo n.º 1
0
    def from_args(cls, cine_fname, h5_fname=None, *args, **kwargs):
        self = cls()
        '''Sets up the object based on arguments
        '''

        cls._verify_params(kwargs)

        self.params = kwargs
        try:
            self.bck_img = self.params.pop('bck_img')
        except KeyError:
            self.bck_img = None

        self.cine_fname = cine_fname

        self.cine_ = cine.Cine('/'.join(self.cine_fname))

        if self.bck_img is None:
            # not passed in, try the data base
            if self.db is not None:
                self.bck_img = self.db.get_background_img(self.cine_.hash)
            # if that fails too, run it
            if self.bck_img is None:
                self.bck_img = infra.gen_bck_img('/'.join(self.cine_fname))
                # if we have a data base, shove in the data
                if self.db is not None:
                    self.db.store_background_img(self.cine_.hash, self.bck_img)

        return self
Exemplo n.º 2
0
    def from_hdf_file(cls, cine_base_path, h5_fname):
        ''' Sets up object to process data based on MD in an hdf file.
        '''
        self = cls()
        tmp_file = h5py.File('/'.join(h5_fname), 'r')
        keys_lst = list(tmp_file.attrs.keys())
        lc_req_args = ['tck0', 'tck1', 'tck2']
        h5_req_args = ['cine_path', 'cine_fname']
        cls._verify_params(keys_lst, extra_req=(lc_req_args + h5_req_args))

        self.params = dict(tmp_file.attrs)

        for k in lc_req_args:
            del self.params[k]

        self.cine_fname = FilePath(cine_base_path,
                                   self.params.pop('cine_path'),
                                   self.params.pop('cine_fname'))
        self.cine_ = cine.Cine('/'.join(self.cine_fname))

        if 'bck_img' in list(tmp_file.keys()):
            self.bck_img = tmp_file['bck_img'][:]
        else:
            self.bck_img = infra.gen_bck_img('/'.join(self.cine_fname))

        seed_curve = infra.SplineCurve.from_hdf(tmp_file)

        tmp_file.close()

        return self, seed_curve
Exemplo n.º 3
0
def make_frames(root_dir, video_path, fn_for_movie='video.mp4', color_by='phase', cmap='isolum_rainbow'):
    save_directory = os.path.join(root_dir, 'movie_frames_' + color_by + '_' + cmap)

    mod_list = [16, 8, 4, 2, 1]

    if not os.path.exists(save_directory):
        os.mkdir(save_directory)

    try:
        c = cine.Cine(video_path)
    except:
        c = []

    path = os.path.join(root_dir, 'com_data.hdf5')
    data = h5py.File(path, 'r')
    keys = data.keys()
    length = len(data['times'])
    neighborhood = 75

    for j in xrange(len(mod_list)):
        avg_x = []
        avg_y = []

        for i in xrange(length):
            frame_name = os.path.join(save_directory, '%05d.png' % i)
            if i % mod_list[j] == 0 and not (os.path.isfile(frame_name)):

                if True:

                    if i < neighborhood:

                        lb = 0
                    else:
                        lb = i - neighborhood

                    if length - 5 < i + neighborhood:
                        ub = length - 5

                    else:
                        ub = i + neighborhood

                    xs, ys = get_average_positions(data, keys, range=[lb, ub])

                    avg_x.append(xs)
                    avg_y.append(ys)
                    time, pts = get_frame_points_and_time(data, keys, i)

                    if len(c) > 0:
                        frame = c[i].astype('f')
                    else:

                        frame = np.zeros((600, 600))

                    plot_on_frame(frame, time, pts, np.array([xs, ys]).T, save_name=frame_name, plotting_mag=5,
                                  color_by=color_by, cmap=cmap)
            if mod_list[j] == 1 and i >= length - 2:
                call(["ffmpeg", "-r", "50", "-start_number", str(0), "-i", save_directory + "/%05d.png", "-f", "mp4",
                      save_directory + "h264", "-pix_fmt", "yuv420p", os.path.join(save_directory, fn_for_movie)])
Exemplo n.º 4
0
    def __init__(self, input_file, frame_rate=40.):
        # first determine the file type (cine, tiff, png, etc)
        self.file_type = input_file.split('.')[-1]

        if self.file_type == 'cine':
            self.cine = True
            self.data = cine.Cine(input_file)
            self.num_frames = len(self.data)
        else:
            self.cine = False
            file_names = thf.find_files_by_extension(input_file,
                                                     '.png',
                                                     tot=True)
            data = []
            for i in xrange(20):
                file = file_names[i]
                data_single = mpimg.imread(file)
                data_single = self.rgb2gray(data_single)
                data_single = data_single[:, 100:1400]
                data.append(data_single)
                print np.shape(data_single)
                print file
            self.data = data
            self.num_frames = len(self.data)

        # print 'data = ', self.data
        # print 'type(data) = ', type(self.data)
        # print 'data[0] = ', self.data[0]
        # print 'type(data[0]) = ', type(self.data[0])
        # sys.exit()

        self._mean_value = 0
        self.min_radius = 17
        self.max_radius = 22
        self.min_dist = 20
        self._min_value = 0.05
        self._max_value = 0.7
        self._pix = 6
        self._centroid_clipmin = 0.0
        self._centroid_clipmax = 1.0

        self.current_frame = []
        self.previous_frame = None
        self.average_frame = None
        self.variance_frame = None
        self.normfactor_frame = None
        self.maxdiff_frame = None
        self.roi = None
        self.frame_current_points = []
        self.reference_points = None
        self.reference_mask = None
        self.ref_masks = []
        self.circles = []
        self.current_time = 0
        self.frame_rate = frame_rate

        self._adjust_min_max_val()
        self._set_dummy_frame()
Exemplo n.º 5
0
def gen_bck_img(fname):
    '''Computes the background image'''
    c_test = cine.Cine(fname)
    bck_img = reduce(lambda x, y: x + y, c_test,
                     np.zeros(c_test.get_frame(0).shape))
    print(c_test.len())
    bck_img /= c_test.len()
    # hack to deal with
    bck_img[bck_img == 0] = .001
    return bck_img
Exemplo n.º 6
0
def get_cine_hashes(base_path, search_path):
    '''returs all paths and cine hash values under the search path'''
    cine_fnames = []
    for dirpath, dirnames, fnames in os.walk(base_path + '/' + search_path):
        cine_fnames.extend([
            FilePath(base_path, dirpath[len(base_path) + 1:], f)
            for f in fnames if 'cine' in f
        ])
    cine_fnames.sort(key=lambda x: x[-1])
    cine_hash = [cine.Cine(cn.format).hash for cn in cine_fnames]

    return list(zip(cine_fnames, cine_hash))
Exemplo n.º 7
0
def disp_frame(fname, n, bck_img=None):
    '''Displays a given frame from the file'''

    c_test = cine.Cine(fname)

    lfimg = c_test.get_frame(n)

    if bck_img is None:
        bck_img = np.ones(lfimg.shape)
    fig = plt.figure()
    ax = fig.add_axes([.1, .1, .8, .8])
    im = ax.imshow(lfimg / bck_img)
    im.set_clim([.5, 1.5])
    ax.set_title(n)
    plt.draw()
Exemplo n.º 8
0
def setup_spline_fitter(fname, bck_img=None):
    ''' gets the initial path '''
    clims = [.5, 1.5]
    # open the first frame and find the initial circle
    c_test = cine.Cine(fname)
    lfimg = c_test.get_frame(0)
    if bck_img is None:
        bck_img = np.ones(lfimg.shape)
        clims = None
    fig = plt.figure()
    ax = fig.add_axes([.1, .1, .8, .8])
    im = ax.imshow(lfimg / bck_img, cmap='cubehelix')
    if clims is not None:
        im.set_clim(clims)
    ef = spline_fitter(ax)
    plt.draw()

    return ef
Exemplo n.º 9
0
def update_average_cache(base_path):
    cine_fnames = []
    for dirpath, dirnames, fnames in os.walk(base_path + '/' + 'leidenfrost'):
        cine_fnames.extend([
            FilePath(base_path, dirpath[len(base_path) + 1:], f)
            for f in fnames if 'cine' in f
        ])

    db = ldb.LFmongodb()

    for cn in cine_fnames:
        if 'cine' not in cn[-1]:
            continue
        cine_hash = cine.Cine(cn.format).hash
        bck_img = db.get_background_img(cine_hash)
        if bck_img is None:
            bck_img = gen_bck_img(cn.format)
            db.store_background_img(cine_hash, bck_img)
Exemplo n.º 10
0
def play_movie(fname, bck_img=None):
    '''plays the movie with correction'''
    def update_img(num, F, bck_img, im, txt):
        im.set_data(F.get_frame(num) / bck_img)
        txt.set_text(str(num))

    F = cine.Cine(fname)

    if bck_img is None:
        bck_img = np.ones(F.get_frame(0).shape)
    fig = plt.figure()
    ax = fig.add_subplot(111)

    im = ax.imshow(F.get_frame(0) / bck_img)
    fr_num = ax.text(0.05, 0.05, 0, transform=ax.transAxes)
    im.set_clim([.75, 1.25])
    prof_ani = animation.FuncAnimation(fig,
                                       update_img,
                                       len(F),
                                       fargs=(F, bck_img, im, fr_num),
                                       interval=50)
    plt.show()

    return prof_ani
Exemplo n.º 11
0
        dest='background_amplitude',
        type=float,
        default=0.22,
        help=
        'fraction of max laser brightness that is background instead of gaussian peak; set to 1 for no brightness correction, 0 for fitting to a straight gaussian [default: 0.22 != 0 because of spherical-aberation/best fit]'
    )
    args = parser.parse_args()

    for fn in args.input:
        base, ext = os.path.splitext(fn)

        if ext.lower().startswith('.tif'):
            sys.setrecursionlimit(10**5)
            source = cine.Tiff(fn)
        else:
            source = cine.Cine(fn)

        try:
            args.real_bpp = source.real_bpp
        except:
            args.real_bpp = soure[0].dtype.itemsize * 8

        max_frame = (len(source) - args.skip) // args.depth
        saved_frames = eval_slice(args.range, max_frame)

        print '%s (%d frames):' % (fn, len(saved_frames))

        frame_offsets = array(eval_slice(args.displayframes, args.depth))

        if args.threads == 1:
            results = []
Exemplo n.º 12
0
                    "frames per volume (depth) must either be specified with '-d' or part of the file name (e.g.: 'XXX_100fpv_YYY')"
                )
        else:
            frames_per_volume = args.depth

        #print frames_per_volume

        #display_range = slice(*[if_int(x) for x in args.displayframes.split(':')])
        #DATA = Image4D(args.input, args.depth, brighten=args.brightness, clip=args.clip, series=args.series, offset=args.offset, display_range=display_range)
        #output = args.output if args.output else os.path.splitext(args.input)[0] + '.s4d'

        if ext.lower().startswith('.tif'):
            sys.setrecursionlimit(10**5)
            source = cine.Tiff(input)
        else:
            source = cine.Cine(input)

        max_frame = (len(source) - args.skip) // frames_per_volume
        saved_frames = eval_slice(args.range, max_frame)

        print '%30s -> %-30s (%d frames)...' % (input, output,
                                                len(saved_frames))
        sys.stdout.flush()

        #continue

        output = Sparse4D(
            output, 'w', {
                'source': input,
                'frames per volume': frames_per_volume,
                'min clip': args.min_val,
Exemplo n.º 13
0
def convert_file(ifn, ofn=None, clip=0, max_frame=None, overwrite=False):
    if ofn is None:
        ofn = os.path.splitext(ifn)[0] + '.sparse'
    
    if not overwrite and os.path.exists(ofn):
        print "%s exists... skipping. (use --overwrite to override)" % ofn
        return
    
    c = cine.Cine(ifn)
    
    header = {}
    for key, t in all_fields:
        val = getattr(c, key)
        if type(val) == str and '\x00' in val: val = val[:val.index('\x00')]
        #print '%20s: %s' % (key, repr(val))
        header[key] = repr(val)
        
    header['original'] = ifn
    header['clip'] = str(clip)
        
    if (not max_frame) or (max_frame > len(c)): max_frame = len(c)
        
        
    
    p = -1
    
    ifp, ifnn = os.path.split(ifn)
    ofp, ofnn = os.path.split(ofn)
    
    max_len = 70
    
    print ifn
    print '  -> ' + ofn
#    msg = ' --> %s' %(ofn if ofp != ifp else ofnn)
#    if len(msg) > max_len: msg = msg[:max_len-3] + '...'   
#    print msg,
#    sys.stdout.flush()

    s = cine.Sparse(ofn, 'w', header, max_blocks=max_frame) 
    max_val = iinfo(c[0].dtype).max


    gtot = 0
    ctot = 0
    stot = 0
    for i in range(max_frame):
        st = time.time()
        f = c[i]
        gtot += (time.time() - st)
        
        st = time.time()
        f = nclip(f, clip, max_val) - clip
        ctot += (time.time() - st)
        
        st = time.time()
        s.append_array(f)
        stot += (time.time() - st)
        
        pn = i * 100 // max_frame
        if pn != p:
            print '\r  %3d%% (load:%6d s, clip:%6d s, save:%6d s)' % (pn, gtot, ctot, stot),
            sys.stdout.flush()
            #print gtot, ctot, stot
            p = pn
        
    print '\r  done. (load:%6d s, clip:%6d s, save:%6d s)' % (gtot, ctot, stot)
        
    s.close()
Exemplo n.º 14
0
    def __init__(self,
                 fname,
                 cine_base_path=None,
                 mode='r',
                 i_disk_dict=None,
                 *args,
                 **kwargs):
        """
        Parameters
        ----------
        fname: `Leidenfrost.FilePath`
            Fully qualified path to the hdf file to open
        cine_base_path: str or `None`
            If not `None`, base path to find the raw cine files
        h5_buffer_bas_path: str or `None`
            If not `None`, base path for buffering the h5 file
        cine_buffer_base_path: str or `None`
            If not `None`, base path for buffering the cine file
        """
        self._iter_cur_item = -1
        self.file = None

        if mode == 'rw':
            self.file = h5py.File(fname.format, 'r+')
            self.writeable = True
        else:
            self.file = h5py.File(fname.format, 'r')
            self.writeable = False

        self.num_frames = len([k for k in self.file.keys() if 'frame' in k])
        self._prams = HdfBEPram(False, True)
        self.proc_prams = dict(self.file.attrs)

        if cine_base_path is not None:
            self.cine_fname = FilePath(cine_base_path,
                                       self.file.attrs['cine_path'],
                                       self.file.attrs['cine_fname'])
            self.cine = cine.Cine('/'.join(self.cine_fname))
        else:
            self.cine_fname = None
            self.cine = None
        try:
            # hard code the mongodb
            self.db = db.LFmongodb(i_disk_dict=i_disk_dict)
        except:
            print('gave up and the DB')
            # this eats _ALL_ exceptions
            self.db = None

        self._procid = None
        if self.db is not None:
            self._procid = self.db.get_proc_id(fname)

        self.bck_img = None
        if self.db is not None and self.cine is not None:
            self.bck_img = self.db.get_background_img(self.cine.hash)
        # if that fails too, run it
        if self.bck_img is None and self.cine is not None:
            self.gen_back_img()
            # if we have a data base, shove in the data
            if self.db is not None and self.bck_img is not None:
                self.db.store_background_img(self.cine.hash, self.bck_img)

        if 'ver' not in self.file.attrs or self.file.attrs['ver'] < b'0.1.5':
            self._frame_str = 'frame_{:05d}'
        else:
            self._frame_str = 'frame_{:07d}'

        self._cal_val = None
        self._cal_val_unit = None
        self._first_frame = None
        self._last_frame = None
Exemplo n.º 15
0
     except:
         print(' !! no .3dsetup file, either specify with -s or place in source directory')
         continue
 else:
     setupfile = args.setup    
 
 setup_str = open(setupfile, 'rt').read()
 setup = eval_vals(setup_str)
 
 for rf in __REQUIRED_FIELDS:
     if rf not in setup:
         print("ERROR: field '%s' missing from setup file! (%s)" % (rf, setupfile))
         sys.exit()
 
 if ee == '.cine':
     input = cine.Cine(fn)
     bit_depth = input.real_bpp
     bottom_clip = setup['bottom_clip']
     u1_top = setup['u1_top']
 
 else:
     input = cine.Sparse(fn)
     bit_depth = try_eval(input.header['real_bpp'])
     bottom_clip = 0
     u1_top = setup['u1_top'] - try_eval(input.header.get('clip', 0)) #Clip was already applied in sparsing
     
 num_frames = len(input) // setup['cine_depth']
 test_frame = input[0]
 header = {'original_bitdepth':bit_depth, 'original_file':fn, 'bottom_clip':bottom_clip, '3dsetup':setup_str, 'use_3dsetup_perspective':True, 'dtype':args.type, 'frame size': test_frame.shape}
 
 #print header
Exemplo n.º 16
0
 def __init__(self, cine_fname=None, bck_img=None):
     self.back_img = bck_img
     self.cine_fname = cine_fname
     self.cine = cine.Cine(cine_fname)
     self.frames = {}
     pass
Exemplo n.º 17
0
def track_and_save(fn, vid_dirs, img_ker_string):
    'tracking...'
    start_time = time.time()
    img_ker = mpimg.imread(img_ker_string)

    c = cine.Cine(fn)
    tot_frames = len(c)
    com_data = []
    gamma = 2.2

    ff = 0
    lf = tot_frames
    lf = int(lf)
    ff = int(ff)

    rd_s = '/Volumes/labshared3/Lisa'

    output_dir_i = fn.split('.')[0]
    output_dir_i2 = output_dir_i.split('/')

    output_dir = rd_s + '/' + time.strftime("%Y_%m_%d") + '_video_tracks_'
    if not os.path.exists(output_dir): os.mkdir(output_dir)

    output_dir = rd_s + '/' + time.strftime(
        "%Y_%m_%d"
    ) + '_video_tracks_' + '/' + vid_dirs + '_' + output_dir_i2[-1] + '/'
    if not os.path.exists(output_dir): os.mkdir(output_dir)

    com_data = []

    tot_frames = lf
    val = 0.8 * max(c[ff].astype(float).flatten())

    points = zeros([tot_frames - ff, 2], dtype='f')
    for i in range(tot_frames - ff):

        if i == 0:
            minval = .04 * val
            maxval = 0.13 * val

        else:
            minval = 0.04 * val
            maxval = 0.13 * val

        frame = (clip(c[ff + i].astype('f'), minval, maxval) -
                 minval) / (maxval - minval)

        if i == 0:
            cine.asimage(frame).save(output_dir + 'original.png')
            print 'saved original', output_dir

            fr = ndimage.convolve(frame, img_ker, mode='reflect', cval=0.0)
            minval = 0. * max(fr.flatten())
            maxval = 1 * max(fr.flatten())
            fr = (clip(fr, minval, maxval) - minval) / (maxval - minval)

            points = find_track(fr, frame, output_dir)

            pix = 7

            fig = plt.figure()
            img = Image.open(output_dir + 'original.png')
            img = array(img)

            for j in range(len(points)):
                img[points[j, 1] - pix:points[j, 1] + pix,
                    points[j, 0] - pix:points[j, 0] + pix] = array(
                        ImageOps.invert(
                            Image.fromarray(
                                np.uint8(img[points[j, 1] - pix:points[j, 1] +
                                             pix, points[j, 0] -
                                             pix:points[j, 0] + pix]))))

            plt.imshow(img, cmap=cm.Greys_r)
            plt.savefig(output_dir + 'tracked_areas.png')

        points = track_points(frame.astype('f'), points, pix, i,
                              tot_frames - ff, output_dir).astype(float)

        t = array([[ff + i, points[j, 0], points[j, 1]]
                   for j in range(len(points))])
        com_data.append(t)

    com_data = array(com_data)
    sh1, sh2, sh3 = shape(com_data)
    com_data = com_data.flatten()
    com_data = reshape(com_data, [sh1 * sh2, 3])

    num_times = len(com_data[::, 0])

    for i in range(num_times):
        com_data[i, 0] = c.get_time(int(com_data[i, 0]))

    dump_pickled_data(output_dir, 'com_data', com_data)

    c.close()

    end_time = time.time()

    total_time = end_time - start_time

    print 'tracked ', tot_frames, 'frames in', total_time, 'seconds'

    return com_data, output_dir