Exemple #1
0
def find_object(img, thresholds, sizerange, dist_thresh, erode=False, check_centers=True):
    
            body = nim.threshold(img, thresholds[0], thresholds[1])
            
            if erode is not False:
                for i in range(erode):
                    body = binary_erosion(body)
                    
            if check_centers is False:
                blobs = nim.find_blobs(body, sizerange=sizerange, aslist=False)
            else:
                blobs = nim.find_blobs(body, sizerange=sizerange, aslist=True)
            body = blobs
            
            if check_centers:
                centers = nim.center_of_blob(blobs)
                dist = []
                for center in centers:
                    diff = np.linalg.norm( center - np.array(img.shape)/2. )
                    dist.append(diff)
                body = np.zeros_like(img)
                for j, d in enumerate(dist):
                    if d < dist_thresh:
                        body += blobs[j]
                                                
            if body.max() > 1:
                body /= body.max()
            
            if body is None:
                body = np.zeros_like(img)
            
            body = np.array(body*255, dtype=np.uint8)
            
            
            return body
Exemple #2
0
def calc_post_pos(movieinfo):
    threshed_img = nim.threshold(movieinfo.background, 0, 35)
    blob = nim.find_biggest_blob(threshed_img)
    center, radius = nim.find_circle(blob,
                                     npts=200,
                                     navg=30,
                                     nstart=0,
                                     plot=False)
    movieinfo.post_pos = center
    movieinfo.post_radius_in_pixels = radius
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 #4
0
 def background_subtraction(self, raw, save_raw=False):
 
     mask_center_0 = int(self.mask_center[0])
     mask_center_1 = int(self.mask_center[1])
     mask_0_lo = max(0, mask_center_0 - self.mask_radius)
     mask_0_hi = min(self.width, mask_center_0 + self.mask_radius)
     mask_1_lo = max(0, mask_center_1 - self.mask_radius)
     mask_1_hi = min(self.width, mask_center_1 + self.mask_radius)
     if self.tracking_mask is not None:
         tracking_mask = copy.copy(self.tracking_mask)
         tracking_mask[mask_0_lo:mask_0_hi, mask_1_lo:mask_1_hi] = 1
     
     if self.dynamic_tracking_mask is True:
         # TODO: currently dynamic tracking and such only works for square format cameras 
         #print 'dynamic tracking'
         masked_img = raw[mask_0_lo:mask_0_hi, mask_1_lo:mask_1_hi]
         masked_background = self.background[mask_0_lo:mask_0_hi, mask_1_lo:mask_1_hi]
     else:
         masked_img = raw
         masked_background = self.background
     '''
     if masked_img.shape[0] < 100 or masked_img.shape[1] < 100:
         #print 'no uframe'
         self.dynamic_tracking_mask = False
         uframe = uFrame()
         return uframe, None        
     '''
         
     absdiff = nim.absdiff(masked_img, masked_background)
     if self.dynamic_tracking_mask is False and self.tracking_mask is not None:
         absdiff *= tracking_mask
         
     #absdiff = nim.auto_adjust_levels(absdiff)
     #print 'shape: ', np.shape(absdiff)
     #threshold = max( 10, absdiff.max() - THRESHRANGE )
     #print 'dynamic threshold: ', threshold 
     
     #diffthresh = nim.threshold(absdiff, threshold)
     #print 'max absdiff: ', absdiff.max()
     diffthresh = nim.threshold(absdiff, 15, threshold_hi=255)
     
     # abort early if there is no info:
     s = np.sum(diffthresh)
     if s < 10:  
         uframe = uFrame()
         #print 'no uframe, early abort, sum: ', s 
         self.dynamic_tracking_mask = False
         return uframe, None
     blobs = nim.find_blobs(diffthresh, self.blob_size_range)
     
     if blobs is None:
         self.dynamic_tracking_mask = False
         masked_img = raw
         masked_background = self.background
         
         absdiff = nim.absdiff(masked_img, masked_background)
         diffthresh = nim.threshold(absdiff, threshold)
         if self.dynamic_tracking_mask is False and self.tracking_mask is not None:
             diffthresh *= tracking_mask
         blobs = nim.find_blobs(diffthresh, self.blob_size_range)
     
     
     if blobs is None:
         uframe = uFrame()
         if save_raw is False:
             #print 'no uframe'
             self.dynamic_tracking_mask = False
             return uframe, None
         else:
             frame = Frame(raw, absdiff, diffthresh)
             return uframe, frame
         
     nblobs = blobs.max()
     #print 'n blobs: ', nblobs
     if nblobs > 1:
         blobs = nim.find_biggest_blob(blobs)
     
         
     center = nim.center_of_blob(blobs)
     #print 'center: ', center
     
     if np.isnan(center)[0] or np.isnan(center)[1]:
         uframe = uFrame()
         #print 'no uframe, NaN center!'
         self.dynamic_tracking_mask = False
         return uframe, None
     if center[0] < 1 or center[1] < 1:
         uframe = uFrame()
         #print 'no uframe, NaN center!'
         self.dynamic_tracking_mask = False
         return uframe, None
     
     #print 'center found'
     if 1:
         limlo_x = max( int(center[0])-ROI_RADIUS, 0 )
         limlo_y = max( int(center[1])-ROI_RADIUS, 0 )
         
         limhi_x = min( int(center[0])+ROI_RADIUS, masked_img.shape[0] )
         limhi_y = min( int(center[1])+ROI_RADIUS, masked_img.shape[1] )
         
         # TODO: right now object entering or leaving frame doesnt work perfectly
         uimg = masked_img[limlo_x:limhi_x, limlo_y:limhi_y]
         uimg = copy.copy(uimg)
         uimg_absdiff = absdiff[limlo_x:limhi_x, limlo_y:limhi_y]
         uimg_absdiff = copy.copy(uimg_absdiff)
         uimg_diffthresh = blobs[limlo_x:limhi_x, limlo_y:limhi_y]
         uimg_diffthresh = copy.copy(uimg_diffthresh)
         
         if self.dynamic_tracking_mask is True:
             tmp = np.array([masked_img.shape[0]/2., masked_img.shape[1]/2.])
             center = np.array(self.mask_center) + np.array(center) - tmp
             uimg_indices = [mask_0_lo+limlo_x, mask_0_lo+limhi_x, mask_1_lo+limlo_y, mask_1_lo+limhi_y]
         else:
             uimg_indices = [limlo_x, limhi_x, limlo_y, limhi_y]
         self.dynamic_tracking_mask = True
         self.mask_center = center
         
         uframe = uFrame(center, uimg, uimg_absdiff, uimg_diffthresh) 
         uframe.blobsize = blobs.sum()
         
         uframe.indices = uimg_indices
             
         if save_raw is False:
             del(raw)
             del(absdiff)
             del(diffthresh)
             del(blobs)
             return uframe, None
             
         frame = Frame(masked_img, absdiff, diffthresh)
     return uframe, frame