Beispiel #1
0
def just_root(root_fname, ddao, mat_fname):
    """Create image that shows just the pixel intensity along
    the roots.

    Arguments:
    root_fname -- Image with root mask.
    ddao       -- Image of DDAO stain.
    """
    plt.rcParams.update({'font.size': 24})
    plt.rcParams["figure.figsize"] = (12, 9)
    # overlay organic material (only for root 1)
    om = mod.get_vals(mat_fname, w=1, max_val=None)
    om = om.T
    just_om = np.uint8(om)
    just_om = cv2.Canny(just_om, 0, 1)
    root_mask = mod.create_root_mask(root_fname)
    just_root = np.zeros([1870, 880])
    for i in range(len(ddao)):
        for j in range(len(ddao[i])):
            if root_mask[i][j] == 0:
                just_root[i][j] = ddao[i][j]
            else:
                just_root[i][j] = 0
    just_om = np.ma.masked_where(just_om == 0, just_om)
    just_root_mask = np.ma.masked_where(just_root == 0, just_root)
    plt.imshow(just_root_mask, cmap=c)
    clbar = plt.colorbar()
    plt.clim(0.0, 0.85)
    clbar.set_ticks([0.0, 0.20, 0.40, 0.60, 0.80])
    #     plt.imshow(just_om)
    plt.axis('off')
    return just_root
Beispiel #2
0
def twoD_hist_mat(root_fname, mat_fname, just_root, ddao):
    """Generate a 2D histogram of pixel intensity and distance from
    material.

    Arguments:
    root_fname -- Image with root mask.
    mat_fname  -- Image with material mask.
    just_root  -- Output from just_root().
    ddao       -- DDAO stain image.
    """
    plt.rcParams.update({'font.size': 24})
    plt.rcParams["figure.figsize"] = (12, 7)
    # calculate distance from material to any pixel
    mat = mod.mat_mask(mat_fname)
    dist_m = mod._calc_dist(mat)
    # translate this to only distance to the root or soil
    dist = np.zeros([mat.shape[0], mat.shape[1]])
    pix_int = np.zeros([mat.shape[0], mat.shape[1]])
    root_mask = mod.create_root_mask(root_fname)
    for i in range(len(root_mask)):
        for j in range(len(root_mask[i])):
            # == 0 for looking at distance from material to
            # roots, == 1 for looking at distance from
            # material to soil
            if root_mask[i][j] == 1:
                dist[i][j] = dist_m[i][j]
                pix_int[i][j] = ddao[i][j]
            else:
                dist[i][j] = 0
                pix_int[i][j] = 0
    # create the 2D histogram
    x = dist.flatten()
    y = pix_int.flatten()
    plot_x = []
    plot_y = []
    for k in range(len(x)):
        if x[k] != 0 and x[k] < 2.0:
            plot_x.append(x[k])
            plot_y.append(y[k])
        else:
            pass
    fig, ax = plt.subplots()
    cax = ax.hist2d(plot_x,
                    plot_y,
                    bins=50,
                    range=[[0, 2], [0, 1.0]],
                    normed=True,
                    cmap=c)
    plt.locator_params(axis='x', nbins=5)
    cax[3].set_clim(vmin=0, vmax=2.5)
    cbar = fig.colorbar(cax[3], cmap=c, ticks=[0, 2.5])
    cbar.ax.set_yticklabels(['low', 'high'])
    plt.show()
    return plot_x, plot_y
Beispiel #3
0
def img_dist_plot(mask, overlay):
    """Takes in image of root mask or material mask and
    returns a plot of how far every pixel in the image is
    from the nearest root/material.

    Arguments:
    mask    -- image with masks
    overlay -- image with mask of what you want to impose
    over the plot
    """
    plt.rcParams.update({'font.size': 24})
    plt.rcParams["figure.figsize"] = (12, 10)
    # use if overlay is material
    #     overlay2 = mod.mat_mask(mask)
    #     just_overlay = np.uint8(overlay2)
    #     just_overlay = cv2.Canny(just_overlay, 0, 1)
    #     just_overlay = np.ma.masked_where(just_overlay == 0, just_overlay)
    # create the root mask, 1=not root 0=root
    #     root_mask = mod.create_root_mask(mask)
    mat_mask = mod.mat_mask(mask)
    # calculate distance of each pixel from closest root/material
    distance = mod._calc_dist(mat_mask)
    plt.imshow(distance, cmap=c)
    # use if overlay is roots
    root = mod.create_root_mask(overlay)
    root = 1 - root
    just_root_mask = np.ma.masked_where(root == 0, root)
    plt.xticks([0, 300, 600, 800, 1100, 1400, 1700],
               ("0", "15", "30", "0", "15", "30", "45"))
    plt.yticks([0, 200], ("0", "10"))
    clbar = plt.colorbar()
    #     plt.clim(0, 20)
    #     clbar.set_ticks([0, 5, 10, 15, 20])
    plt.imshow(just_root_mask, cmap="Greys")
    #     plt.imshow(just_overlay, cmap="Greys")
    #     clbar.set_label('Distance from root (mm)')
    # turn off axis unless trying to scale
    plt.axis("off")
    plt.show()
Beispiel #4
0
def avg_near_single_root(root_fname, om_fname, ddao):
    """Create image of average pixel intensity alongside a single root
    with organic material displayed.

    Arguments:
    root_fname -- image with root mask
    om_fname   -- image with organic material mask
    ddao       -- image of ddao stain
    """
    plt.rcParams.update({'font.size': 24})
    plt.rcParams["figure.figsize"] = (12, 9)
    root_mask = mod.create_root_mask(root_fname)
    # detect organic material and display it in the image
    om = mod.get_vals(om_fname, w=1, max_val=None)
    om = om.T
    just_om = np.uint8(om)
    just_om = cv2.Canny(just_om, 0, 1)
    mod.show(just_om, cmap="Greys")
    # create mask of organic material so you don't include it
    # in calculation of average pixel intensity (only want soil)
    om_mask = cv2.imread(om_fname)
    om_mask = 1 - np.array(cv2.cvtColor(om_mask, cv2.COLOR_BGR2GRAY)) / 255.
    # get pixel intensity everywhere except root
    new_array = root_mask * ddao
    # calculate average pixel intensity for pixels on either side
    #  of a single root
    count = -1
    # only go out 200 pixels from either side of the root
    to_plot = np.zeros([root_mask.shape[0], 40])
    no_added_values = []
    for i in range(len(new_array)):
        root = []
        for j in range(len(new_array[i])):
            pixel = new_array[i][j]
            org = om_mask[i][j]
            # keep track of where the root is
            if pixel == 0:
                root.append(j)
            else:
                pass
            if org == 1:
                new_array[i][j] = 0
        # wherever there is root, look on either side and average
        # the pixel intensities together
        if root != []:
            count += 1
            # find center pixel of root
            root_point = root[int(len(root) / 2)]
            if (root[0] - 41) <= 0:
                start = 0
            else:
                start = (root[0] - 41)
            begin = new_array[i][start:root[0] - 1]
            if (root[-1] + 21) >= len(new_array[i] - 1):
                stop = len(new_array[i]) - 1
            else:
                stop = root[-1] + 41
            end = new_array[i][(root[-1] + 1):stop]
            smaller_new = []
            for k in range(len(begin)):
                b = begin[len(begin) - k - 1]
                if k >= (len(end) - 1) and b != 0:
                    smaller_new.append(b)
                else:
                    e = end[k]
                    if b != 0 and e != 0:
                        avg_pix = (b + e) / 2
                    elif b != 0 and e == 0:
                        avg_pix = b
                    elif b == 0 and e != 0:
                        avg_pix = e
                    smaller_new.append(avg_pix)
            # add extra pixels as placeholders if area around root doesn't
            # extend 200 pixels out
            if len(smaller_new) < 40:
                to_add = 40 - len(smaller_new)
                for l in range(to_add):
                    smaller_new.append(0)
        else:
            count += 1
            smaller_new = []
            for d in range(40):
                smaller_new.append(0)
        for k in range(len(smaller_new)):
            to_plot[count][k] = smaller_new[k]
    mod.show(to_plot, cmap=c)
    clbar = plt.colorbar()
    plt.clim(0.0, 0.85)
    clbar.set_ticks([0.0, 0.20, 0.40, 0.60, 0.80])
    return to_plot
Beispiel #5
0
def avg_near_multiple_root(root_fname, q_fname, ddao):
    """Create image of average pixel intensity alongside multiple roots
    with quartz displayed.

    Arguments:
    root_fname -- image with root mask
    q_fname    -- image with quartz material mask
    ddao       -- image of ddao stain
    """
    plt.rcParams.update({'font.size': 24})
    plt.rcParams["figure.figsize"] = (12, 9)
    root_mask = mod.create_root_mask(root_fname)
    # put the quartz in the image
    qu = mod.get_vals(q_fname, w=1, max_val=None)
    qu = qu.T
    quartz = np.uint8(qu)
    show_quartz = cv2.Canny(quartz, 0, 1)
    mod.show(show_quartz, cmap="Greys")
    # create mask of quartz so you can avoid it
    q_mask = cv2.imread(q_fname)
    q_mask = 1 - np.array(cv2.cvtColor(q_mask, cv2.COLOR_BGR2GRAY)) / 255.
    # get pixel intensity everywhere except root
    new_array = root_mask * ddao
    count = -1
    to_plot = np.zeros([root_mask.shape[0], 40])
    for i in range(len(new_array)):
        root = []
        for j in range(len(new_array[i])):
            pixel = new_array[i][j]
            q = q_mask[i][j]
            if pixel == 0:
                root.append(j)
            else:
                pass
            if q == 1:
                new_array[i][j] = 0
        sep_root = find_all_roots(root, [])
        sep_roots = [x for x in sep_root if x != []]
        if sep_roots != []:
            count += 1
            averaged = []
            either = [either_side(y, i, new_array) for y in sep_roots]
            either = [s for s in either if len(s) == 40]
            if len(either) == 1:
                averaged = either[0]
            elif len(either) == 2:
                averaged = root_checker(either[0], either[1])
            elif len(either) == 3:
                new_avg = root_checker(either[0], either[1])
                averaged = root_checker(new_avg, either[2])
            elif len(either) == 4:
                new_avg = root_checker(either[0], either[1])
                new_new_avg = root_checker(new_avg, either[2])
                averaged = root_checker(new_new_avg, either[3])
            else:
                new_avg = root_checker(either[0], either[1])
                new_new_avg = root_checker(new_avg, either[2])
                newest_avg = root_checker(new_new_avg, either[3])
                averaged = root_checker(newest_avg, either[4])
        else:
            count += 1
            averaged = []
            for d in range(40):
                averaged.append(0)
        for k in range(len(averaged)):
            to_plot[count][k] = averaged[k]
    mod.show(to_plot, cmap=c)
    clbar = plt.colorbar()
    plt.clim(0.0, 0.85)
    clbar.set_ticks([0.0, 0.20, 0.40, 0.60, 0.80])
    return to_plot