def create_merge_mask(raw_img, seg1, seg2, drawing_aim):
    global pts, draw_img, draw_mask, draw_ax
   
    offset = 20
    seg1_label = seg1 + offset # make it brighter
    seg1_label[seg1_label==offset]=0
    seg1_label = seg1_label.astype(float) * (255/seg1_label.max())
    seg1_label = np.round(seg1_label)
    seg1_label = seg1_label.astype(np.uint8)

    offset = 25
    seg2_label = seg2 + offset # make it brighter
    seg2_label[seg2_label==offset]=0
    seg2_label = seg2_label.astype(float) * (255/seg2_label.max())
    seg2_label = np.round(seg2_label)
    seg2_label = seg2_label.astype(np.uint8)


    bw = seg1>0
    z_profile = np.zeros((bw.shape[0],),dtype=int)
    for zz in range(bw.shape[0]):
        z_profile[zz] = np.count_nonzero(bw[zz,:,:])
    mid_frame = round(histogram_otsu(z_profile)*bw.shape[0]).astype(int)

    img = np.zeros((2*raw_img.shape[1], 3*raw_img.shape[2], 3),dtype=np.uint8)

    row_index = 0

    for cc in range(3):
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], :raw_img.shape[2], cc]=np.amax(raw_img, axis=0)
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], raw_img.shape[2]:2*raw_img.shape[2], cc]=np.amax(seg1_label, axis=0)
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], 2*raw_img.shape[2]:, cc]=np.amax(seg2_label, axis=0)
    
    row_index = 1
    for cc in range(3):
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], :raw_img.shape[2], cc]=raw_img[mid_frame,:,:]
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], raw_img.shape[2]:2*raw_img.shape[2], cc]=seg1_label[mid_frame,:,:]
        img[row_index*raw_img.shape[1]:(row_index+1)*raw_img.shape[1], 2*raw_img.shape[2]:, cc]=seg2_label[mid_frame,:,:]

    draw_mask = np.zeros((img.shape[0],img.shape[1]),dtype=np.uint8)
    draw_img = img.copy()
    # display the image for good/bad inspection
    fig = plt.figure()
    figManager = plt.get_current_fig_manager() 
    figManager.full_screen_toggle() 
    ax = fig.add_subplot(111)
    ax.set_title('Interface for annotating '+drawing_aim+'. Left: raw, Middle: segmentation v1, Right: segmentation v2. \n' \
        +'Top row: max z projection, Bottom row: middle z slice. \n'\
        +'Please draw in the upper left panel \n'\
        +'Left click to add a vertex; Right click to close the current polygon \n' \
        +'Press D to finish annotating mask, Press Q to quit curation (can resume later)')
    draw_ax = ax.imshow(img)
    cid = fig.canvas.mpl_connect('button_press_event', draw_polygons)
    cid2 = fig.canvas.mpl_connect('key_press_event', quit_mask_drawing)
    plt.show()
    fig.canvas.mpl_disconnect(cid)
    fig.canvas.mpl_disconnect(cid2)
示例#2
0
def create_mask(raw_img, seg):
    global pts, draw_img, draw_mask, draw_ax
    bw = seg>0
    z_profile = np.zeros((bw.shape[0],),dtype=int)
    for zz in range(bw.shape[0]):
        z_profile[zz] = np.count_nonzero(bw[zz,:,:])
    
    mid_frame = histogram_otsu(z_profile)*bw.shape[0]
    print("trying to find the best Z to display ...")
    print(f"the raw image has z profile {z_profile}")
    print(f"find best Z = {mid_frame}")
    mid_frame = int(round(mid_frame))

    offset = 20
    seg_label = seg + offset # make it brighter
    seg_label[seg_label==offset]=0
    seg_label = seg_label.astype(float) * (255/seg_label.max())
    seg_label = np.round(seg_label)
    seg_label = seg_label.astype(np.uint8)

    img = np.zeros((raw_img.shape[1], 3*raw_img.shape[2], 3),dtype=np.uint8)

    for cc in range(3):
        img[:, :raw_img.shape[2], cc]=raw_img[mid_frame,:,:]
        img[:, raw_img.shape[2]:2*raw_img.shape[2], cc]=seg_label[mid_frame,:,:]
        img[:, 2*raw_img.shape[2]:, cc]=np.amax(seg_label, axis=0)

    draw_mask = np.zeros((img.shape[0],img.shape[1]),dtype=np.uint8)
    draw_img = img.copy()
    # display the image for good/bad inspection
    fig = plt.figure()
    figManager = plt.get_current_fig_manager() 
    figManager.full_screen_toggle() 
    ax = fig.add_subplot(111)
    ax.set_title('Interface for annotating excluding mask. \n' \
        +'Left: Middle z slice of raw. Middle: Middle z slice of segmentation. Right: Max z projection of segmentation \n' \
        +'Please draw in the left panel \n' \
        +'Left click to add a vertex; Right click to close the current polygon \n' \
        +'Press D to finish annotating mask, Press Q to quit curation (can resume later)')
    draw_ax = ax.imshow(img)
    cid = fig.canvas.mpl_connect('button_press_event', draw_polygons)
    cid2 = fig.canvas.mpl_connect('key_press_event', quit_mask_drawing)
    plt.show()
    fig.canvas.mpl_disconnect(cid)
    fig.canvas.mpl_disconnect(cid2)
def gt_sorting(raw_img, seg):
    global button

    bw = seg > 0
    z_profile = np.zeros((bw.shape[0], ), dtype=int)
    for zz in range(bw.shape[0]):
        z_profile[zz] = np.count_nonzero(bw[zz, :, :])
    mid_frame = round(histogram_otsu(z_profile) * bw.shape[0]).astype(int)

    #create 2x4 mosaic
    out = np.zeros((2 * raw_img.shape[1], 4 * raw_img.shape[2], 3),
                   dtype=np.uint8)
    row_index = 0
    im = raw_img

    for cc in range(3):
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            0 * raw_img.shape[2]:1 * raw_img.shape[2],
            cc] = im[mid_frame - 4, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            1 * raw_img.shape[2]:2 * raw_img.shape[2],
            cc] = im[mid_frame, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            2 * raw_img.shape[2]:3 * raw_img.shape[2],
            cc] = im[mid_frame + 4, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            3 * raw_img.shape[2]:4 * raw_img.shape[2], cc] = np.amax(im,
                                                                     axis=0)

    row_index = 1
    offset = 20
    im = seg + offset  # make it brighter
    im[im == offset] = 0
    im = im.astype(float) * (255 / im.max())
    im = np.round(im)
    im = im.astype(np.uint8)
    for cc in range(3):
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            0 * raw_img.shape[2]:1 * raw_img.shape[2],
            cc] = im[mid_frame - 4, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            1 * raw_img.shape[2]:2 * raw_img.shape[2],
            cc] = im[mid_frame, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            2 * raw_img.shape[2]:3 * raw_img.shape[2],
            cc] = im[mid_frame + 4, :, :]
        out[row_index * raw_img.shape[1]:(row_index + 1) * raw_img.shape[1],
            3 * raw_img.shape[2]:4 * raw_img.shape[2], cc] = np.amax(im,
                                                                     axis=0)

    # display the image for good/bad inspection
    fig = plt.figure()
    figManager = plt.get_current_fig_manager()
    figManager.full_screen_toggle()
    ax = fig.add_subplot(111)
    ax.imshow(out)
    ax.set_title(
        'Interface for Sorting. Left click = BAD. Right click = GOOD \n' +
        'Columns left to right: 4 z slice below middle z slice, middle z slice, 4 z slice above middle z slice, max z projection'
        + 'Top row: raw image; bottom row: segmentation. \n ')
    #plt.tight_layout()
    cid = fig.canvas.mpl_connect('button_press_event', gt_sorting_callback)
    plt.show()
    fig.canvas.mpl_disconnect(cid)

    score = 0
    if button == 3:
        score = 1

    button = 0
    return score