Exemplo n.º 1
0
def threshold_test(data, input_params):
    img = data.nuc_img

    z_size = img.shape[0]

    med_img = np.zeros(shape=img.shape)
    for z in range(z_size):
        temp_img = img[z, :, :]
        med_img[z, :, :] = cv2.medianBlur(temp_img, ksize=5)

    med_img = img_as_float(med_img)

    ## THRESHOLD TEST
    z = int(med_img.shape[0] / 2)

    fig, ax = filters.try_all_threshold(med_img[z, :, :],
                                        figsize=(10, 8),
                                        verbose=False)
    # plt.show()

    plt.savefig(os.path.join(input_params.output_path,
                             data.rep_name + 'thresh_test.png'),
                dpi=300)
    plt.close()

    # threshold = filters.threshold_otsu(med_img)
    threshold = filters.threshold_triangle(med_img)
    nuc_mask = med_img >= threshold

    for z in range(z_size):
        nuc_mask[z, :, :] = nd.morphology.binary_fill_holes(nuc_mask[z, :, :])

    nuc_mask = nd.morphology.binary_opening(nuc_mask)

    ### WATERSHED TEST
    labels, _ = nd.label(nuc_mask)

    distance = nd.distance_transform_edt(nuc_mask)

    ##custom faster solution for getting markers
    sure_fg = distance
    sure_fg[sure_fg <= 0.4 * distance.max()] = 0
    sure_fg = sure_fg > 0
    sure_fg = nd.morphology.binary_erosion(sure_fg)

    markers, num_regions = nd.label(sure_fg)
    row, col = optimum_subplots(nuc_mask.shape[0])
    fig, ax = plt.subplots(row, col)

    ax = ax.flatten()

    for idx, a in enumerate(ax):
        if idx < nuc_mask.shape[0]:
            labeled_image = label2rgb(markers[idx, :, :],
                                      image=exposure.equalize_adapthist(
                                          data.nuc_img[idx, :, :]),
                                      alpha=0.3,
                                      bg_label=0,
                                      bg_color=[0, 0, 0])
            a.imshow(labeled_image)
        clear_axis_ticks(a)

    plt.savefig(os.path.join(input_params.output_path,
                             data.rep_name + 'watershed_test.png'),
                dpi=300)
    plt.close()
Exemplo n.º 2
0
def find_watershed_3D(data, vol):
    # vol is the binary nuclear mask
    # sure_bg = nd.morphology.binary_dilation(vol, iterations=3)
    '''
    ##  EROSION TEST
    fig, ax = plt.subplots(3, 3)
    ax = ax.flatten()
    
    size = 21
#     z = 10
#     c = make_struct_element(size, shape='circle').astype(vol.dtype)
#     print(c)
#     print(c.dtype)
#     e_t = make_struct_element(size, shape='ellipse_tall').astype(vol.dtype)
    e_w = make_struct_element(size, shape='ellipse_wide').astype(vol.dtype)
        
    c_temp1 = nd.morphology.binary_erosion(vol, structure=c, iterations=1)
    c_temp2 = nd.morphology.binary_erosion(vol, structure=c, iterations=3)
    c_temp3 = nd.morphology.binary_erosion(vol, structure=c, iterations=9)
    
    e_t_temp1 = nd.morphology.binary_erosion(vol, structure=e_t, iterations=1)
    e_t_temp2 = nd.morphology.binary_erosion(vol, structure=e_t, iterations=3)
    e_t_temp3 = nd.morphology.binary_erosion(vol, structure=e_t, iterations=9)
    
    e_w_temp1 = nd.morphology.binary_erosion(vol, structure=e_w, iterations=1)
    e_w_temp2 = nd.morphology.binary_erosion(vol, structure=e_w, iterations=3)
    e_w_temp3 = nd.morphology.binary_erosion(vol, structure=e_w, iterations=9)  # money beet
    
    print(f'Image size is {c_temp1.shape}')
    ax[0].imshow(c_temp1[z, :, :], cmap='gray')
    ax[1].imshow(c_temp2[z, :, :], cmap='gray')
    ax[2].imshow(c_temp3[z, :, :], cmap='gray')
    ax[3].imshow(e_t_temp1[z, :, :], cmap='gray')
    ax[4].imshow(e_t_temp2[z, :, :], cmap='gray')
    ax[5].imshow(e_t_temp3[z, :, :], cmap='gray')
    ax[6].imshow(e_w_temp1[z, :, :], cmap='gray')
    ax[7].imshow(e_w_temp2[z, :, :], cmap='gray')
    ax[8].imshow(e_w_temp3[z, :, :], cmap='gray')
    
    for a in ax:
        clear_axis_ticks(a)
    
    plt.tight_layout()    
    plt.show()
    input('Press enter to continue')
    sys.exit(0)
    '''

    labels, _ = nd.label(vol)

    distance = nd.distance_transform_edt(vol)

    ##custom faster solution for getting markers
    sure_fg = distance
    sure_fg[sure_fg <= 0.2 * distance.max()] = 0
    sure_fg = sure_fg > 0

    struct_size = 19
    e_w = make_struct_element(struct_size,
                              shape='ellipse_wide').astype(vol.dtype)
    sure_fg = nd.morphology.binary_erosion(sure_fg,
                                           structure=e_w,
                                           iterations=5)
    # sure_fg = nd.morphology.binary_opening(sure_fg, structure=e_w, iterations=15)  # iterations < 1 keeps it going until nothing changes

    # sure_fg = nd.morphology.binary_erosion(sure_fg)

    markers, num_regions = nd.label(sure_fg)
    '''
    ### WATERSHED TEST
    row, col = optimum_subplots(vol.shape[0])
    fig, ax = plt.subplots(row, col)
    
    ax = ax.flatten()
    
    for idx, a in enumerate(ax):
        if idx < vol.shape[0]:
            labeled_image = label2rgb(markers[idx, :, :], image=exposure.equalize_adapthist(data.nuc_img[idx, :, :]),
                                 alpha=0.3, bg_label=0, bg_color=[0, 0, 0])
            a.imshow(labeled_image)
        # ax[1].imshow(sure_bg[15, :, :], cmap='Blues')
#         ax[2].imshow(data.nuc_img[15, :, :], cmap='gray')
#         ax[3].imshow(vol[15, :, :], cmap='gray')
        clear_axis_ticks(a)
    
    plt.show()
    
    input('Press enter to continue')
    plt.close()
    sys.exit(0)
    '''

    # local_maxi = peak_local_max(distance, min_distance=20, indices=False, labels=labels, exclude_border=True)
    # markers = nd.label(local_maxi)[0]

    output = watershed(-distance, markers, mask=vol, watershed_line=True)

    return output
Exemplo n.º 3
0
def threshold_puncta(img, data, input_params, channel):
    z_size = img.shape[0]

    float_img = img_as_float(img)

    # THRESHOLD TEST
    z = int(float_img.shape[0] / 2)
    fig, ax = filters.try_all_threshold(float_img[z, :, :],
                                        figsize=(10, 8),
                                        verbose=False)
    plt.savefig(os.path.join(input_params.output_path, data.rep_name + " " +
                             channel + 'thresh_test.png'),
                dpi=300)
    plt.close()

    if '6h Cis HP1 HCT 002' in data.rep_name:
        threshold = 2125
    elif '12h Cis HP1 HCT 003' in data.rep_name:
        threshold = 2978
    elif '6h Cis HP1 HCT 001' in data.rep_name:
        threshold = 1734
    elif '6h Cis Med1 HCT 001' in data.rep_name:
        threshold = 743
    elif '6h Cis Med1 HCT 002' in data.rep_name:
        threshold = 569
    elif '6h Cis Med1 HCT 003' in data.rep_name:
        threshold = 562
    elif 'IF 6h fib1 Cis HCT116 002' in data.rep_name:
        threshold = 2819
    else:
        threshold = filters.threshold_triangle(float_img)

    mask = float_img >= threshold
    puncta_mask = float_img >= threshold

    fig, ax = plt.subplots(1, 1)
    ax.imshow(exposure.equalize_adapthist(mask[z, :, :]), cmap='gray')
    plt.tight_layout()
    plt.savefig(os.path.join(
        input_params.output_path,
        data.rep_name + " " + channel + 'chosen_thresh_test.png'),
                dpi=300)
    plt.close()

    ## quantify number of regions at chosen threshold
    labels, num_regions_threshold = nd.label(mask)

    distance = nd.distance_transform_edt(mask)

    ##custom faster solution for getting markers
    sure_fg = distance
    sure_fg[sure_fg <= 0.4 * distance.max()] = 0
    sure_fg = sure_fg > 0
    sure_fg = nd.morphology.binary_erosion(sure_fg)

    ## number of regions for fast solution
    markers, num_regions = nd.label(sure_fg)

    ## print number of regions and find fraction of total nuclear area
    print(num_regions_threshold)
    markers_no_nuc = np.where(data.nuc_mask != False, labels, 0)
    puncta = regionprops(markers_no_nuc)
    total_area = []
    for pt in puncta:
        total_area.append(pt.area)

    row, col = optimum_subplots(mask.shape[0])
    fig, ax = plt.subplots(row, col)

    img = img.astype(np.uint16)  # not sure if this is allowed

    ax = ax.flatten()

    for idx, a in enumerate(ax):
        if idx < mask.shape[0]:
            labeled_image = label2rgb(markers[idx, :, :],
                                      image=exposure.equalize_adapthist(
                                          img[idx, :, :]),
                                      alpha=0.3,
                                      bg_label=0,
                                      bg_color=[0, 0, 0])
            a.imshow(labeled_image)
        clear_axis_ticks(a)

    plt.savefig(os.path.join(
        input_params.output_path,
        data.rep_name + " " + channel + 'watershed_test.png'),
                dpi=300)
    plt.close()

    return labels, num_regions, puncta, puncta_mask