src/create_rois.py
def create_rois(image, size_thresh, method_thresh, closing, scale_factor):
    '''
    Main entry-point function for generating ROIs automatically. 
    Does thresholding, clever merging of intersecting ROIs and ordering left-right-top-bottom.

            Parameters:
                    image (np.array): 3-dimensional (2d + RGB) numpy array with pixel data for retrieved jpeg from OMERO
                    size_thresh (num): Minimum size (in full-resolution pixels) for an ROI to be considered an ROI
                    method_thresh (str): Thresholding method. Current options are 'otsu', 'triangle', 'yen' and 'li'.
                    closing (int): radius for the diamond-shaped structuring element used for closing operation.
                    scale_factor (int): scaling that was used to generate the downsampled image. Used to re-scale minimum size threshold.

            Returns:
                    regions (list): list of pruned, ordered tuples of the form (y1,x1,y2,x2) representing the ROIs to be saved back to OMERO.
    '''
    from skimage.color import rgb2gray
    from skimage.filters import threshold_otsu, threshold_triangle, threshold_yen, threshold_li
    from skimage.util import invert
    from skimage.morphology import diamond, binary_closing
    from skimage.measure import regionprops, label
    import numpy as np


    # we're assuming the image is RGB and dark features on light background
    im = rgb2gray(image)
    im = invert(im)
    
    # ugly thresholding choice here - I'm assuming the inputs to be well-behaved
    if method_thresh == 'otsu':
        im_thresh = im > threshold_otsu(im)
    elif method_thresh == 'triangle':
        im_thresh = im > threshold_triangle(im)
    elif method_thresh == 'yen':
        im_thresh = im > threshold_yen(im)
    elif method_thresh == 'li':
        im_thresh = im > threshold_li(im)

    # do a bit of closing to already merge regions that are almost touching
    # how much? up to you, it's an input parameter
    im_thresh = binary_closing(im_thresh, diamond(closing))
    im_lab = label(im_thresh)

    # get rid of ROIs smaller than required size threshold
    for i in range(1, im_lab.max()+1):
        coords = np.where(im_lab == i)
        if len(coords[0]) < (size_thresh / (scale_factor ** 2)):
            im_lab[coords] = 0
      
    regionproperties = regionprops(im_lab)
    regions = []

    # at least for now we only care about the bounding boxes for ROIs
    for r in regionproperties:
        regions.append(r.bbox)
    regions = prune_regions(regions)
    regions = order_regions(regions)
    #return []
    return regions

    Main entry-point function for generating ROIs automatically. 
    Does thresholding, clever merging of intersecting ROIs and ordering left-right-top-bottom.

            Parameters:
                    image (np.array): 3-dimensional (2d + RGB) numpy array with pixel data for retrieved jpeg from OMERO
                    size_thresh (num): Minimum size (in full-resolution pixels) for an ROI to be considered an ROI
                    method_thresh (str): Thresholding method. Current options are 'otsu', 'triangle', 'yen' and 'li'.
                    closing (int): radius for the diamond-shaped structuring element used for closing operation.
                    scale_factor (int): scaling that was used to generate the downsampled image. Used to re-scale minimum size threshold.

            Returns:
                    regions (list): list of pruned, ordered tuples of the form (y1,x1,y2,x2) representing the ROIs to be saved back to OMERO.
    
Main entry-point function for generating ROIs automatically.
Does thresholding, clever merging of intersecting ROIs and ordering left-right-top-bottom.

image (np.array): 3-dimensional (2d + RGB) numpy array with pixel data for retrieved jpeg from OMERO
size_thresh (num): Minimum size (in full-resolution pixels) for an ROI to be considered an ROI
method_thresh (str): Thresholding method.

regions (list): list of pruned, ordered tuples of the form (y1,x1,y2,x2) representing the ROIs to be saved back to OMERO.
Main entry - point function for generating ROIs automatically . Does thresholding clever merging of intersecting ROIs and ordering left - right - top - bottom . image ( np . array ) : 3 - dimensional ( 2d + RGB ) numpy array with pixel data for retrieved jpeg from OMERO size_thresh ( num ) : Minimum size ( in full - resolution pixels ) for an ROI to be considered an ROI method_thresh ( str ) : Thresholding method . regions ( list ) : list of pruned ordered tuples of the form ( y1 x1 y2 x2 ) representing the ROIs to be saved back to OMERO .
def create_rois(image, size_thresh, method_thresh, closing, scale_factor):
    from skimage.color import rgb2gray
    from skimage.filters import threshold_otsu, threshold_triangle, threshold_yen, threshold_li
    from skimage.util import invert
    from skimage.morphology import diamond, binary_closing
    from skimage.measure import regionprops, label
    import numpy as np
    im = rgb2gray(image)
    im = invert(im)
    if method_thresh == 'otsu':
        im_thresh = im > threshold_otsu(im)
    elif method_thresh == 'triangle':
        im_thresh = im > threshold_triangle(im)
    elif method_thresh == 'yen':
        im_thresh = im > threshold_yen(im)
    elif method_thresh == 'li':
        im_thresh = im > threshold_li(im)
    im_thresh = binary_closing(im_thresh, diamond(closing))
    im_lab = label(im_thresh)
    for i in range(1, im_lab.max()+1):
        coords = np.where(im_lab == i)
        if len(coords[0]) < (size_thresh / (scale_factor ** 2)):
            im_lab[coords] = 0
    regionproperties = regionprops(im_lab)
    regions = []
    for r in regionproperties:
        regions.append(r.bbox)
    regions = prune_regions(regions)
    regions = order_regions(regions)
    return regions
def create_rois ( image , size_thresh , method_thresh , closing , scale_factor ) : from skimage . color import rgb2gray from skimage . filters import threshold_otsu , threshold_triangle , threshold_yen , threshold_li from skimage . util import invert from skimage . morphology import diamond , binary_closing from skimage . measure import regionprops , label import numpy as np im = rgb2gray ( image ) im = invert ( im ) if method_thresh == 'otsu' : im_thresh = im > threshold_otsu ( im ) elif method_thresh == 'triangle' : im_thresh = im > threshold_triangle ( im ) elif method_thresh == 'yen' : im_thresh = im > threshold_yen ( im ) elif method_thresh == 'li' : im_thresh = im > threshold_li ( im ) im_thresh = binary_closing ( im_thresh , diamond ( closing ) ) im_lab = label ( im_thresh ) for i in range ( 1 , im_lab . max ( ) + 1 ) : coords = np . where ( im_lab == i ) if len ( coords [ 0 ] ) < ( size_thresh / ( scale_factor ** 2 ) ) : im_lab [ coords ] = 0 regionproperties = regionprops ( im_lab ) regions = [ ] for r in regionproperties : regions . append ( r . bbox ) regions = prune_regions ( regions ) regions = order_regions ( regions ) return regions
Main entry-point function for generating ROIs automatically.
Main entry - point function for generating ROIs automatically .
'''
    Main entry-point function for generating ROIs automatically. 
    Does thresholding, clever merging of intersecting ROIs and ordering left-right-top-bottom.

            Parameters:
                    image (np.array): 3-dimensional (2d + RGB) numpy array with pixel data for retrieved jpeg from OMERO
                    size_thresh (num): Minimum size (in full-resolution pixels) for an ROI to be considered an ROI
                    method_thresh (str): Thresholding method. Current options are 'otsu', 'triangle', 'yen' and 'li'.
                    closing (int): radius for the diamond-shaped structuring element used for closing operation.
                    scale_factor (int): scaling that was used to generate the downsampled image. Used to re-scale minimum size threshold.

            Returns:
                    regions (list): list of pruned, ordered tuples of the form (y1,x1,y2,x2) representing the ROIs to be saved back to OMERO.
    ''' # we're assuming the image is RGB and dark features on light background # ugly thresholding choice here - I'm assuming the inputs to be well-behaved # do a bit of closing to already merge regions that are almost touching # how much? up to you, it's an input parameter # get rid of ROIs smaller than required size threshold # at least for now we only care about the bounding boxes for ROIs #return []
image
size_thresh
method_thresh
closing
scale_factor