Exemplo n.º 1
0
def transform_real(coordinates_prgls, coor_pre_real_t, BETA_t, C_t, i, rep, draw=True):
    """
    Predict cell coordinates using one set of the transformation parameters
        from fnn_prgls()
    Input:
        coordinates_prgls: the coordinates before transformation
        coor_pre_real_t, BETA_t, C_t: one set of the transformation parameters
        i: the number of the repetition the set of parameters come from (used if draw==True)
        rep: the total number of repetition (used if draw==True)
        draw: whether draw the intermediate results or not
    Return:
        coordinates_prgls_2: the coordinates after transformation
    """
    length_cells = np.size(coordinates_prgls, axis=0)
    length_auto_segmentation = np.size(coor_pre_real_t, axis=0)
    Gram_matrix = np.zeros((length_auto_segmentation,length_cells))
    for idx_i in range(length_cells):
        for idx_j in range(length_auto_segmentation):
            Gram_matrix[idx_j,idx_i] = np.exp(-np.sum(np.square(
                    coordinates_prgls[idx_i,:]-coor_pre_real_t[idx_j,:]))/
                       (2*BETA_t*BETA_t)) 
    coordinates_prgls_2 = np.matrix.transpose(np.matrix.transpose(
            coordinates_prgls)+np.dot(C_t,Gram_matrix))
    
    if draw:
        plt.subplot(rep,2,i*2+1)
        tracking_plot(coordinates_prgls, coordinates_post_real, coordinates_prgls_2)
        plt.subplot(rep, 2, i*2+2)
        tracking_plot_zx(coordinates_prgls, coordinates_post_real, coordinates_prgls_2)
    
    return coordinates_prgls_2
Exemplo n.º 2
0
def fnn_prgls(coordinates_pre_real, coordinates_post_real, rep, draw=True):
    """
    Appliy FFN + PR-GLS from t1 to t2 (multiple times) to get transformation 
        parameters to predict cell coordinates
    Input:
        coordinates_pre_real, coordinates_post_real: segmented cell coordinates from two volumes
        rep: the number of reptitions of (FFN + max_iteration times of PR-GLS)
        draw: if True, draw predicted coordinates from t1 and segmented coordinates of cells at t2
    Return:
        C_t: list of C in each repetition (to predict the transformed coordinates)
        BETA_t: list of the parameter beta used in each repetition (to predict coordinates)
        coor_real_t: list of the pre-transformed coordinates of automatically 
            segmented cells in each repetition (to predict coordinates)
    """
    pre_transformation = coordinates_pre_real.copy()
    C_t = []
    BETA_t = []
    coor_real_t = []
    for i in range(rep):
        coor_real_t.append(pre_transformation)
        init_match = initial_matching(FNN_model, pre_transformation, coordinates_post_real, 20)
        pre_transformation_pre = pre_transformation.copy()
        [P, pre_transformation, C] = pr_gls(pre_transformation, 
            coordinates_post_real, init_match, BETA=BETA*(0.8**i), max_iteration=max_iteration, 
            LAMBDA=LAMBDA)
        C_t.append(C)
        BETA_t.append(BETA*(0.8**i))
        if draw:
            plt.subplot(rep,2,i*2+1)
            tracking_plot(pre_transformation_pre, coordinates_post_real, pre_transformation)
            plt.subplot(rep, 2, i*2+2)
            tracking_plot_zx(pre_transformation_pre, coordinates_post_real, pre_transformation)
    return C_t, BETA_t, coor_real_t
Exemplo n.º 3
0
def draw_tracking_results(i, pre_transformation_pre, r_coordinates_post_real,
                          pre_transformation, rep):
    plt.figure(figsize=(16, 16))
    plt.subplot(rep, 2, i * 2 + 1)
    tracking_plot(pre_transformation_pre, r_coordinates_post_real,
                  pre_transformation)
    plt.subplot(rep, 2, i * 2 + 2)
    tracking_plot_zx(pre_transformation_pre, r_coordinates_post_real,
                     pre_transformation)
def test_tracking(vol1, vol2):
    """
    Test whether the parameters for tracking are proper (generating figures for the transformation)
    Input: 
        vol1, vol2: the numbers of two volumes for testing the registration between them
    """
    segmentation(vol1)
    coordinates_pre = np.asarray(center_coordinates)
    coordinates_pre_real=coordinates_pre.copy()
    coordinates_pre_real[:,2]=coordinates_pre[:,2]*z_xy_resolution_ratio
    
    segmentation(vol2)
    coordinates_post = np.asarray(center_coordinates)
    coordinates_post_real=coordinates_post.copy()
    coordinates_post_real[:,2]=coordinates_post[:,2]*z_xy_resolution_ratio
    
    init_match = initial_matching(FNN_model, coordinates_pre_real, coordinates_post_real, 20)

    [P, pre_transformation, C] = pr_gls(coordinates_pre_real, 
        coordinates_post_real, init_match, BETA=BETA, max_iteration=max_iteration, 
        LAMBDA=LAMBDA)
    
    tracking_plot(coordinates_pre_real, coordinates_post_real, pre_transformation)
    tracking_plot_zx(coordinates_pre_real, coordinates_post_real, pre_transformation)
Exemplo n.º 5
0
def match(volume1,
          volume2,
          par_image,
          par_tracker,
          par_path,
          par_subregions,
          r_coor_segment_pre,
          r_coor_tracked_pre,
          r_coor_confirmed_vol1,
          cells_on_boundary,
          unet_model,
          FFN_model,
          r_disp_from_vol1_input,
          seg_cells_interp,
          cell_t1,
          seg_t1,
          method="min_size"):
    """
    Match current volume and another volume2
    Input:
        volume1, volume2: the two volume to be tested for tracking
        r_coor_segment_pre, r_coor_tracked_pre: the coordinates of cells from segmentation or tracking in previous volume
        r_coor_confirmed_vol1: coordinates of cells in volume #1
        r_disp_from_vol1: displacement (from vol1) of cells in previous volume
        seg_cells_interp: segmentation (interpolated)
        cell_t1, seg_t1: cell-regions/segmentation in vol1
    """
    print('t=%i' % volume2)

    #######################################################
    # skip frames that cannot be tracked
    #######################################################
    if volume2 in par_image["miss_frame"]:
        print("volume2 is a miss_frame")
        return None

    ########################################################
    # generate automatic segmentation in current volume
    ########################################################
    image_cell_bg, l_center_coordinates, _, image_gcn = \
        segmentation(volume2, par_image, par_tracker, par_path, unet_model,
                     method=method, neuron_num=par_tracker["neuron_num"])

    t = time.time()
    r_coordinates_segment_post = displacement_image_to_real(
        l_center_coordinates, par_image)
    #######################################
    # track by fnn + prgls
    #######################################
    # calculate the mean predictions of each cell locations
    r_coor_prgls = predict_pos_once(r_coor_segment_pre,
                                    r_coordinates_segment_post,
                                    r_coor_tracked_pre,
                                    par_tracker,
                                    FFN_model,
                                    draw=True)
    print('fnn + pr-gls took %.1f s' % (time.time() - t))
    #####################
    # boundary cells
    #####################
    cells_bd = get_cells_onBoundary(r_coor_prgls, par_image)
    print("cells on boundary:", cells_bd[0] + 1)
    cells_on_boundary_local = cells_on_boundary.copy()
    cells_on_boundary_local[cells_bd] = 1

    ###################################
    # accurate correction
    ###################################
    t = time.time()
    # calculate r_displacements from the first volume
    # r_displacement_from_vol1: accurate displacement; i_disp_from_vol1: displacement using voxels numbers as unit
    r_disp_from_vol1 = r_disp_from_vol1_input + r_coor_prgls - r_coor_tracked_pre
    i_disp_from_vol1 = displacement_real_to_interpolatedimage(
        r_disp_from_vol1, par_image)

    i_cumulated_disp = i_disp_from_vol1 * 0.0

    print("FFN + PR-GLS: Left: x-y; Right: x-z")
    plt.pause(10)
    print("Accurate correction:")
    rep_correction = 5
    for i in range(rep_correction):
        # update positions (from vol1) by correction
        r_disp_from_vol1, i_disp_from_vol1, r_disp_correction = \
            correction_once_interp(
            i_disp_from_vol1, par_image, par_subregions, cells_on_boundary_local,
            r_coor_confirmed_vol1, image_cell_bg, image_gcn, seg_cells_interp
        )
        # stop the repetition if correction converged
        stop_flag = evaluate_correction(r_disp_correction, i_cumulated_disp, i,
                                        par_image)

        # draw correction
        if i == rep_correction - 1 or stop_flag:
            r_coordinates_correction = r_coor_confirmed_vol1 + r_disp_from_vol1
            plt.figure(figsize=(16, 4))
            plt.subplot(1, 2, 1)
            tracking_plot(r_coor_prgls, r_coordinates_segment_post,
                          r_coordinates_correction)
            plt.subplot(1, 2, 2)
            tracking_plot_zx(r_coor_prgls, r_coordinates_segment_post,
                             r_coordinates_correction)

            # generate current image of labels (more accurate)
            i_tracked_cells_corrected, i_overlap_corrected = transform_cells_quick(
                par_subregions, i_disp_from_vol1, print_seq=False)

            # re-calculate boundaries by watershed
            i_tracked_cells_corrected[np.where(i_overlap_corrected > 1)] = 0
            for i in np.where(cells_on_boundary_local == 1)[0]:
                i_tracked_cells_corrected[np.where(
                    i_tracked_cells_corrected == (i + 1))] = 0

            z_range = range(par_image["z_scaling"] // 2,
                            par_image["z_siz"] * par_image["z_scaling"],
                            par_image["z_scaling"])
            l_label_T_watershed = watershed_2d_markers(
                i_tracked_cells_corrected[:, :, z_range],
                i_overlap_corrected[:, :, z_range],
                z_range=par_image["z_siz"])

            plt.pause(10)
            print("current volume: t=", volume1)
            plt.figure(figsize=(16, 10))
            plt.subplot(1, 2, 1)
            fig = plt.imshow(cell_t1, cmap="gray")
            plt.subplot(1, 2, 2)
            fig = plt.imshow(seg_t1, cmap="tab20b")

            plt.pause(10)
            print("target volume: t=", volume2)
            plt.figure(figsize=(16, 10))
            plt.subplot(1, 2, 1)
            fig = plt.imshow(
                np.max(image_cell_bg[0, :, :, :, 0], axis=2) > 0.5,
                cmap="gray")
            plt.subplot(1, 2, 2)
            fig = plt.imshow(np.max(l_label_T_watershed, axis=2),
                             cmap="tab20b")
            break

    return None