Esempio n. 1
0
    def score_state_full(self, state, img):
        assert len(img.shape)== 2
        x = state['x']
        y = state['y']
        theta = state['theta']
        phi = state['phi']
        img_thold = img.copy()
        img_thold[img_thold < self.params['pix-threshold']] = 0 

        if self.img_cache == None or (self.img_cache != img_thold).any():
            self.img_cache = img_thold.copy()
            coordinates = skimage.feature.peak_local_max(img, 
                                                         min_distance=30, 
                                                         threshold_rel=0.8)
            frame_regions = filters.label_regions(img_thold)

            filtered_regions = filters.filter_regions(frame_regions, 
                                                      max_width = 30,
                                                      max_height=30)
            fc = filters.points_in_mask(filtered_regions > 0, 
                                        coordinates)
            self.coordinates = fc
            # pylab.imshow(img, interpolation='nearest', cmap=pylab.cm.gray)
            # pylab.plot([p[1] for p in self.coordinates], 
            #            [p[0] for p in self.coordinates], 'r.')
            # pylab.show()

        
        # get the points 
        coordinates = self.coordinates
        
        if len(coordinates) == 0:
            return 0
        x_pix, y_pix = self.env.gc.real_to_image(x, y)
        x_pix = int(x_pix)
        y_pix = int(y_pix)
        
        front_pos_pix, back_pos_pix = util.compute_pos(self.template_obj.length, 
                                                       x_pix, y_pix, phi, theta)
        front_deltas = np.abs((coordinates - np.array(front_pos_pix)[:2][::-1]))
        front_dists = np.sqrt(np.sum(front_deltas**2, axis=1))

        
        assert len(front_dists) == len(coordinates)



        back_deltas = np.abs((coordinates - np.array(back_pos_pix)[:2][::-1]))
        back_dists = np.sqrt(np.sum(back_deltas**2, axis=1))


        dist_thold = self.params.get('dist-thold', None)
        if dist_thold != None:
            front_deltas= front_deltas[front_deltas < dist_thold]
            back_deltas = back_deltas[back_deltas < dist_thold]
        
        closest_n = self.params.get('closest-n', None)
        if closest_n != None:
            front_deltas = np.sort(front_deltas)[::-1]
            back_deltas = np.sort(back_deltas)[::-1]
            front_deltas= front_deltas[:closest_n]
            back_deltas = back_deltas[:closest_n]
            
        power = self.params.get('power', 1)
        
        delta_sum = np.sum(front_dists**power) + np.sum(back_dists**power)

        if self.params.get('normalize', True):
            delta_sum = delta_sum / len(coordinates)

        if len(coordinates) == 0:
            score = -1e10

        elif self.params.get('log', False):
            score = -np.log(delta_sum)
        elif self.params.get('exp', False):
            score = -np.exp(delta_sum)
        else:
            score = -delta_sum
            
        return score
Esempio n. 2
0
    def score_state_full(self, state, img):
        assert len(img.shape) == 2
        x = state['x']
        y = state['y']
        theta = state['theta']
        phi = state['phi']
        img_thold = img.copy()
        img_thold[img_thold < self.params['pix-threshold']] = 0

        if self.img_cache == None or (self.img_cache != img_thold).any():
            self.img_cache = img_thold.copy()
            coordinates = skimage.feature.peak_local_max(img,
                                                         min_distance=30,
                                                         threshold_rel=0.8)
            frame_regions = filters.label_regions(img_thold)

            filtered_regions = filters.filter_regions(frame_regions,
                                                      max_width=30,
                                                      max_height=30)
            fc = filters.points_in_mask(filtered_regions > 0, coordinates)
            self.coordinates = fc
            # pylab.imshow(img, interpolation='nearest', cmap=pylab.cm.gray)
            # pylab.plot([p[1] for p in self.coordinates],
            #            [p[0] for p in self.coordinates], 'r.')
            # pylab.show()

        # get the points
        coordinates = self.coordinates

        if len(coordinates) == 0:
            return 0
        x_pix, y_pix = self.env.gc.real_to_image(x, y)
        x_pix = int(x_pix)
        y_pix = int(y_pix)

        front_pos_pix, back_pos_pix = util.compute_pos(
            self.template_obj.length, x_pix, y_pix, phi, theta)
        front_deltas = np.abs(
            (coordinates - np.array(front_pos_pix)[:2][::-1]))
        front_dists = np.sqrt(np.sum(front_deltas**2, axis=1))

        assert len(front_dists) == len(coordinates)

        back_deltas = np.abs((coordinates - np.array(back_pos_pix)[:2][::-1]))
        back_dists = np.sqrt(np.sum(back_deltas**2, axis=1))

        dist_thold = self.params.get('dist-thold', None)
        if dist_thold != None:
            front_deltas = front_deltas[front_deltas < dist_thold]
            back_deltas = back_deltas[back_deltas < dist_thold]

        closest_n = self.params.get('closest-n', None)
        if closest_n != None:
            front_deltas = np.sort(front_deltas)[::-1]
            back_deltas = np.sort(back_deltas)[::-1]
            front_deltas = front_deltas[:closest_n]
            back_deltas = back_deltas[:closest_n]

        power = self.params.get('power', 1)

        delta_sum = np.sum(front_dists**power) + np.sum(back_dists**power)

        if self.params.get('normalize', True):
            delta_sum = delta_sum / len(coordinates)

        if len(coordinates) == 0:
            score = -1e10

        elif self.params.get('log', False):
            score = -np.log(delta_sum)
        elif self.params.get('exp', False):
            score = -np.exp(delta_sum)
        else:
            score = -delta_sum

        return score
    FRAMEN = len(frames)

    coordinates = []
    
    regions = np.zeros((FRAMEN, frames[0].shape[0], frames[0].shape[1]), 
                       dtype=np.uint8)
    point_est_track_data = []

    for fi, frame in enumerate(frames):
        abs_frame_index = frame_pos[fi]

        coordinates.append(skimage.feature.peak_local_max(frame, 
                                                          min_distance=30, 
                                                          threshold_abs=220))

        frame_regions = filters.label_regions(frame, mark_min=120, mark_max=220)
        regions[fi] = frame_regions
        point_est_track_data.append(dettrack.point_est_track2(frame, env, eo_params))

    pickle.dump({'frame_pos' : frame_pos, 
                 'coordinates' : coordinates, 
                 'regions' : regions, 
                 'eo_params' : eo_params, 
                 'point_est_track' : point_est_track_data, 
                 'truth' : truth, 
                 'missing' : missing, 
                 'truth_interp' : truth_interp, 
                 'derived_truth' : derived_truth, 
                 'epoch' : epoch, 
                 'frame_start' : frame_start}, 
                open(outfile, 'w'))
    coordinates = []

    regions = np.zeros((FRAMEN, frames[0].shape[0], frames[0].shape[1]),
                       dtype=np.uint8)
    point_est_track_data = []

    for fi, frame in enumerate(frames):
        abs_frame_index = frame_pos[fi]

        coordinates.append(
            skimage.feature.peak_local_max(frame,
                                           min_distance=30,
                                           threshold_abs=220))

        frame_regions = filters.label_regions(frame,
                                              mark_min=120,
                                              mark_max=220)
        regions[fi] = frame_regions
        point_est_track_data.append(
            dettrack.point_est_track2(frame, env, eo_params))

    pickle.dump(
        {
            'frame_pos': frame_pos,
            'coordinates': coordinates,
            'regions': regions,
            'eo_params': eo_params,
            'point_est_track': point_est_track_data,
            'truth': truth,
            'missing': missing,
            'truth_interp': truth_interp,