Пример #1
0
def estimate_eggs_from_info(row_slice, mask_shape):
    """ finds all eggs for particular slice and mask them by ellipse annotated
    by ant, post and lat in the all info table

    :param str path_img:
    :return ndarray: ndarray
    """
    pos_ant, pos_lat, pos_post = tl_visu.parse_annot_rectangles(row_slice)
    list_masks = tl_visu.draw_eggs_rectangle(mask_shape, pos_ant, pos_lat,
                                             pos_post)
    mask_eggs = tl_visu.merge_object_masks(list_masks, thr_overlap=0.5)

    return mask_eggs
def select_optimal_ellipse(idx_row,
                           path_dir_csv,
                           overlap_thr=OVERLAP_THRESHOLD):
    """ reconstruct the user annotation and compare it with estimated ellipses

    :param (int, row) idx_row: index and row with user annotation
    :param str path_dir_csv: path to list of ellipse parameters
    :param float overlap_thr: skip all annot. which Jaccard lower then threshold
    :return {}:
    """
    _, row = idx_row
    dict_row = dict(row)
    path_csv = os.path.join(path_dir_csv, row['image_name'] + '.csv')
    df_ellipses = pd.read_csv(path_csv, index_col=0)

    pos = row[tl_visu.COLUMNS_POSITION_EGG_ANNOT]
    max_size = 2 * max(pos) + min(pos)

    pos_ant = [[row['ant_x'], row['ant_y']]]
    pos_lat = [[row['lat_x'], row['lat_y']]]
    pos_post = [[row['post_x'], row['post_y']]]
    mask_ref = tl_visu.draw_eggs_rectangle((max_size, max_size), pos_ant,
                                           pos_lat, pos_post)[0]

    list_jaccard = []
    for _, ell_row in df_ellipses.iterrows():
        mask_ell = ell_fit.add_overlap_ellipse(np.zeros(mask_ref.shape),
                                               ell_row.values.tolist(), 1)
        union = np.sum(np.logical_and(mask_ref, mask_ell))
        intersect = np.sum(np.logical_or(mask_ref, mask_ell))
        list_jaccard.append(union / float(intersect))

    # if no match with annotation
    if max(list_jaccard) < overlap_thr:
        dict_row['ellipse_Jaccard'] = max(list_jaccard)
        return dict_row

    idx_best = np.argmax(list_jaccard)
    ell_best = dict(df_ellipses.iloc[idx_best])

    # optional swap main axis and rotate by 90 deg
    if ell_best['b'] > ell_best['a']:
        ell_best['a'], ell_best['b'] = ell_best['b'], ell_best['a']
        ell_best['theta'] += np.deg2rad(90)

    ell_best['Jaccard'] = max(list_jaccard)
    # add to each name ellipse prefix
    dict_row.update({'ellipse_' + n: ell_best[n] for n in ell_best})

    return dict_row
def figure_draw_annot_csv(fig, img, row_slice, subfig_size=FIGURE_SIZE):
    """ draw from expert annotation stored in info file

    :param obj fig:
    :param ndarray img: backround image
    :param row_slice: line from info file containing annotation
    :param int subfig_size:
    :return obj:
    """
    if fig is None:
        norm_size = np.array(img.shape[:2]) / float(np.max(img.shape))
        fig, ax = plt.subplots(figsize=(norm_size[::-1] * subfig_size))
        ax.imshow(img[:, :, 0], cmap=plt.cm.Greys_r)
    else:
        ax = fig.gca()

    pos_ant, pos_lat, pos_post = tl_visu.parse_annot_rectangles(row_slice)

    list_masks = tl_visu.draw_eggs_rectangle(img.shape[:2],
                                             pos_ant, pos_lat, pos_post)
    for mask in list_masks:
        ax.contour(mask, colors=COLOR_ANNOT, linewidths=(3,))

    return fig