def ROI_plot(reference,region_names,stretch):
    
    #Define parameters for plot presentation
    nobjects = len(region_names) #get number of objects to be drawn

    #Make reference image the base image on which to draw
    image = hv.Image((np.arange(reference.shape[1]), np.arange(reference.shape[0]), reference))
    image.opts(width=int(reference.shape[1]*stretch['width']),
               height=int(reference.shape[0]*stretch['height']),
              invert_yaxis=True,cmap='gray',
              colorbar=True,
               toolbar='above',
              title="Draw Regions: "+', '.join(region_names))

    #Create polygon element on which to draw and connect via stream to PolyDraw drawing tool
    poly = hv.Polygons([])
    poly_stream = streams.PolyDraw(source=poly, drag=True, num_objects=nobjects, show_vertices=True)
    poly.opts(fill_alpha=0.3, active_tools=['poly_draw'])

    def centers(data):
        try:
            x_ls, y_ls = data['xs'], data['ys']
        except TypeError:
            x_ls, y_ls = [], []
        xs = [np.mean(x) for x in x_ls]
        ys = [np.mean(y) for y in y_ls]
        rois = region_names[:len(xs)]
        return hv.Labels((xs, ys, rois))

    dmap = hv.DynamicMap(centers, streams=[poly_stream])
    
    return (image * poly * dmap), poly_stream
def ROI_plot(directory, fnames, file, region_names=None):

    #Get image
    try:
        Image_Current_File = os.path.join(os.path.normpath(directory),
                                          fnames[file])
        img = cv2.imread(Image_Current_File, cv2.IMREAD_GRAYSCALE)
        print(Image_Current_File)
        print('file: {}'.format(file))
    except IndexError:
        print('Max file index exceeded. All images in folder drawn.')
        return None, None, None

    #get number of objects to be drawn
    nobjects = len(region_names) if region_names else 0

    #Make reference image the base image on which to draw
    image_title = "No Regions to Draw" if nobjects == 0 else "Draw Regions: " + ', '.join(
        region_names)
    image = hv.Image((np.arange(img.shape[1]), np.arange(img.shape[0]), img))
    image.opts(width=int(img.shape[1]),
               height=int(img.shape[0]),
               invert_yaxis=True,
               cmap='gray',
               colorbar=True,
               toolbar='below',
               title=image_title)

    #Create polygon element on which to draw and connect via stream to PolyDraw drawing tool
    poly = hv.Polygons([])
    poly_stream = streams.PolyDraw(source=poly,
                                   drag=True,
                                   num_objects=nobjects,
                                   show_vertices=True)
    poly.opts(fill_alpha=0.3, active_tools=['poly_draw'])

    def centers(data):
        try:
            x_ls, y_ls = data['xs'], data['ys']
        except TypeError:
            x_ls, y_ls = [], []
        xs = [np.mean(x) for x in x_ls]
        ys = [np.mean(y) for y in y_ls]
        rois = region_names[:len(xs)]
        return hv.Labels((xs, ys, rois))

    if nobjects > 0:
        dmap = hv.DynamicMap(centers, streams=[poly_stream])
        return (image * poly * dmap), poly_stream, img.shape
    else:
        return (image), None, img.shape
Beispiel #3
0
from random import randint

import holoviews as hv
import panel as pn
from holoviews import opts, streams

LINE = "red"
FILL1 = "green"
FILL2 = "fulvous"
FILL3 = "blue"

hv.extension("bokeh")
path = hv.Path([[(1, 5), (9, 5)]])
poly = hv.Polygons([[(2, 2), (5, 8), (8, 2)]])
path_stream = streams.PolyDraw(source=path, drag=True, show_vertices=True)
poly_stream = streams.PolyDraw(
    source=poly,
    drag=True,
    num_objects=4,
    show_vertices=True,
    styles={"fill_color": [FILL1, FILL2, FILL3]},
)


def get_plot(path, poly):
    return (path * poly).opts(
        opts.Polygons(fill_alpha=0.3, active_tools=["poly_draw"]),
        opts.Path(color=LINE, height=400, line_width=5, width=400),
    )