src/create_rois.py
def cluster_regions(regions):

    '''
    This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas. By defining merge 
    priorities a priori instead of iteratively, we get really good quality ROIs that can even overlap without becoming a single
    huge bounding box.

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    results (list): also tuples of the form (y1,x1,y2,x2) (but fewer of them) representing the ROI bounding boxes, but merged
                                    
    '''
    import networkx as nx
    willmerge = []
    mergee = []
    results = []

    # we do a pass through all regions and generate two new lists: a boolean saying whether that region needs merging
    # (i.e. it intersects with another one) and an integer one with the index of the region that should be merged with
    # that one
    for region in regions:
        intersection = check_intersections(region,regions)
        if intersection != -1:
            willmerge.append(True)
            mergee.append(intersection)
            
    # if a region doesn't have intersections, go ahead and add it to the results list        
        else:
            willmerge.append(False)
            mergee.append(intersection)
            results.append(region)
    
    # create a graph and add an edge for each combination of regions that need to be merged.
    G = nx.Graph()
    edges = []
    count = 0
    for roi in range(len(mergee)):
        if willmerge[count] == True:
            edges.append((count,mergee[count]))
        count = count + 1
    G.add_edges_from(edges)


    # the resulting graph will have one connected component per final ROI, and the final ROI is the bounding box
    # around all ROIs in this connected component
    for a in nx.connected_components(G):
        roi = merge_cluster(list(a), regions)
        results.append(roi)
    
    return results

    This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas. By defining merge 
    priorities a priori instead of iteratively, we get really good quality ROIs that can even overlap without becoming a single
    huge bounding box.

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    results (list): also tuples of the form (y1,x1,y2,x2) (but fewer of them) representing the ROI bounding boxes, but merged
                                    
    
This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas. By defining merge
priorities a priori instead of iteratively, we get really good quality ROIs that can even overlap without becoming a single
huge bounding box.
This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas . By defining merge priorities a priori instead of iteratively we get really good quality ROIs that can even overlap without becoming a single huge bounding box .
def cluster_regions(regions):
    import networkx as nx
    willmerge = []
    mergee = []
    results = []
    for region in regions:
        intersection = check_intersections(region,regions)
        if intersection != -1:
            willmerge.append(True)
            mergee.append(intersection)
        else:
            willmerge.append(False)
            mergee.append(intersection)
            results.append(region)
    G = nx.Graph()
    edges = []
    count = 0
    for roi in range(len(mergee)):
        if willmerge[count] == True:
            edges.append((count,mergee[count]))
        count = count + 1
    G.add_edges_from(edges)
    for a in nx.connected_components(G):
        roi = merge_cluster(list(a), regions)
        results.append(roi)
    return results
def cluster_regions ( regions ) : import networkx as nx willmerge = [ ] mergee = [ ] results = [ ] for region in regions : intersection = check_intersections ( region , regions ) if intersection != - 1 : willmerge . append ( True ) mergee . append ( intersection ) else : willmerge . append ( False ) mergee . append ( intersection ) results . append ( region ) G = nx . Graph ( ) edges = [ ] count = 0 for roi in range ( len ( mergee ) ) : if willmerge [ count ] == True : edges . append ( ( count , mergee [ count ] ) ) count = count + 1 G . add_edges_from ( edges ) for a in nx . connected_components ( G ) : roi = merge_cluster ( list ( a ) , regions ) results . append ( roi ) return results
This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas.
This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas .
'''
    This function does some clever graph stuff to merge ROIs hierarchically based on intersection areas. By defining merge 
    priorities a priori instead of iteratively, we get really good quality ROIs that can even overlap without becoming a single
    huge bounding box.

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    results (list): also tuples of the form (y1,x1,y2,x2) (but fewer of them) representing the ROI bounding boxes, but merged
                                    
    ''' # we do a pass through all regions and generate two new lists: a boolean saying whether that region needs merging # (i.e. it intersects with another one) and an integer one with the index of the region that should be merged with # that one # if a region doesn't have intersections, go ahead and add it to the results list         # create a graph and add an edge for each combination of regions that need to be merged. # the resulting graph will have one connected component per final ROI, and the final ROI is the bounding box # around all ROIs in this connected component
regions
tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
Returns.
tuples of the form ( y1 x1 y2 x2 ) representing the ROI bounding boxes Returns .
results
also tuples of the form (y1,x1,y2,x2) (but fewer of them) representing the ROI bounding boxes, but merged
also tuples of the form ( y1 x1 y2 x2 ) ( but fewer of them ) representing the ROI bounding boxes but merged