Exemple #1
0
def strobe_from_npmovie(npmovie, interval=200, frames=None):

    bkgrd = npmovie.background
    strobe_img = copy.copy(bkgrd)

    i = 0

    if frames is None:
        frames = np.arange(0, len(npmovie.uframes), interval)
    else:
        frames = np.arange(frames[0], frames[-1], interval)

    for i in frames:
        if npmovie.uframes[i].uimg is not None:

            uimg = npmovie.uframes[i].uimg
            indices = npmovie.uframes[i].indices
            # center = npmovie.uframes[i].center
            blank = 255 * np.ones_like(bkgrd)
            # blank.dtype = float

            blank[indices[0] : indices[1], indices[2] : indices[3]] = uimg
            strobe_img = nim.darken(strobe_img, blank)

    return strobe_img
def calc_object_pos(movie):

    # define background, do background subtraction and thresholding
    background = nim.darken(movie.frames[0].raw, movie.frames[-1].raw)
    for i, frame in enumerate(movie.frames):

        print 'processing frames... ', i

        movie.frames[i].absdiff = nim.absdiff(frame.raw, background)
        movie.frames[i].diffthresh = nim.threshold(movie.frames[i].absdiff, THRESH_HOLD)
        
        ## TODO: right now this assumes the thresholding is perfect...!
        # find center of fly:
        center = center_of_mass(movie.frames[i].diffthresh)
        
        if 1:
            
            if center[0] == np.nan:
                center = (0,0)
            
            limlo_x = center[0]-ROI_RADIUS
            if limlo_x < 0:
                limlo_x = 0
            limhi_x = center[0]+ROI_RADIUS
            if limhi_x > movie.height:
                limhi_x = movie.height
                
            limlo_y = center[1]-ROI_RADIUS
            if limlo_y < 0:
                limlo_y = 0
            limhi_y = center[1]+ROI_RADIUS
            if limhi_y > movie.width:
                limhi_y = movie.width
                
            # TODO: right now object entering or leaving frame doesnt work perfectly
            uimg = movie.frames[i].diffthresh[limlo_x:limhi_x, limlo_y:limhi_y]
            movie.frames[i].uimg = uimg
            movie.frames[i].uimg_center = center
            
            el = nim.fit_ellipse(uimg)
            center_relative_to_roi,xr,yr,theta_to_xaxis = el
            
            tmp = np.array([float(ROI_RADIUS)/2, float(ROI_RADIUS)/2])
            obj_pos = np.array(center) + np.array(center_relative_to_roi) - tmp
            
            if xr>=yr:
                angle = theta_to_xaxis
            else:
                angle = theta_to_xaxis+np.pi/2.
            
            movie.frames[i].obj = Object(obj_pos, angle)
            
            print xr,yr

    return movie        
Exemple #3
0
def strobe_from_movieinfo(movieinfo, interval=200, frames=None):

    bkgrd = movieinfo.background
    strobe_img = copy.copy(bkgrd)

    f = 0
    while f < len(movieinfo.frames):
        frame = movieinfo.frames[f]
        uimg = frame.uimg
        zero = frame.zero
        size = frame.uimg.shape

        blank = 255 * np.ones_like(bkgrd)
        blank[zero[0]:zero[0] + size[0], zero[1]:zero[1] + size[1]] = uimg
        strobe_img = nim.darken(strobe_img, blank)

        f += interval

    return strobe_img
Exemple #4
0
def strobe_image(movie, interval=10, timerange=None, mode='darken'):
    # combine frames from a movie using lighten or darken modes to get a 'strobe-like' sequence
    # interval is the timestamp interval in movie-time (most likely 30 frames per second)

    if timerange is None:
        timerange = [0,movie.duration]
    
    strobe_image = None
    
    timestamp = timerange[0]
    while timestamp < timerange[1]:
        raw = movie.get_frame_at_timestamp(timestamp)
        if strobe_image is None:
            strobe_image = raw
        else:
            if mode == 'darken':
                strobe_image = nim.darken(strobe_image, raw)
            elif mode == 'lighten':
                strobe_image = nim.lighten(strobe_image, raw)
        timestamp += interval
        
    return strobe_image
Exemple #5
0
 def updatefig(*args):
     anim_params['frame'] += 1 + nskip
     if anim_params['frame'] >= len(x[0])-1:
         anim_params['frame'] = 1
         anim_params['movie_finished'] = True
     anim_params['frames'].append(anim_params['frame'])
     print anim_params['frame']
     for i, fly in enumerate(flies):
         frame_start = anim_params['frame'] - ghost_tail
         frame_end = anim_params['frame']
         if frame_start < anim_params['first_frames'][i]:
             frame_start = anim_params['first_frames'][i]
         if frame_end > anim_params['final_frames'][i]:
             frame_end = anim_params['final_frames'][i]
         if frame_end < frame_start+nskip:
             continue
         frames = np.arange(frame_start, frame_end, nskip+1).tolist()
         colors = ['none' for f in range(len(x[i]))]
         edgecolors = ['none' for f in range(len(x[i]))]
         for f in frames:
             try:
                 colors[f] = color_mappable.to_rgba(color[i][f])
             except:
                 colors[f] = color[i]
             edgecolors[f] = edgecolor[i]
         
         fly.set_edgecolors(edgecolors)
         fly.set_facecolors(colors)
         
     if images is not None:
         if hasattr(images, '__call__'):
             imgdata = images(frames[-1])
             if len(imgdata.shape) > 2:
                 imgdata = imgdata[:,:,0]
             if flipimgx:
                 if flipimgy:
                     imgdata = imgdata[::-1,::-1]
                 else:
                     imgdata = imgdata[::-1,:]
             elif flipimgy:
                 imgdata = imgdata[:,::-1]
             else:
                 imgdata = imgdata[:,:]
          
         else:   
             imgdata = get_image_data(images, frames[-1], mono=mono, flipimgx=flipimgx, flipimgy=flipimgy)
             
         if anim_params['strobe']:
             if anim_params['strobeimg'] is None:
                 anim_params['strobeimg'] = imgdata
             else:
                 anim_params['strobeimg'] = nim.darken([anim_params['strobeimg'], imgdata])
             img.set_array(anim_params['strobeimg'])
         else:
             img.set_array(imgdata)
             
         
     if save and not anim_params['movie_finished']:
         print 'saving frame: ', str(anim_params['frame']), ' -- if the animation you see is strange, do not worry, look at the pngs'
         frame_prefix = '_tmp'
         frame_prefix = os.path.join(save_movie_path, frame_prefix)
         strnum = str(anim_params['frame'])
         num_frame_digits = np.ceil( np.log10(len(x[0]))) + 1
         while len(strnum) < num_frame_digits:
             strnum = '0' + strnum
         frame_name = frame_prefix + '_' + strnum + '_' + '.png'
         flytext.set_fontsize(fig, 8)
         fig.savefig(frame_name, format='png')
         
     if save and anim_params['movie_finished']:
         print 
         print 'Movie finished saving! Close the plot screen now.'
         print 'PNGs are at: ', save_movie_path
         print 'To turn the PNGs into a movie, you can run this command from inside the directory with the tmp files: '
         print 'mencoder \'mf://*.png\' -mf type=png:fps=30 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o animation.avi'
     
     if images is None:
         return flies
     else:
         animation_objects = [fly for fly in flies]
         animation_objects.insert(0, img)
         return animation_objects
Exemple #6
0
    def updatefig(*args):
        anim_params['frame'] += 1 + nskip
        if anim_params['frame'] >= len(x[0]) - 1:
            anim_params['frame'] = 1
            anim_params['movie_finished'] = True
        anim_params['frames'].append(anim_params['frame'])
        print anim_params['frame']
        for i, fly in enumerate(flies):
            frame_start = anim_params['frame'] - ghost_tail
            frame_end = anim_params['frame']
            if frame_start < anim_params['first_frames'][i]:
                frame_start = anim_params['first_frames'][i]
            if frame_end > anim_params['final_frames'][i]:
                frame_end = anim_params['final_frames'][i]
            if frame_end < frame_start + nskip:
                continue
            frames = np.arange(frame_start, frame_end, nskip + 1).tolist()
            colors = ['none' for f in range(len(x[i]))]
            edgecolors = ['none' for f in range(len(x[i]))]
            for f in frames:
                try:
                    colors[f] = color_mappable.to_rgba(color[i][f])
                except:
                    colors[f] = color[i]
                edgecolors[f] = edgecolor[i]

            fly.set_edgecolors(edgecolors)
            fly.set_facecolors(colors)

        if images is not None:
            if hasattr(images, '__call__'):
                imgdata = images(frames[-1])
                if len(imgdata.shape) > 2:
                    imgdata = imgdata[:, :, 0]
                if flipimgx:
                    if flipimgy:
                        imgdata = imgdata[::-1, ::-1]
                    else:
                        imgdata = imgdata[::-1, :]
                elif flipimgy:
                    imgdata = imgdata[:, ::-1]
                else:
                    imgdata = imgdata[:, :]

            else:
                imgdata = get_image_data(images,
                                         frames[-1],
                                         mono=mono,
                                         flipimgx=flipimgx,
                                         flipimgy=flipimgy)

            if anim_params['strobe']:
                if anim_params['strobeimg'] is None:
                    anim_params['strobeimg'] = imgdata
                else:
                    anim_params['strobeimg'] = nim.darken(
                        [anim_params['strobeimg'], imgdata])
                img.set_array(anim_params['strobeimg'])
            else:
                img.set_array(imgdata)

        if save and not anim_params['movie_finished']:
            print 'saving frame: ', str(
                anim_params['frame']
            ), ' -- if the animation you see is strange, do not worry, look at the pngs'
            frame_prefix = '_tmp'
            frame_prefix = os.path.join(save_movie_path, frame_prefix)
            strnum = str(anim_params['frame'])
            num_frame_digits = np.ceil(np.log10(len(x[0]))) + 1
            while len(strnum) < num_frame_digits:
                strnum = '0' + strnum
            frame_name = frame_prefix + '_' + strnum + '_' + '.png'
            flytext.set_fontsize(fig, 8)
            fig.savefig(frame_name, format='png')

        if save and anim_params['movie_finished']:
            print
            print 'Movie finished saving! Close the plot screen now.'
            print 'PNGs are at: ', save_movie_path
            print 'To turn the PNGs into a movie, you can run this command from inside the directory with the tmp files: '
            print 'mencoder \'mf://*.png\' -mf type=png:fps=30 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o animation.avi'

        if images is None:
            return flies
        else:
            animation_objects = [fly for fly in flies]
            animation_objects.insert(0, img)
            return animation_objects