예제 #1
0
파일: test4.py 프로젝트: BSeppke/vigra
def testGridGraphSegmentationFelzenszwalbSegmentation():
    dataRGB  = numpy.random.random([3,3,3]).astype(numpy.float32)
    dataRGB  = taggedView(dataRGB,'xyc')
    data  = numpy.random.random([3,3]).astype(numpy.float32)
    edata = numpy.random.random([3*2-1,3*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(g0,edata)

    labels = graphs.felzenszwalbSegmentation(graph=g0,edgeWeights=ew,k=1.0,nodeNumStop=5)

    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 5


    
    data  = numpy.random.random([3,3,3]).astype(numpy.float32)
    edata = numpy.random.random([3*2-1,3*2-1,3*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(g0,edata)

    labels = graphs.felzenszwalbSegmentation(graph=g0,edgeWeights=ew,k=1.0,nodeNumStop=15)

    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 15
예제 #2
0
파일: test4.py 프로젝트: saqibns/vigra
def testGridGraphSegmentationFelzenszwalbSegmentation():
    dataRGB = numpy.random.random([3, 3, 3]).astype(numpy.float32)
    dataRGB = taggedView(dataRGB, 'xyc')
    data = numpy.random.random([3, 3]).astype(numpy.float32)
    edata = numpy.random.random([3 * 2 - 1, 3 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(g0, edata)

    labels = graphs.felzenszwalbSegmentation(graph=g0,
                                             edgeWeights=ew,
                                             k=1.0,
                                             nodeNumStop=5)

    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 5

    data = numpy.random.random([3, 3, 3]).astype(numpy.float32)
    edata = numpy.random.random([3 * 2 - 1, 3 * 2 - 1,
                                 3 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(g0, edata)

    labels = graphs.felzenszwalbSegmentation(graph=g0,
                                             edgeWeights=ew,
                                             k=1.0,
                                             nodeNumStop=15)

    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 15
예제 #3
0
파일: test4.py 프로젝트: saqibns/vigra
def testGridGraphWatersheds():

    data = numpy.random.random([10, 10, 10]).astype(numpy.float32)
    edata = numpy.random.random([10 * 2 - 1, 10 * 2 - 1,
                                 10 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0, image=edata)

    # generate seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0, nodeWeights=data)
    # node weighted watershed seeds
    labelsNodeWeightedA = graphs.nodeWeightedWatersheds(graph=g0,
                                                        nodeWeights=data,
                                                        seeds=seeds)
    # node weighted watershed seeds
    labelsNodeWeightedB = graphs.nodeWeightedWatersheds(graph=g0,
                                                        nodeWeights=data)
    # edge weighted watershed seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0, nodeWeights=data)
    labelsEdgeWeighted = graphs.edgeWeightedWatersheds(graph=g0,
                                                       edgeWeights=ew,
                                                       seeds=seeds)

    assert numpy.array_equal(labelsNodeWeightedA, labelsNodeWeightedB)

    data = numpy.random.random([10, 10]).astype(numpy.float32)
    edata = numpy.random.random([10 * 2 - 1, 10 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0, image=edata)

    # generate seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0, nodeWeights=data)
    # node weighted watershed seeds
    labelsNodeWeightedA = graphs.nodeWeightedWatersheds(graph=g0,
                                                        nodeWeights=data,
                                                        seeds=seeds)
    # node weighted watershed seeds
    labelsNodeWeightedB = graphs.nodeWeightedWatersheds(graph=g0,
                                                        nodeWeights=data)
    # edge weighted watershed seeds
    labelsEdgeWeighted = graphs.edgeWeightedWatersheds(graph=g0,
                                                       edgeWeights=ew,
                                                       seeds=seeds)

    assert numpy.array_equal(labelsNodeWeightedA, labelsNodeWeightedB)
예제 #4
0
파일: test4.py 프로젝트: saqibns/vigra
def testGridGraphAgglomerativeClustering():
    dataRGB = numpy.random.random([10, 10, 3]).astype(numpy.float32)
    dataRGB = vigra.taggedView(dataRGB, 'xyc')
    data = numpy.random.random([10, 10]).astype(numpy.float32)
    edata = numpy.random.random([10 * 2 - 1, 10 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0, image=edata)
    #ew = taggedView(ew,'xyz')

    labels = graphs.agglomerativeClustering(graph=g0,
                                            edgeWeights=ew,
                                            nodeFeatures=dataRGB,
                                            nodeNumStop=5)
    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 5

    labels = graphs.agglomerativeClustering(graph=g0,
                                            edgeWeights=ew,
                                            nodeNumStop=5)
    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 5

    dataRGB = numpy.random.random([10, 10, 10, 3]).astype(numpy.float32)
    dataRGB = vigra.taggedView(dataRGB, 'xyzc')
    data = numpy.random.random([10, 10, 10]).astype(numpy.float32)
    edata = numpy.random.random([10 * 2 - 1, 10 * 2 - 1,
                                 10 * 2 - 1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)

    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0, image=edata)
    #ew = taggedView(ew,'xyz')

    labels = graphs.agglomerativeClustering(graph=g0,
                                            edgeWeights=ew,
                                            nodeFeatures=dataRGB,
                                            nodeNumStop=5)
    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 5

    labels = graphs.agglomerativeClustering(graph=g0,
                                            edgeWeights=ew,
                                            nodeNumStop=5)
    g1 = graphs.regionAdjacencyGraph(graph=g0, labels=labels)
    assert g1.nodeNum == 5
def labels_to_dense_gt(labels_path, probs):
    import vigra.graphs as vgraph

    labels = vol_to_vol(labels_path)
    gt = np.zeros_like(labels, dtype=np.uint32)

    offset = 0
    for z in xrange(gt.shape[2]):

        hmap = vigra.filters.gaussianSmoothing(probs[:, :, z], 2.)
        seeds = vigra.analysis.labelImageWithBackground(labels[:, :, z])
        gt[:, :, z], _ = vigra.analysis.watershedsNew(hmap, seeds=seeds)
        gt[:, :, z][gt[:, :, z] != 0] += offset
        offset = gt[:, :, z].max()

    # bring to 0 based indexing
    gt -= 1

    # remove isolated segments
    rag_global = vgraph.regionAdjacencyGraph(vgraph.gridGraph(gt.shape[0:3]),
                                             gt)

    node_to_node = np.concatenate([
        np.arange(rag_global.nodeNum, dtype=np.uint32)[:, None]
        for _ in range(2)
    ],
                                  axis=1)

    for z in xrange(gt.shape[2]):
        rag_local = vgraph.regionAdjacencyGraph(
            vgraph.gridGraph(gt.shape[0:2]), gt[:, :, z])
        for node in rag_local.nodeIter():
            neighbour_nodes = []
            for nnode in rag_local.neighbourNodeIter(node):
                neighbour_nodes.append(nnode)
            if len(neighbour_nodes) == 1:
                node_coordinates = np.where(gt == node.id)
                if not 0 in node_coordinates[0] and not 511 in node_coordinates[
                        0] and not 0 in node_coordinates[
                            1] and not 511 in node_coordinates[1]:
                    node_to_node[node.id] = neighbour_nodes[0].id

    gt_cleaned = rag_global.projectLabelsToBaseGraph(node_to_node)[:, :, :, 0]

    return gt_cleaned
예제 #6
0
    def calc_edge_features(self, feature_matrix=None):

        grid_graph = graphs.gridGraph(self._data.shape)

        if feature_matrix is None:
            edge_features = graphs.edgeFeaturesFromImage(grid_graph, self._data)
        else:
            edge_features = graphs.edgeFeaturesFromImage(grid_graph, feature_matrix)

        self._edge_features = self._rag.accumulateEdgeFeatures(edge_features)
예제 #7
0
파일: test4.py 프로젝트: BSeppke/vigra
def testGridGraphAgglomerativeClustering():
    dataRGB  = numpy.random.random([10,10,3]).astype(numpy.float32)
    dataRGB  = vigra.taggedView(dataRGB,'xyc')
    data  = numpy.random.random([10,10]).astype(numpy.float32)
    edata = numpy.random.random([10*2-1,10*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)


    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0,image=edata)
    #ew = taggedView(ew,'xyz')

    
    labels = graphs.agglomerativeClustering(graph=g0,edgeWeights=ew,nodeFeatures=dataRGB,nodeNumStop=5)
    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 5
    
    labels = graphs.agglomerativeClustering(graph=g0,edgeWeights=ew,nodeNumStop=5)
    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 5



    dataRGB  = numpy.random.random([10,10,10,3]).astype(numpy.float32)
    dataRGB  = vigra.taggedView(dataRGB,'xyzc')
    data  = numpy.random.random([10,10,10]).astype(numpy.float32)
    edata = numpy.random.random([10*2-1,10*2-1,10*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)


    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0,image=edata)
    #ew = taggedView(ew,'xyz')

    
    labels = graphs.agglomerativeClustering(graph=g0,edgeWeights=ew,nodeFeatures=dataRGB,nodeNumStop=5)
    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 5
    
    labels = graphs.agglomerativeClustering(graph=g0,edgeWeights=ew,nodeNumStop=5)
    g1  = graphs.regionAdjacencyGraph(graph=g0,labels=labels)
    assert g1.nodeNum == 5
예제 #8
0
파일: __init__.py 프로젝트: timoMa/skneuro
def pmapSizeFilter(neuroCCBinaryPmap, minSize):
    """
    keywords:
        neuroCCBinaryPmap:  zero where membranes are
                            one where neurons are

    """
    shape = neuroCCBinaryPmap.shape


    # get the connected components 
    neuroCCMap  = vigra.analysis.labelVolume(neuroCCBinaryPmap)
    print "min max",neuroCCMap.min(), neuroCCMap.max()
    assert neuroCCMap.min() == 1
    neuroCCMap -= 1

    # get region adjacency graph from super-pixel labels
    gridGraph = graphs.gridGraph(shape[0:3])
    rag = graphs.regionAdjacencyGraph(gridGraph, neuroCCMap)

    # get all sizes
    nodeSizes = rag.nodeSize()


    # nodeColoring
    print "neuroCCBinaryPmap",neuroCCBinaryPmap.dtype,neuroCCBinaryPmap.shape
    nodeColor = rag.accumulateNodeFeatures(neuroCCBinaryPmap.astype('float32'))


    # find to small nodes
    toSmallNodesIds = numpy.where(nodeSizes<minSize)[0]
    toSmallNodesSizes = nodeSizes[toSmallNodesIds]
    toSmallNodesColor = nodeColor[toSmallNodesIds]

    sortedIndices = numpy.argsort(toSmallNodesSizes)
    sortedIndices.shape,sortedIndices
    toSmallNodesIds = toSmallNodesIds[sortedIndices]

    # todo (impl degree map as numpy array)

    for nid in toSmallNodesIds:

        neigbours = []
        for n in rag.neighbourNodeIter(rag.nodeFromId(nid)):
            neigbours.append(n)
        if len(neigbours)==1:
            # flip color
            nodeColor[nid] = int(not bool(nodeColor[nid]))

    pixelColor = rag.projectNodeFeaturesToGridGraph(nodeColor)
    return pixelColor,neuroCCBinaryPmap
예제 #9
0
파일: test4.py 프로젝트: BSeppke/vigra
def testGridGraphWatersheds():

    data  = numpy.random.random([10,10,10]).astype(numpy.float32)
    edata = numpy.random.random([10*2-1,10*2-1,10*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)


    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0,image=edata)

    # generate seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0,nodeWeights=data)
    # node weighted watershed seeds
    labelsNodeWeightedA  = graphs.nodeWeightedWatersheds(graph=g0,nodeWeights=data,seeds=seeds)
    # node weighted watershed seeds
    labelsNodeWeightedB  = graphs.nodeWeightedWatersheds(graph=g0,nodeWeights=data)
    # edge weighted watershed seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0,nodeWeights=data)
    labelsEdgeWeighted  = graphs.edgeWeightedWatersheds(graph=g0,edgeWeights=ew,seeds=seeds)

    assert numpy.array_equal(labelsNodeWeightedA,labelsNodeWeightedB)

    data  = numpy.random.random([10,10]).astype(numpy.float32)
    edata = numpy.random.random([10*2-1,10*2-1]).astype(numpy.float32)
    g0 = graphs.gridGraph(data.shape)


    ew = graphs.edgeFeaturesFromInterpolatedImage(graph=g0,image=edata)

    # generate seeds
    seeds = graphs.nodeWeightedWatershedsSeeds(graph=g0,nodeWeights=data)
    # node weighted watershed seeds
    labelsNodeWeightedA  = graphs.nodeWeightedWatersheds(graph=g0,nodeWeights=data,seeds=seeds)
    # node weighted watershed seeds
    labelsNodeWeightedB  = graphs.nodeWeightedWatersheds(graph=g0,nodeWeights=data)
    # edge weighted watershed seeds
    labelsEdgeWeighted  = graphs.edgeWeightedWatersheds(graph=g0,edgeWeights=ew,seeds=seeds)

    assert numpy.array_equal(labelsNodeWeightedA,labelsNodeWeightedB)
예제 #10
0
파일: __init__.py 프로젝트: timoMa/skneuro
def pmapSizeFilter(neuroCCBinaryPmap, minSize):
    """
    keywords:
        neuroCCBinaryPmap:  zero where membranes are
                            one where neurons are

    """
    shape = neuroCCBinaryPmap.shape

    # get the connected components
    neuroCCMap = vigra.analysis.labelVolume(neuroCCBinaryPmap)
    print "min max", neuroCCMap.min(), neuroCCMap.max()
    assert neuroCCMap.min() == 1
    neuroCCMap -= 1

    # get region adjacency graph from super-pixel labels
    gridGraph = graphs.gridGraph(shape[0:3])
    rag = graphs.regionAdjacencyGraph(gridGraph, neuroCCMap)

    # get all sizes
    nodeSizes = rag.nodeSize()

    # nodeColoring
    print "neuroCCBinaryPmap", neuroCCBinaryPmap.dtype, neuroCCBinaryPmap.shape
    nodeColor = rag.accumulateNodeFeatures(neuroCCBinaryPmap.astype('float32'))

    # find to small nodes
    toSmallNodesIds = numpy.where(nodeSizes < minSize)[0]
    toSmallNodesSizes = nodeSizes[toSmallNodesIds]
    toSmallNodesColor = nodeColor[toSmallNodesIds]

    sortedIndices = numpy.argsort(toSmallNodesSizes)
    sortedIndices.shape, sortedIndices
    toSmallNodesIds = toSmallNodesIds[sortedIndices]

    # todo (impl degree map as numpy array)

    for nid in toSmallNodesIds:

        neigbours = []
        for n in rag.neighbourNodeIter(rag.nodeFromId(nid)):
            neigbours.append(n)
        if len(neigbours) == 1:
            # flip color
            nodeColor[nid] = int(not bool(nodeColor[nid]))

    pixelColor = rag.projectNodeFeaturesToGridGraph(nodeColor)
    return pixelColor, neuroCCBinaryPmap
예제 #11
0
def get_segmentation(predict, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1, cleanCloseSeeds=True,
                     returnSeedsOnly=False, edgeLengths=None,nodeFeatures=None, nodeSizes=None, nodeLabels=None,
                     nodeNumStop=None, beta=0, metric='l1', wardness=0.2, out=None):
    """ Get segmentation through watershed and agglomerative clustering
    :param predict: prediction map
    :return: segmentation map
    """
    #use watershed and save superpixels map
    super_pixels = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, returnSeedsOnly)
    # seeds = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, True)
    # save_h5(seeds, "/home/stamylew/delme/seeds.h5", "data")
    print
    print "#Nodes in superpixels", len(np.unique(super_pixels))
    # save_h5(super_pixels, "/home/stamylew/delme/super_pixels.h5", "data")

    #smooth prediction map
    probs = vf.gaussianSmoothing(predict, sigSmooth)
     # save_h5(probs, "/home/stamylew/delme/probs.h5", "data")

    #make grid graph
    grid_graph = vg.gridGraph(super_pixels.shape, False)

    grid_graph_edge_indicator = vg.edgeFeaturesFromImage(grid_graph, probs)
    #make region adjacency graph
    rag = vg.regionAdjacencyGraph(grid_graph, super_pixels)

    #accumulate edge features from grid graph node map
    edge_weights = rag.accumulateEdgeFeatures(grid_graph_edge_indicator)
    edge_weights_tag = "mean of the probabilities"

    #do agglomerative clustering

    labels = vg.agglomerativeClustering(rag, edge_weights, edgeLengths, nodeFeatures, nodeSizes,
            nodeLabels, nodeNumStop, beta, metric, wardness, out)

    #segmentation data
    wsDt_data = np.zeros((8,1))
    wsDt_data[:,0] = (pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth, cleanCloseSeeds, returnSeedsOnly)
    agglCl_data = edge_weights_tag, str(edgeLengths), str(nodeFeatures), str(nodeSizes), str(nodeLabels), str(nodeNumStop), \
                  str(beta), metric, str(wardness), str(out)

    #project labels back to data
    segmentation = rag.projectLabelsToBaseGraph(labels)
    print "#nodes in segmentation", len(np.unique(segmentation))
    # save_h5(segmentation, "/home/stamylew/delme/segmap.h5", "data", None)
    print "seg", np.unique(segmentation)
    return segmentation, super_pixels, wsDt_data, agglCl_data
예제 #12
0
def shortest_paths(indicator, pairs, bounds=None, hfp=None):

    # Crate the grid graph and shortest path objects
    gridgr = graphs.gridGraph(indicator.shape)
    indicator = indicator.astype(np.float32)
    gridgr_edgeind = graphs.edgeFeaturesFromImage(gridgr, indicator)
    instance = graphs.ShortestPathPathDijkstra(gridgr)

    # Initialize paths image
    pathsim = np.zeros(indicator.shape)
    # Initialize list of path coordinates
    paths = []

    for pair in pairs:

        source = pair[0]
        target = pair[1]

        if hfp is not None:
            hfp.logging('Calculating path from {} to {}', source, target)

        targetNode = gridgr.coordinateToNode(target)
        sourceNode = gridgr.coordinateToNode(source)

        instance.run(gridgr_edgeind, sourceNode, target=targetNode)
        path = instance.path(pathType='coordinates')
        if path.any():
            # Do not forget to correct for the offset caused by cropping!
            if bounds is not None:
                paths.append(path + [bounds[0].start, bounds[1].start, bounds[2].start])
            else:
                paths.append(path)

        pathindices = np.swapaxes(path, 0, 1)
        pathsim[pathindices[0], pathindices[1], pathindices[2]] = 1

    return paths, pathsim
예제 #13
0
def shortest_paths(indicator, pairs, n_threads=1):
    """
    This function was copied from processing_lib.py
    :param indicator:
    :return:
    """

    gridgr = graphs.gridGraph(indicator.shape)
    gridgr_edgeind = graphs.implicitMeanEdgeMap(gridgr,
                                                indicator.astype('float32'))

    def single_path(pair, instance=None):
        source = pair[0]
        target = pair[1]
        print 'Calculating path from {} to {}'.format(source, target)
        if instance == None:
            instance = graphs.ShortestPathPathDijkstra(gridgr)

        targetNode = gridgr.coordinateToNode(target)
        sourceNode = gridgr.coordinateToNode(source)

        instance.run(gridgr_edgeind, sourceNode, target=targetNode)
        path = instance.path(pathType='coordinates')
        if path.any():
            return path

    if n_threads > 1:
        print "Multi-threaded w/ n-threads = ", n_threads
        with futures.ThreadPoolExecutor(max_workers=n_threads) as executor:
            tasks = [executor.submit(single_path, pair) for pair in pairs]
            paths = [t.result() for t in tasks]
    else:
        print "Single threaded"
        instance = graphs.ShortestPathPathDijkstra(gridgr)
        paths = [single_path(pair, instance) for pair in pairs]

    return paths
예제 #14
0
    print "write thinned pmap transform"
    vigra.impex.writeHDF5(res, opt['thinnedDistTransformPMap1'], "data")


    skneuro.addHocViewer(grayData, segData)



if False: 
    print "make the graph and save it"

    print "read segmentation"
    seg = vigra.impex.readHDF5(opt['oversegL0'], "data")
    segData.append([seg, "seg"])

    gridGraph = graphs.gridGraph(seg.shape[0:3])
    print "make rag"
    # get region adjacency graph from super-pixel labels
    rag = graphs.regionAdjacencyGraph(gridGraph, seg, isDense=False)


    print "save rag to file"
    rag.writeHDF5(opt['ragL0'],'data')




if False:
    print "accumulate edge weights and save them"

    print "load rag"
def calculate_distances():

    """
    compute distances between color markers instead of existing synapses
    markers of the same color should be in the same neuron
    """

    files_2d = glob.glob(inputdir + d2_pattern)
    files_2d = sorted(files_2d, key=str.lower)

    files_3d = glob.glob(inputdir + d3_pattern)
    files_3d = sorted(files_3d, key=str.lower)

    files_markers = glob.glob(inputdir + marker_pattern)
    files_markers = sorted(files_markers, key=str.lower)

    debug_dirs = glob.glob(debugdir)
    debug_dirs = sorted(debug_dirs, key=str.lower)

    files_raw = glob.glob(inputdir + raw_pattern)
    files_raw = sorted(files_raw, key=str.lower)

    #print files_2d, files_3d, files_markers, debug_dirs, files_raw


    first = 0
    last = 4
    all_distances_same = []
    all_distances_diff = []
    nsamesame = 0
    nsamediff = 0
    ndiffdiff = 0
    ndiffsame = 0

    for f2name, f3name, mname, ddir, rawname in zip(files_2d[first:last], files_3d[first:last],
                                                    files_markers[first:last], debug_dirs[first:last],
                                                    files_raw[first:last]):


        print "processing files:"
        print f2name
        print f3name
        print mname
        print ddir
        print rawname

        tempGraph = Graph()
        edgeIndicators = []
        instances = []

        if debug_images:
            rawim = vigra.readImage(rawname)
            vigra.impex.writeImage(rawim, ddir + "/raw.tiff")

        #print "processing files:", f2name, f3name, mname

        f2 = h5py.File(f2name)
        f3 = h5py.File(f3name)


        if use_2d_only:
            d2 = f2["exported_data"][5, :, :, 0]
            d3 = f2["exported_data"][5, :, :, 2]
        else:
            d2 = f2["exported_data"][..., 0]
            d3 = f3["exported_data"][5, :, :, 2] # 5 because we only want the central slice, there are 11 in total
            d3 = d3.swapaxes(0, 1)

        # print d2.shape, d3.shape

        combined = d2 + d3
        if use_2d_only:
            #convert to float
            combined = combined.astype(numpy.float32)
            combined = combined/255.

        markedNodes = extractMarkedNodes(mname)
        # print
        opUpsample = OpUpsampleByTwo(graph=tempGraph)
        combined = numpy.reshape(combined, combined.shape + (1,) + (1,))

        combined = combined.view(vigra.VigraArray)
        combined.axistags = vigra.defaultAxistags('xytc')
        opUpsample.Input.setValue(combined)
        upsampledMembraneProbs = opUpsample.Output[:].wait()
        # get rid of t
        upsampledMembraneProbs = upsampledMembraneProbs[:, :, 0, :]
        upsampledMembraneProbs = upsampledMembraneProbs.view(vigra.VigraArray)
        upsampledMembraneProbs.axistags = vigra.defaultAxistags('xyc')

        # try to filter
        upsampledSmoothedMembraneProbs = computeDistanceRaw(upsampledMembraneProbs, 1.6, ddir)
        upsampledMembraneProbs = filter_by_size(upsampledSmoothedMembraneProbs, ddir)
        upsampledMembraneProbs = upsampledMembraneProbs.view(vigra.VigraArray)
        upsampledMembraneProbs.axistags = vigra.defaultAxistags('xyc')


        edgeIndicators.append(computeDistanceHessian(upsampledMembraneProbs, 5.0, ddir))
        edgeIndicators.append(upsampledSmoothedMembraneProbs)

        segm = superpixels(combined.squeeze())
        if debug_images:
            vigra.impex.writeImage(segm, ddir + "/superpixels.tiff")

        gridGr = graphs.gridGraph((d2.shape[0], d2.shape[1]))  # !on original pixels

        for iind, indicator in enumerate(edgeIndicators):
            gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGr, indicator)
            instance = vigra.graphs.ShortestPathPathDijkstra(gridGr)
            instances.append(instance)
            distances_same = []
            distances_diff = []
            for color, points in markedNodes.iteritems():
                #going over points of *same* color
                if len(points)>1:
                    print "Processing color", color
                for i in range(len(points)):
                    node = map(long, points[i])
                    sourceNode = gridGr.coordinateToNode(node)
                    instance.run(gridGraphEdgeIndicator, sourceNode, target=None)
                    distances_all = instance.distances()
                    sp_this = segm[node[0], node[1]]
                    for j in range(i + 1, len(points)):
                        # go over points of the same color

                        other_node = map(long, points[j])
                        distances_same.append(distances_all[other_node[0], other_node[1]])

                        sp_other = segm[other_node[0], other_node[1]]
                        if sp_this==sp_other:
                            nsamesame = nsamesame + 1
                            #print "same color in the same superpixel!"
                        else:
                            nsamediff += 1
                        #targetNode = gridGr.coordinateToNode(other_node)
                        #path = instance.run(gridGraphEdgeIndicator, sourceNode).path(pathType='coordinates',
                        #                                                             target=targetNode)
                        #max_on_path = numpy.max(distances_all[path])
                        #min_on_path = numpy.min(distances_all[path])
                        # print max_on_path, min_on_path
                        # print path.shape
                        #print "distance b/w", node, other_node, " = ", distances_all[other_node[0], other_node[1]]

                    for newcolor, newpoints in markedNodes.iteritems():
                        # go over points of other colors
                        if color == newcolor:
                            continue
                        for newi in range(len(newpoints)):
                            other_node = map(long, newpoints[newi])
                            sp_other = segm[other_node[0], other_node[1]]
                            if sp_this==sp_other:
                                ndiffsame += 1
                            else:
                                ndiffdiff += 1
                            distances_diff.append(distances_all[other_node[0], other_node[1]])

                    # highlight the source point in image
                    distances_all[node[0], node[1]] = numpy.max(distances_all)
                    outfile = ddir + "/" + str(node[0]) + "_" + str(node[1]) + "_" + str(iind) + ".tiff"
                    vigra.impex.writeImage(distances_all, outfile)

            while len(all_distances_diff)<len(edgeIndicators):
                all_distances_diff.append([])
                all_distances_same.append([])

            all_distances_diff[iind].extend(distances_diff)
            all_distances_same[iind].extend(distances_same)
            #print "summary for edge indicator:", iind
            #print "points of same color:", distances_same
            #print "points of other colors:", distances_diff
                    # print distances_same

                    # vigra.impex.writeImage(combined, f2name+"_combined.tiff")
                    # vigra.impex.writeImage(d3, f2name+"_synapse.tiff")
                    # vigra.impex.writeImage(d2, f2name+"_membrane.tiff")


    print "same color in the same superpixels:", nsamesame
    print "same color, different superpixels:", nsamediff
    print "diff color, same superpixel:", ndiffsame
    print "diff color, diff superpixels", ndiffdiff

    analyze_distances(all_distances_same, all_distances_diff)
예제 #16
0
from vigra import Timer
iEdgeMap = graphs.implicitMeanEdgeMap

numpy.random.seed(42)

# input
shape = [200, 100, 100]
data = numpy.random.rand(*shape).astype(numpy.float32)
data = vigra.taggedView(data, "xyz")

if False:
    labels = numpy.random.randint(5, size=shape[0] * shape[1] * shape[2])
    labels = labels.reshape(shape).astype(numpy.uint32)
    labels = vigra.analysis.labelVolume(labels)
    adjListGraph = graphs.listGraph()
    gridGraph = graphs.gridGraph(shape)
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)
    rag.writeHDF5("bla.h5", "dset")

else:

    # load the region adjacency graph
    rag = graphs.loadGridRagHDF5("bla.h5", "dset")

print rag.labels.shape, rag.labels.dtype, type(rag.labels)

print "accumulate edge and node features"

edgeCuesMean = rag.accumulateEdgeFeatures(iEdgeMap(rag.baseGraph, data))
edgeCuesMean = numpy.array([edgeCuesMean, edgeCuesMean]).T
def find_shortest_path(ifp, penaltypower, bounds):

    # Modify distancetransform
    #
    # a) Invert: the lowest values (i.e. the lowest penalty for the shortest path detection) should be at the center of
    #    the current process
    ifp.invert_image(ids='curdisttransf')
    #
    # b) Set all values outside the process to infinity
    ifp.filter_values(ifp.amax('curdisttransf'),
                      type='eq',
                      setto=np.inf,
                      ids='curdisttransf',
                      targetids='curdisttransf_inf')
    #
    # c) Increase the value difference between pixels near the boundaries and pixels central within the processes
    #    This increases the likelihood of the paths to follow the center of processes, thus avoiding short-cuts
    ifp.power(penaltypower, ids='curdisttransf_inf')

    indicator = ifp.get_image('curdisttransf_inf')
    gridgr = graphs.gridGraph(ifp.shape('curlabel'))

    ifp.logging('gridgr.shape = {}'.format(gridgr.shape))

    indicator = indicator.astype(np.float32)
    gridgr_edgeind = graphs.edgeFeaturesFromImage(gridgr, indicator)
    instance = graphs.ShortestPathPathDijkstra(gridgr)

    # Get two local maxima
    indices = np.where(ifp.get_image('curlocmax') == 1)
    coords = zip(indices[0], indices[1], indices[2])
    ifp.logging('Local maxima coordinates: {}'.format(coords))

    ifp.set_data_dict({'pathsim': np.zeros(ifp.get_image('curlocmax').shape)},
                      append=True)

    ifp.logging('len(coords) = {}'.format(len(coords)))
    paths = []
    for i in xrange(0, len(coords) - 1):

        for j in xrange(i + 1, len(coords)):

            ifp.logging('---')
            ifp.logging('i = {0}; j = {1}'.format(i, j))

            source = coords[i]
            target = coords[j]

            targetNode = gridgr.coordinateToNode(target)
            sourceNode = gridgr.coordinateToNode(source)

            ifp.logging('Source = {}'.format(source))
            ifp.logging('Target = {}'.format(target))

            instance.run(gridgr_edgeind, sourceNode, target=targetNode)
            path = instance.path(pathType='coordinates')
            # Do not forget to correct for the offset caused by cropping!
            paths.append(path + [bounds[0][0], bounds[1][0], bounds[2][0]])

            # for coords in path:
            #     # ifp.logging('coords = {}'.format(coords))
            #     pass

            pathindices = np.swapaxes(path, 0, 1)
            ifp.get_image('pathsim')[pathindices[0], pathindices[1],
                                     pathindices[2]] = 1

    # ifp.concatenate('disttransf', 'paths', target='paths_over_dist')
    ifp.astype(np.uint8, ('pathsim', 'curlocmax'))
    # ifp.anytask(vigra.filters.multiBinaryDilation, ('paths', 'locmax'), 3)
    ifp.swapaxes(0, 2, ids=('pathsim', 'curlocmax', 'curdisttransf'))
    ifp.anytask(vigra.filters.discDilation, 2, ids=('pathsim', 'curlocmax'))
    ifp.set_data_dict(
        {
            'paths_over_dist':
            np.array([
                ifp.get_image('pathsim'),
                ifp.get_image('curlocmax'),
                ifp.get_image('curdisttransf')
            ])
        },
        append=True)

    return paths
예제 #18
0
파일: rag3d.py 프로젝트: ThomasWalter/vigra


numpy.random.seed(42)

# input
shape = [200, 100, 100]
data = numpy.random.rand(*shape).astype(numpy.float32)
data = vigra.taggedView(data,"xyz")

if False:
    labels = numpy.random.randint(5, size=shape[0]*shape[1]*shape[2])
    labels = labels.reshape(shape).astype(numpy.uint32)
    labels = vigra.analysis.labelVolume(labels)
    adjListGraph  = graphs.listGraph()
    gridGraph = graphs.gridGraph(shape)
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)
    rag.writeHDF5("bla.h5", "dset")

else :

    # load the region adjacency graph
    rag = graphs.loadGridRagHDF5("bla.h5","dset")

print rag.labels.shape, rag.labels.dtype ,type(rag.labels)


print "accumulate edge and node features"


edgeCuesMean = rag.accumulateEdgeFeatures( iEdgeMap(rag.baseGraph, data) )
def eccentricity( img, label, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ):

    img = img.astype(numpy.uint8)
    dim = len(img.shape)

    ## Enlarge image by one pixel on each side
    bigShape = []
    for s in img.shape:
        bigShape.append(s + 2)
    bigImg = numpy.ones(bigShape)
    slices = []
    for i in range(dim):
        slices.append(slice(1, bigImg.shape[i]-1))
    bigImg[ slices ] = img
    inside = numpy.where(bigImg==0)
    outside = numpy.where(bigImg==1)

    # ## Apply distanceTransform and modify (outside: high values, inside: low values)
    # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32))
    # if showPathImage:
    #     imgp = distImage.copy()
    # if distFunc == "exponential":
    #     distImage = numpy.exp(distImage*-gamma)
    # elif distFunc == "linear":
    #     maxDist = distImage.max()
    #     distImage = maxDist - distImage
    # elif distFunc == "inverse":
    #     w = numpy.where(distImage!=0)
    #     distImage[w] = 1/distImage[w]
    # else:
    #     print "wrong parameters for distFunc in eccentricity"

    ## Distance in the inside between two pixels is 1.0
    distImage = bigImg.copy().astype(numpy.float32)
    distImage[inside]=1.0

    ## Set the outside to a very high value
    distImage[outside]=10000.0

    ## Image copy to draw the paths in
    imgp = distImage.copy()
    imgp[outside] = 100

    ## Get image graph and its path finder
    gridGraph = vigraph.gridGraph(bigImg.shape[0:dim],False)
    graphShape = []
    for s in distImage.shape:
        graphShape.append(s*2-1)
    edgeWeights = vigra.resize(distImage, graphShape, order=0)
    edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph, edgeWeights)
    pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

    ## Find borders in img
    if dim == 2:
        bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8))
    else:
        bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8))
    rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg)
    node = vigraph.GridRegionAdjacencyGraph.nodeFromId(rag, long( bigLblImg[inside][0] ))
    edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node)

    # ## Remove duplicates
    # edges_a = numpy.ascontiguousarray(edges)
    # unique_edges = numpy.unique(edges_a.view([('', edges_a.dtype)]*edges_a.shape[1]))
    # edges = unique_edges.view(edges_a.dtype).reshape((unique_edges.shape[0], edges_a.shape[1]))

    borderImg = numpy.zeros(bigImg.shape)
    for edge in edges:
        slices = []
        for d in range(dim):
            slices.append( slice(edge[d], edge[d]+1) )
        borderImg[slices] = 1

    # ## Find borders in img
    # borderImg = numpy.zeros(bigImg.shape)
    # if dim == 2:
    #     for y in range(bigImg.shape[1]-1):
    #         for x in range(bigImg.shape[0]-1):
    #             if bigImg[x,y] == 0:
    #                 if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1:
    #                     borderImg[x, y] = 1
    #             else:
    #                 if bigImg[x+1,y] == 0:
    #                     borderImg[x+1, y] = 1
    #                 if bigImg[x,y+1] == 0:
    #                     borderImg[x, y+1] = 1
    # else:
    #     for z in range(bigImg.shape[2]-1):
    #         for y in range(bigImg.shape[1]-1):
    #             for x in range(bigImg.shape[0]-1):
    #                 if bigImg[x,y,z] == 0:
    #                     if bigImg[x+1,y,z] == 1 or bigImg[x,y+1,z] == 1 or bigImg[x,y,z+1] == 1:
    #                         borderImg[x, y, z] = 1
    #                 else:
    #                     if bigImg[x+1,y,z] == 0:
    #                         borderImg[x+1,y,z] = 1
    #                     if bigImg[x,y+1,z] == 0:
    #                         borderImg[x,y+1,z] = 1
    #                     if bigImg[x,y,z+1] == 0:
    #                         borderImg[x,y,z+1] = 1

    ## End points for paths (all points on the border)
    targets = numpy.where(borderImg==1)
    nTargets = len(targets[0])

    ## Indices of start points for paths (random)
    nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0))
    numpy.random.seed(42)
    starts = numpy.random.permutation(range(nTargets))[:nPoints]

    ## Compute paths
    maxPaths = []
    maxPathLengths = []
    for i in range(nPoints):
        sourceIndex = []
        for d in range(dim):
            sourceIndex.append(int(targets[d][starts[i]]))
        source = gridGraph.coordinateToNode(sourceIndex)
        pathFinder.run(edgeWeights, source)
        maxPathLength = 0
        for j in range(nTargets):
            targetIndex = []
            for d in range(dim):
                targetIndex.append(int(targets[d][j]))
            target = gridGraph.coordinateToNode(targetIndex)
            path = pathFinder.path(pathType='coordinates', target=target)
            pathLength = pathFinder.distance(target)
            if pathLength > maxPathLength or maxPathLength == 0:
                maxPathLength = pathLength
                maxPath = path
        maxPaths.append(maxPath)
        maxPathLengths.append(maxPathLength)
        imgp[sourceIndex[0], sourceIndex[1]] = 3*maxPathLength*maxPathLength

    if showPathImage:
        val = (imgp.max()+imgp.min())/2
        for p in maxPaths:
            imgp[p[:,0], p[:,1]] = val
    if showPathImage:
        plt.figure(distFunc)
        plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none')
    if len(imgSaveName)>1:

        scipy.misc.imsave(imgSaveName, numpy.swapaxes(imgp, 1, 0))

    return maxPathLengths
def eccentricity( img, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ):

    img = img.astype(numpy.uint8)
    dim = len(img.shape)

    ## Enlarge image by one pixel on each side
    bigShape = []
    for s in img.shape:
        bigShape.append(s + 2)
    bigImg = numpy.ones(bigShape)
    slices = []
    for i in range(dim):
        slices.append(slice(1, bigImg.shape[i]-1))
    bigImg[ slices ] = img
    inside = numpy.where(bigImg==0)
    outside = numpy.where(bigImg==1)


    ## Apply distanceTransform and modify (outside: high values, inside: low values)
    distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32))
    imgp = distImage.copy()
    # if showPathImage:
    #     imgp = distImage.copy()
    if distFunc == "exponential":
        distImage = numpy.exp(distImage*-gamma)
    elif distFunc == "linear":
        maxDist = distImage.max()
        distImage = maxDist - distImage + 10
    elif distFunc == "inverse":
        w = numpy.where(distImage!=0)
        distImage[w] = 1/distImage[w]
    else:
        print "wrong parameters for distFunc in eccentricity"

    ## Distance in the inside between two pixels is 1.0
    #distImage = bigImg.copy().astype(numpy.float32)
    #distImage[inside]=1.0

    # if len(imgSaveName)>1:
    #     plt.imshow(numpy.swapaxes(distImage, 1, 0))
    #     plt.savefig(imgSaveName+"_dist.png")
    #     #scipy.misc.imsave(imgSaveName+"_dist.png", numpy.swapaxes(distImage, 1, 0))

    ## Set the outside to a very high value
    distImage[outside]=1000.0

    ## Image copy to draw the paths in
    #imgp = distImage.copy()
    imgp[outside] = 100

    ## Get image graph and its path finder
    gridGraph = vigraph.gridGraph(bigImg.shape[0:dim],False)
    graphShape = []
    for s in distImage.shape:
        graphShape.append(s*2-1)
    edgeWeights = vigra.resize(distImage, graphShape, order=0)
    #edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, edgeWeights)
    edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph,edgeWeights)
    pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

    ## Find borders in img
    if dim == 2:
        bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8))
    else:
        bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8))
    rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg)
    node = vigraph.GridRegionAdjacencyGraph.nodeFromId(rag, long( bigLblImg[inside][0] ))
    edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node)

    borderImg = numpy.zeros(bigImg.shape)
    for edge in edges:
        slices = []
        for d in range(dim):
            slices.append( slice(edge[d], edge[d]+1) )
        borderImg[slices] = 1

    ## End points for paths (all points on the border)
    targets = numpy.where(borderImg==1)
    nTargets = len(targets[0])

    ## Find the diameter (longest path)
    eccLength = numpy.empty(nTargets)
    eccLength.fill(-1)
    eccTargetPath = {}
    vpIndex = 0
    vpGraphIndex = []
    for d in range(dim):
        vpGraphIndex.append(int(targets[d][vpIndex]))
    vp = gridGraph.coordinateToNode(vpGraphIndex)
    visited = numpy.zeros(nTargets)
    while True:
        visited[vpIndex] = 1
        pathFinder.run(edgeWeights, vp)
        eccChanged = False
        for j in range(nTargets):
            targetIndex = []
            for d in range(dim):
                targetIndex.append(int(targets[d][j]))
            target = gridGraph.coordinateToNode(targetIndex)
            pathLength = pathFinder.distance(target)
            m = max(eccLength[j], pathLength)
            if m > eccLength[j]:
                eccChanged = True
                eccLength[j] = m
                eccTargetPath[j] = pathFinder.path(pathType='coordinates', target=target)
        vpIndex = numpy.argmax(eccLength)
        vpGraphIndex = []
        for d in range(dim):
            vpGraphIndex.append(int(targets[d][vpIndex]))
        vp = gridGraph.coordinateToNode(vpGraphIndex)
        if visited[vpIndex] or not eccChanged:
            break

    ## Find the length of the diameter (non-weighted)
    path = eccTargetPath[vpIndex]
    dMax = 0
    for k in range(1, len(path)):
        diffCount = 0
        for d in range(dim):
            if path[k][d] != path[k-1][d]:
                diffCount += 1
        dMax += numpy.sqrt(diffCount)

    ## Find the midpoint of the diameter
    dMax = dMax/2
    if len(path) == 0:
        path = numpy.empty( (1, 2), numpy.uint8 )
        path[0][0] = targets[0][0]
        path[0][1] = targets[1][0]
    p1 = path[0]
    d1 = 0
    for k in range(1, len(path)):
        p2 = path[k]
        d2 = d1 + numpy.linalg.norm(p2-p1)
        if d2 > dMax:
            if (abs(d2-dMax) < abs(d1-dMax)):
                p1 = p2
            break
        p1 = p2
        d1 = d2

    ## Compute eccentricity from center (p1) to all points on border
    sourceIndex = []
    for d in range(dim):
        sourceIndex.append(int(p1[d]))
    source = gridGraph.coordinateToNode(sourceIndex)
    pathFinder.run(edgeWeights, source)
    maxPathLength = 0
    for j in range(nTargets):
        targetIndex = []
        for d in range(dim):
            targetIndex.append(int(targets[d][j]))
        target = gridGraph.coordinateToNode(targetIndex)
        pathLength = pathFinder.distance(target)
        maxPathLength = max(maxPathLength, pathLength)
        imgp[targetIndex[0], targetIndex[1]] = 40*pathLength

    imgp[ path[:,0], path[:,1] ] = 12*maxPathLength
    imgp[sourceIndex[0], sourceIndex[1]] = 40*maxPathLength
    plt.figure(distFunc)
    plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none')
    if len(imgSaveName)>1:
         scipy.misc.imsave(imgSaveName+".png", numpy.swapaxes(imgp, 1, 0))
예제 #21
0
파일: workspace.py 프로젝트: Erotemic/ibeis
def segmentation_example():
    import vigra
    import opengm
    import sklearn
    import sklearn.mixture
    import numpy as np
    from vigra import graphs
    import matplotlib as mpl
    import plottool as pt

    pt.ensure_pylab_qt4()

    # load image and convert to LAB
    img_fpath = str(ut.grab_test_imgpath(str('lena.png')))
    img = vigra.impex.readImage(img_fpath)
    imgLab = vigra.colors.transform_RGB2Lab(img)

    superpixelDiameter = 15   # super-pixel size
    slicWeight = 15.0        # SLIC color - spatial weight
    labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight,
                                                  superpixelDiameter)
    labels = vigra.analysis.labelImage(labels) - 1

    # get 2D grid graph and RAG
    gridGraph = graphs.gridGraph(img.shape[0:2])
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)

    # Node Features
    nodeFeatures = rag.accumulateNodeFeatures(imgLab)
    nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures)
    nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc")
    nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg)

    nCluster = 5
    g = sklearn.mixture.GMM(n_components=nCluster)
    g.fit(nodeFeatures[:, :])
    clusterProb = g.predict_proba(nodeFeatures)
    # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb
    # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb
    clusterProbImg = rag.projectNodeFeaturesToGridGraph(
        clusterProb.astype(np.float32))
    clusterProbImg = vigra.taggedView(clusterProbImg, "xyc")

    ndim_data = clusterProbImg.reshape((-1, nCluster))
    pca = sklearn.decomposition.PCA(n_components=3)
    print(ndim_data.shape)
    pca.fit(ndim_data)
    print(ut.repr2(pca.explained_variance_ratio_, precision=2))
    oldshape = (clusterProbImg.shape[0:2] + (-1,))
    clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape)
    print(clusterProgImg3.shape)

    # graphical model with as many variables
    # as superpixels, each has 3 states
    gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster)
    # convert probabilites to energies
    probs = np.clip(clusterProb, 0.00001, 0.99999)
    costs = -1.0 * np.log(probs)
    # add ALL unaries AT ONCE
    fids = gm.addFunctions(costs)
    gm.addFactors(fids, np.arange(rag.nodeNum))
    # add a potts function
    beta = 40.0  # strength of potts regularizer
    regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta)
    fid = gm.addFunction(regularizer)
    # get variable indices of adjacent superpixels
    # - or "u" and "v" node id's for edges
    uvIds = rag.uvIds()
    uvIds = np.sort(uvIds, axis=1)
    # add all second order factors at once
    gm.addFactors(fid, uvIds)

    # get super-pixels with slic on LAB image
    Inf = opengm.inference.BeliefPropagation
    parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001)
    inf = Inf(gm, parameter=parameter)

    class PyCallback(object):

        def __init__(self,):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector),))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)

    inf.infer(visitor)

    pt.imshow(clusterProgImg3.swapaxes(0, 1))
    # plot superpixels
    cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3))
    pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap)
    pt.imshow(nodeFeaturesImgRgb)

    cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3))
    for arg in callback.labels:
        arg = vigra.taggedView(arg, "n")
        argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32))
        argImg = vigra.taggedView(argImg, "xy")
        # plot superpixels
        pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
def find_shortest_path(ifp):

    # Modify distancetransform
    #
    # a) Invert: the lowest values (i.e. the lowest penalty for the shortest path detection) should be at the center of
    #    the current process
    ifp.invert_image(ids='curdisttransf')
    #
    # b) Set all values outside the process to infinity
    ifp.filter_values(ifp.amax('curdisttransf'), type='eq', setto=np.inf, ids='curdisttransf', targetids='curdisttransf_inf')
    #
    # c) Increase the value difference between pixels near the boundaries and pixels central within the processes
    #    This increases the likelihood of the paths to follow the center of processes, thus avoiding short-cuts
    ifp.power(10, ids='curdisttransf_inf')

    indicator = ifp.get_image('curdisttransf_inf')
    gridgr = graphs.gridGraph(ifp.shape('curlabel'))

    ifp.logging('gridgr.shape = {}'.format(gridgr.shape))

    indicator = indicator.astype(np.float32)
    gridgr_edgeind = graphs.edgeFeaturesFromImage(gridgr, indicator)
    instance = graphs.ShortestPathPathDijkstra(gridgr)

    # Get two local maxima
    indices = np.where(ifp.get_image('locmax') == 1)
    coords = zip(indices[0], indices[1], indices[2])
    ifp.logging('Local maxima coordinates: {}'.format(coords))

    # ifp.deepcopy_entry('locmax', 'paths')
    ifp.set_data_dict({'paths': np.zeros(ifp.get_image('locmax').shape)}, append=True)

    ifp.logging('len(coords) = {}'.format(len(coords)))
    for i in xrange(0, len(coords)-1):

        for j in xrange(i+1, len(coords)):

            ifp.logging('---')
            ifp.logging('i = {0}; j = {1}'.format(i, j))

            source = coords[i]
            target = coords[j]

            targetNode = gridgr.coordinateToNode(target)
            sourceNode = gridgr.coordinateToNode(source)

            ifp.logging('Source = {}'.format(source))
            ifp.logging('Target = {}'.format(target))

            instance.run(gridgr_edgeind, sourceNode, target=targetNode)
            path = instance.path(pathType='coordinates')

            # for coords in path:
            #     # ifp.logging('coords = {}'.format(coords))
            #     pass

            pathindices = np.swapaxes(path, 0, 1)
            ifp.get_image('paths')[pathindices[0], pathindices[1], pathindices[2]] = 1

    # ifp.concatenate('disttransf', 'paths', target='paths_over_dist')
    ifp.astype(np.uint8, ('paths', 'locmax'))
    # ifp.anytask(vigra.filters.multiBinaryDilation, ('paths', 'locmax'), 3)
    ifp.swapaxes(0, 2, ids=('paths', 'locmax', 'curdisttransf'))
    ifp.anytask(vigra.filters.discDilation, 2, ids=('paths', 'locmax'))
    ifp.set_data_dict({'paths_over_dist': np.array([ifp.get_image('paths'), ifp.get_image('locmax'), ifp.get_image('curdisttransf')])}, append=True)
예제 #23
0
def get_segmentation(predict, pmin=0.5, minMemb=10, minSeg=10, sigMin=6, sigWeights=1, sigSmooth=0.1, cleanCloseSeeds=True,
                     returnSeedsOnly=False, edgeLengths=None,nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=None,
                     beta=0, metric='l1', wardness=0.2, out=None):
    """ Get segmentation through watershed and agglomerative clustering
    :param predict: prediction map
    :return: segmentation map
    """
    #use watershed and save superpixels map
    super_pixels = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, returnSeedsOnly)
    # seeds = wsDtSegmentation(predict, pmin, minMemb, minSeg, sigMin, sigWeights, cleanCloseSeeds, True)
    # save_h5(seeds, "/home/stamylew/delme/seeds.h5", "data")
    print
    print "#Nodes in superpixels", len(np.unique(super_pixels))
    # save_h5(super_pixels, "/home/stamylew/delme/super_pixels.h5", "data")

    #smooth prediction map
    probs = vf.gaussianSmoothing(predict, sigSmooth)
     # save_h5(probs, "/home/stamylew/delme/probs.h5", "data")

    #make grid graph
    grid_graph = vg.gridGraph(super_pixels.shape, False)

    grid_graph_edge_indicator = vg.edgeFeaturesFromImage(grid_graph, probs)
    #make region adjacency graph
    rag = vg.regionAdjacencyGraph(grid_graph, super_pixels)

    #accumulate edge features from grid graph node map
    edge_weights = rag.accumulateEdgeFeatures(grid_graph_edge_indicator)
    edge_weights_tag = "mean of the probabilities"

    #do agglomerative clustering

    def agglomerativeClustering_beststop_th05(graph, edgeWeights=None, edgeStoppers=None,
                                          edgeLengths=None, nodeFeatures=None, nodeSizes=None,
                                          nodeLabels=None, beta=0, metric=None,
                                          wardness=1.0, sameLabelMultiplier=1.0,  out=None):


        vg.ac(graph, edgeWeights=edgeWeights, edgeStoppers=edgeStoppers,
                                       edgeLengths=edgeLengths, nodeFeatures=nodeFeatures,
                                       nodeSizes=nodeSizes, nodeLabels=nodeLabels, nodeNumStop=0,
                                       beta=beta, wardness=wardness, sameLabelMultiplier=sameLabelMultiplier, out=out)

        f = open('/tmp/ac_bestNodenumstop.txt', 'r')
        nodeNumStop = f.readline()
        f.close()
        print "best nodeNumStop:", nodeNumStop

        # in experiments if n of the groundtruth is n the file contains n-1. probably in the hc.hxx the
        #  number is written to file too late. does this make any sense??

        nodeNumStop = int(nodeNumStop)
        nodeNumStop += 1

        ac_res, nodelabel_out_1 = vg.ac(graph, edgeWeights=edgeWeights, edgeStoppers=edgeStoppers,
                                            edgeLengths=edgeLengths, nodeFeatures=nodeFeatures,
                                            nodeSizes=nodeSizes, nodeLabels=nodeLabels, nodeNumStop=nodeNumStop,
                                            beta=beta, wardness=wardness, sameLabelMultiplier=sameLabelMultiplier,
                                            out=out)

        return ac_res, nodeNumStop, nodelabel_out_1

    labels, _, _ = agglomerativeClustering_beststop_th05(rag, edgeWeights=edge_weights, edgeStoppers=edge_weights,
                                          edgeLengths=edgeLengths, nodeFeatures=nodeFeatures, nodeSizes=nodeSizes,
                                          nodeLabels=nodeLabels, beta=beta, metric=metric,
                                          wardness=0.1, sameLabelMultiplier=1.0,  out=out)


    # labels = vg.agglomerativeClustering(rag, edge_weights, edgeLengths, nodeFeatures, nodeSizes,
    #         nodeLabels, nodeNumStop, beta, metric, wardness, out)

    #segmentation data
    wsDt_data = np.zeros((8,1))
    wsDt_data[:,0] = (pmin, minMemb, minSeg, sigMin, sigWeights, sigSmooth, cleanCloseSeeds, returnSeedsOnly)
    agglCl_data = edge_weights_tag, str(edgeLengths), str(nodeFeatures), str(nodeSizes), str(nodeLabels), str(nodeNumStop), str(beta), metric, str(wardness), str(out)

    #project labels back to data
    segmentation = rag.projectLabelsToBaseGraph(labels)
    print "#nodes in segmentation", len(np.unique(segmentation))
    # save_h5(segmentation, "/home/stamylew/delme/segmap.h5", "data", None)

    return segmentation, super_pixels, wsDt_data, agglCl_data
예제 #24
0
def eccentricity(img,
                 label,
                 distFunc="exponential",
                 showPathImage=False,
                 percentageOfPaths=100,
                 imgSaveName=""):

    img = img.astype(numpy.uint8)
    dim = len(img.shape)

    ## Enlarge image by one pixel on each side
    bigShape = []
    for s in img.shape:
        bigShape.append(s + 2)
    bigImg = numpy.ones(bigShape)
    slices = []
    for i in range(dim):
        slices.append(slice(1, bigImg.shape[i] - 1))
    bigImg[slices] = img
    inside = numpy.where(bigImg == 0)
    outside = numpy.where(bigImg == 1)

    # ## Apply distanceTransform and modify (outside: high values, inside: low values)
    # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32))
    # if showPathImage:
    #     imgp = distImage.copy()
    # if distFunc == "exponential":
    #     distImage = numpy.exp(distImage*-gamma)
    # elif distFunc == "linear":
    #     maxDist = distImage.max()
    #     distImage = maxDist - distImage
    # elif distFunc == "inverse":
    #     w = numpy.where(distImage!=0)
    #     distImage[w] = 1/distImage[w]
    # else:
    #     print "wrong parameters for distFunc in eccentricity"

    ## Distance in the inside between two pixels is 1.0
    distImage = bigImg.copy().astype(numpy.float32)
    distImage[inside] = 1.0

    ## Set the outside to a very high value
    distImage[outside] = 10000.0

    ## Image copy to draw the paths in
    imgp = distImage.copy()
    imgp[outside] = 100

    ## Get image graph and its path finder
    gridGraph = vigraph.gridGraph(bigImg.shape[0:dim], False)
    graphShape = []
    for s in distImage.shape:
        graphShape.append(s * 2 - 1)
    edgeWeights = vigra.resize(distImage, graphShape, order=0)
    edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(
        gridGraph, edgeWeights)
    pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

    ## Find borders in img
    if dim == 2:
        bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8))
    else:
        bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8))
    rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg)
    node = vigraph.GridRegionAdjacencyGraph.nodeFromId(
        rag, long(bigLblImg[inside][0]))
    edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges,
                                  bigLblImg, node)

    # ## Remove duplicates
    # edges_a = numpy.ascontiguousarray(edges)
    # unique_edges = numpy.unique(edges_a.view([('', edges_a.dtype)]*edges_a.shape[1]))
    # edges = unique_edges.view(edges_a.dtype).reshape((unique_edges.shape[0], edges_a.shape[1]))

    borderImg = numpy.zeros(bigImg.shape)
    for edge in edges:
        slices = []
        for d in range(dim):
            slices.append(slice(edge[d], edge[d] + 1))
        borderImg[slices] = 1

    # ## Find borders in img
    # borderImg = numpy.zeros(bigImg.shape)
    # if dim == 2:
    #     for y in range(bigImg.shape[1]-1):
    #         for x in range(bigImg.shape[0]-1):
    #             if bigImg[x,y] == 0:
    #                 if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1:
    #                     borderImg[x, y] = 1
    #             else:
    #                 if bigImg[x+1,y] == 0:
    #                     borderImg[x+1, y] = 1
    #                 if bigImg[x,y+1] == 0:
    #                     borderImg[x, y+1] = 1
    # else:
    #     for z in range(bigImg.shape[2]-1):
    #         for y in range(bigImg.shape[1]-1):
    #             for x in range(bigImg.shape[0]-1):
    #                 if bigImg[x,y,z] == 0:
    #                     if bigImg[x+1,y,z] == 1 or bigImg[x,y+1,z] == 1 or bigImg[x,y,z+1] == 1:
    #                         borderImg[x, y, z] = 1
    #                 else:
    #                     if bigImg[x+1,y,z] == 0:
    #                         borderImg[x+1,y,z] = 1
    #                     if bigImg[x,y+1,z] == 0:
    #                         borderImg[x,y+1,z] = 1
    #                     if bigImg[x,y,z+1] == 0:
    #                         borderImg[x,y,z+1] = 1

    ## End points for paths (all points on the border)
    targets = numpy.where(borderImg == 1)
    nTargets = len(targets[0])

    ## Indices of start points for paths (random)
    nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0))
    numpy.random.seed(42)
    starts = numpy.random.permutation(range(nTargets))[:nPoints]

    ## Compute paths
    maxPaths = []
    maxPathLengths = []
    for i in range(nPoints):
        sourceIndex = []
        for d in range(dim):
            sourceIndex.append(int(targets[d][starts[i]]))
        source = gridGraph.coordinateToNode(sourceIndex)
        pathFinder.run(edgeWeights, source)
        maxPathLength = 0
        for j in range(nTargets):
            targetIndex = []
            for d in range(dim):
                targetIndex.append(int(targets[d][j]))
            target = gridGraph.coordinateToNode(targetIndex)
            path = pathFinder.path(pathType='coordinates', target=target)
            pathLength = pathFinder.distance(target)
            if pathLength > maxPathLength or maxPathLength == 0:
                maxPathLength = pathLength
                maxPath = path
        maxPaths.append(maxPath)
        maxPathLengths.append(maxPathLength)
        imgp[sourceIndex[0],
             sourceIndex[1]] = 3 * maxPathLength * maxPathLength

    if showPathImage:
        val = (imgp.max() + imgp.min()) / 2
        for p in maxPaths:
            imgp[p[:, 0], p[:, 1]] = val
    if showPathImage:
        plt.figure(distFunc)
        plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none')
    if len(imgSaveName) > 1:

        scipy.misc.imsave(imgSaveName, numpy.swapaxes(imgp, 1, 0))

    return maxPathLengths
예제 #25
0
def segmentation_example():
    import vigra
    import opengm
    import sklearn
    import sklearn.mixture
    import numpy as np
    from vigra import graphs
    import matplotlib as mpl
    import plottool as pt

    pt.ensure_pylab_qt4()

    # load image and convert to LAB
    img_fpath = str(ut.grab_test_imgpath(str('lena.png')))
    img = vigra.impex.readImage(img_fpath)
    imgLab = vigra.colors.transform_RGB2Lab(img)

    superpixelDiameter = 15  # super-pixel size
    slicWeight = 15.0  # SLIC color - spatial weight
    labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight,
                                                  superpixelDiameter)
    labels = vigra.analysis.labelImage(labels) - 1

    # get 2D grid graph and RAG
    gridGraph = graphs.gridGraph(img.shape[0:2])
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)

    # Node Features
    nodeFeatures = rag.accumulateNodeFeatures(imgLab)
    nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures)
    nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc")
    nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg)

    nCluster = 5
    g = sklearn.mixture.GMM(n_components=nCluster)
    g.fit(nodeFeatures[:, :])
    clusterProb = g.predict_proba(nodeFeatures)
    # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb
    # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb
    clusterProbImg = rag.projectNodeFeaturesToGridGraph(
        clusterProb.astype(np.float32))
    clusterProbImg = vigra.taggedView(clusterProbImg, "xyc")

    ndim_data = clusterProbImg.reshape((-1, nCluster))
    pca = sklearn.decomposition.PCA(n_components=3)
    print(ndim_data.shape)
    pca.fit(ndim_data)
    print(ut.repr2(pca.explained_variance_ratio_, precision=2))
    oldshape = (clusterProbImg.shape[0:2] + (-1, ))
    clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape)
    print(clusterProgImg3.shape)

    # graphical model with as many variables
    # as superpixels, each has 3 states
    gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster)
    # convert probabilites to energies
    probs = np.clip(clusterProb, 0.00001, 0.99999)
    costs = -1.0 * np.log(probs)
    # add ALL unaries AT ONCE
    fids = gm.addFunctions(costs)
    gm.addFactors(fids, np.arange(rag.nodeNum))
    # add a potts function
    beta = 40.0  # strength of potts regularizer
    regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta)
    fid = gm.addFunction(regularizer)
    # get variable indices of adjacent superpixels
    # - or "u" and "v" node id's for edges
    uvIds = rag.uvIds()
    uvIds = np.sort(uvIds, axis=1)
    # add all second order factors at once
    gm.addFactors(fid, uvIds)

    # get super-pixels with slic on LAB image
    Inf = opengm.inference.BeliefPropagation
    parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001)
    inf = Inf(gm, parameter=parameter)

    class PyCallback(object):
        def __init__(self, ):
            self.labels = []

        def begin(self, inference):
            print("begin of inference")

        def end(self, inference):
            self.labels.append(inference.arg())

        def visit(self, inference):
            gm = inference.gm()
            labelVector = inference.arg()
            print("energy  %r" % (gm.evaluate(labelVector), ))
            self.labels.append(labelVector)

    callback = PyCallback()
    visitor = inf.pythonVisitor(callback, visitNth=1)

    inf.infer(visitor)

    pt.imshow(clusterProgImg3.swapaxes(0, 1))
    # plot superpixels
    cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3))
    pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap)
    pt.imshow(nodeFeaturesImgRgb)

    cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3))
    for arg in callback.labels:
        arg = vigra.taggedView(arg, "n")
        argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32))
        argImg = vigra.taggedView(argImg, "xy")
        # plot superpixels
        pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
예제 #26
0
img = vigra.impex.readImage(filepath)

# get super-pixels with slic on LAB image
imgLab = vigra.colors.transform_RGB2Lab(img)
labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight,
                                              superpixelDiameter)
labels = vigra.analysis.labelImage(labels)

# compute gradient on interpolated image
imgLabBig = vigra.resize(imgLab,
                         [imgLab.shape[0] * 2 - 1, imgLab.shape[1] * 2 - 1])
gradMag = vigra.filters.gaussianGradientMagnitude(imgLabBig, sigmaGradMag)

# get 2D grid graph and  edgeMap for grid graph
# from gradMag of interpolated image
gridGraph = graphs.gridGraph(img.shape[0:2])
gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(
    gridGraph, gradMag)

# get region adjacency graph from super-pixel labels
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

# accumulate edge weights from gradient magnitude
edgeIndicator = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator)

# accumulate node features from grid graph node map
# which is just a plain image (with channels)
nodeFeatures = rag.accumulateNodeFeatures(imgLab)
resultFeatures = graphs.recursiveGraphSmoothing(rag,
                                                nodeFeatures,
                                                edgeIndicator,
        def process_branch( branch_index, branch_rois ):
            # opFeatures and opThreshold are declared locally so this whole block can be parallelized!
            # (We use Input.setValue() instead of Input.connect() here.)
            opFeatures = OpPixelFeaturesPresmoothed(graph=tempGraph)
            
            # Compute the Hessian slicewise and create gridGraphs
            standard_scales = [0.3, 0.7, 1.0, 1.6, 3.5, 5.0, 10.0]
            standard_feature_ids = ['GaussianSmoothing', 'LaplacianOfGaussian', \
                                    'GaussianGradientMagnitude', 'DifferenceOfGaussians', \
                                    'StructureTensorEigenvalues', 'HessianOfGaussianEigenvalues']
        
            opFeatures.Scales.setValue(standard_scales)
            opFeatures.FeatureIds.setValue(standard_feature_ids)
            
            # Select Hessian Eigenvalues at scale 5.0
            scale_index = standard_scales.index(5.0)
            feature_index = standard_feature_ids.index('HessianOfGaussianEigenvalues')
            selection_matrix = numpy.zeros( (6,7), dtype=bool ) # all False
            selection_matrix[feature_index][scale_index] = True
            opFeatures.Matrix.setValue(selection_matrix)
        
            # opFeatures and opThreshold are declared locally so this whole block can be parallelized!
            # (We use Input.setValue() instead of Input.connect() here.)
            opThreshold = OpThresholdTwoLevels(graph=tempGraph)
            opThreshold.Channel.setValue(0) # We select SYNAPSE_CHANNEL before the data is given to opThreshold
            opThreshold.SmootherSigma.setValue({'x': 2.0, 'y': 2.0, 'z': 1.0}) #NOTE: two-level is much better. Maybe we can afford it?

            #opThreshold.CurOperator.setValue(0) # 0==one-level
            #opThreshold.SingleThreshold.setValue(0.4) #FIXME: solve the mess with uint8/float in predictions

            opThreshold.CurOperator.setValue(1) # 1==two-level
            opThreshold.HighThreshold.setValue(0.4)
            opThreshold.LowThreshold.setValue(0.2)
            
            previous_slice_objects = None
            previous_slice_roi = None
            conn_ids = [x.id for x in connector_infos]
            connector_infos_dict = dict(zip(conn_ids, connector_infos))

            branch_node_count = len(branch_rois)
            for node_index_in_branch, (node_info, roi) in enumerate(branch_rois):
                with Timer() as timer:
                    skeletonCoord = (node_info.x_px, node_info.y_px, node_info.z_px)
                    logger.debug("skeleton point: {}".format( skeletonCoord ))
                    #Add channel dimension
                    roi_with_channel = numpy.zeros((2, roi.shape[1]+1), dtype=numpy.uint32)
                    roi_with_channel[:, :-1] = roi[:]
                    roi_with_channel[0, -1] = 0
                    roi_with_channel[1, -1] = 1
                    iz = roi[0][2]
                    roi_hessian = (roi_with_channel[0]*2, roi_with_channel[1]*2-1)
                    for x in range(roi.shape[1]):
                        if roi[0][x] == 0:
                            roi_hessian[0][x] = 0
                    roi_hessian[0][2] = iz
                    roi_hessian[1][2] = iz+1
                    #we need the second eigenvalue
                    roi_hessian[0][-1] = 1
                    roi_hessian[1][-1] = 2
                    
                    WITH_CONNECTORS_ONLY = True
                    if WITH_CONNECTORS_ONLY:
                        if not node_info.id in node_to_connector.keys():
                            continue
                    
                    if debug_images:
                        outdir1 = outdir+"raw/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".png"
                        data = opPixelClassification3d.InputImages[-1](roi_with_channel[0], roi_with_channel[1]).wait()
                        vigra.impex.writeImage(data.squeeze().astype(numpy.uint8), outfile)
                        '''
                        outdir2 = outdir + "synapse_pred/"
                        outfile = outdir2+"%.02d"%iz + ".png"
                        data = opThreshold.InputImage(roi_with_channel[0], roi_with_channel[1]).wait()
                        vigra.impex.writeImage(data.squeeze().astype(numpy.uint8), outfile)
                        '''
                    start_pred = time.time()
                    prediction_roi = numpy.append( roi_with_channel[:,:-1], [[0],[4]], axis=1 )
                    synapse_prediction_roi = numpy.append( prediction_roi[:,:-1], [[SYNAPSE_CHANNEL],[SYNAPSE_CHANNEL+1]], axis=1 )
                    membrane_prediction_roi = numpy.append( prediction_roi[:,:-1], [[MEMBRANE_CHANNEL],[MEMBRANE_CHANNEL+1]], axis=1 )
                    
                    #synapse_predictions = opPixelClassification3d.PredictionProbabilities[-1](*prediction_roi).wait()                    
                    synapse_predictions = opSynapsePredictionCache.Output(*synapse_prediction_roi).wait()
                    synapse_predictions = vigra.taggedView( synapse_predictions, "xytc" )

                    if debug_images:
                        outdir1 = outdir+"membrane/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".png"
                        #membrane_predictions = opPixelClassification2d.HeadlessPredictionProbabilities[-1](*prediction_roi).wait()
                        membrane_predictions = opMembranePredictionCache.Output(*membrane_prediction_roi).wait()
                        vigra.impex.writeImage(membrane_predictions[..., 0].squeeze(), outfile)
                    
                    stop_pred = time.time()
                    timing_logger.debug( "spent in first 3d prediction: {}".format( stop_pred-start_pred ) )
                    opThreshold.InputImage.setValue(synapse_predictions)
                    opThreshold.InputImage.meta.drange = opPixelClassification3d.PredictionProbabilities[-1].meta.drange
                    synapse_cc = opThreshold.Output[:].wait()
                    if debug_images:
                        outdir1 = outdir+"predictions_roi/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".tiff"
                        #norm = numpy.where(synapse_cc[:, :, 0, 0]>0, 255, 0)
                        vigra.impex.writeImage(synapse_predictions[...,0,0], outfile)
        
                    if debug_images:
                        outdir1 = outdir+"synapses_roi/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".tiff"
                        norm = numpy.where(synapse_cc[:, :, 0, 0]>0, 255, 0)
                        vigra.impex.writeImage(norm.astype(numpy.uint8), outfile)
                    
                    if numpy.sum(synapse_cc)==0:
                        print "NO SYNAPSES IN THIS SLICE:", iz
                        timing_logger.debug( "ROI TIMER: {}".format( timer.seconds() ) )
                        continue
                    
                    # Distances over Hessian
                    start_hess = time.time()
                    roi_hessian = ( tuple(map(long, roi_hessian[0])), tuple(map(long, roi_hessian[1])) )
                    upsampled_combined_membranes = opUpsample.Output(*roi_hessian).wait()
                    upsampled_combined_membranes = vigra.taggedView(upsampled_combined_membranes, opUpsample.Output.meta.axistags )
                    opFeatures.Input.setValue(upsampled_combined_membranes)
                    eigenValues = opFeatures.Output[...,1:2].wait() #we need the second eigenvalue
                    eigenValues = numpy.abs(eigenValues[:, :, 0, 0])
                    stop_hess = time.time()
                    timing_logger.debug( "spent for hessian: {}".format( stop_hess-start_hess ) )
                    shape_x = roi[1][0]-roi[0][0]
                    shape_y =  roi[1][1]-roi[0][1]
                    shape_x = long(shape_x)
                    shape_y = long(shape_y)
                    start_gr = time.time()
                    gridGr = graphs.gridGraph((shape_x, shape_y )) # !on original pixels
                    gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGr, eigenValues) 
                    #gridGraphs.append(gridGr)
                    #graphEdges.append(gridGraphEdgeIndicator)
                    stop_gr = time.time()
                    timing_logger.debug( "creating graph: {}".format( stop_gr - start_gr ) )
                    if debug_images:
                        outdir1 = outdir+"hessianUp/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".tiff"
                        logger.debug( "saving hessian to file: {}".format( outfile ) )
                        vigra.impex.writeImage(eigenValues, outfile )
                    
                    instance = vigra.graphs.ShortestPathPathDijkstra(gridGr)
                    relative_coord = [skeletonCoord[0]-roi[0][0], skeletonCoord[1]-roi[0][1]]
                    relative_coord = map(long, relative_coord)
                    sourceNode = gridGr.coordinateToNode(relative_coord)
                    start_dij = time.time()
                    instance.run(gridGraphEdgeIndicator, sourceNode, target=None)
                    
                    distances = instance.distances()
                    stop_dij = time.time()
                    timing_logger.debug( "spent in dijkstra {}".format( stop_dij - start_dij ) )
                    if debug_images:
                        outdir1 = outdir+"distances/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".tiff"
                        logger.debug( "saving distances to file:".format( outfile ) )
                        # Create a "white" pixel at the source node
                        distances[skeletonCoord[0]-roi[0][0], skeletonCoord[1]-roi[0][1]] = numpy.max(distances)
                        vigra.impex.writeImage(distances, outfile )

                    # Distances over raw membrane probabilities
                    roi_upsampled_membrane = numpy.asarray( roi_hessian )
                    roi_upsampled_membrane[:, -1] = [0,1]
                    roi_upsampled_membrane = (map(long, roi_upsampled_membrane[0]), map(long, roi_upsampled_membrane[1]))
                    connector_distances = None
                    connector_coords = None
                    if node_info.id in node_to_connector.keys():
                        connectors = node_to_connector[node_info.id]
                        connector_info = connector_infos_dict[connectors[0]]
                        #Convert to pixels
                        con_x_px = int(connector_info.x_nm / float(X_RES))
                        con_y_px = int(connector_info.y_nm / float(Y_RES))
                        con_z_px = int(connector_info.z_nm / float(Z_RES))
                        connector_coords = (con_x_px-roi[0][0], con_y_px-roi[0][1])
                        if con_x_px>roi[0][0] and con_x_px<roi[1][0] and con_y_px>roi[0][1] and con_y_px<roi[1][1]:
                            #this connector is inside our prediction roi, compute the distance field                                                                                                        "
                            con_relative = [long(con_x_px-roi[0][0]), long(con_y_px-roi[0][1])]
    
                            sourceNode = gridGr.coordinateToNode(con_relative)
                            instance.run(gridGraphEdgeIndicator, sourceNode, target=None)
                            connector_distances = instance.distances()
                        else:
                            connector_distances = None
                    
                    upsampled_membrane_probabilities = opUpsample.Output(*roi_upsampled_membrane).wait().squeeze()
                    upsampled_membrane_probabilities = vigra.filters.gaussianSmoothing(upsampled_membrane_probabilities, sigma=1.0)
                    #print "UPSAMPLED MEMBRANE SHAPE: {} MAX: {} MIN: {}".format( upsampled_membrane_probabilities.shape, upsampled_membrane_probabilities.max(), upsampled_membrane_probabilities.min() )
                    gridGrRaw = graphs.gridGraph((shape_x, shape_y )) # !on original pixels
                    gridGraphRawEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGrRaw, upsampled_membrane_probabilities) 
                    #gridGraphs.append(gridGrRaw)
                    #graphEdges.append(gridGraphRawEdgeIndicator)
                    instance_raw = vigra.graphs.ShortestPathPathDijkstra(gridGrRaw)
                    sourceNode = gridGrRaw.coordinateToNode(relative_coord)
                    instance_raw.run(gridGraphRawEdgeIndicator, sourceNode, target=None)
                    distances_raw = instance_raw.distances()

                    stop_dij = time.time()
                    timing_logger.debug( "spent in dijkstra (raw probs) {}".format( stop_dij - start_dij ) )
                    if debug_images:
                        outdir1 = outdir+"distances_raw/"
                        try:
                            os.makedirs(outdir1)
                        except os.error:
                            pass
                        outfile = outdir1+"/{}-{}".format( iz, node_info.id ) + ".tiff"
                        logger.debug( "saving distances (raw probs) to file:".format( outfile ) )
                        # Create a "white" pixel at the source node
                        distances_raw[skeletonCoord[0]-roi[0][0], skeletonCoord[1]-roi[0][1]] = numpy.max(distances_raw)
                        vigra.impex.writeImage(distances_raw, outfile )
    
                    if numpy.sum(synapse_cc)==0:
                        continue
                    

                    with max_label_lock:                    
                        synapse_objects_4d, maxLabelCurrent = normalize_synapse_ids( synapse_cc, 
                                                                                     roi,
                                                                                     previous_slice_objects, 
                                                                                     previous_slice_roi,
                                                                                     maxLabelSoFar[0] )
    
                        maxLabelSoFar[0] = maxLabelCurrent
                        synapse_objects = synapse_objects_4d.squeeze()

                        #add this synapse to the exported list
                        previous_slice_objects = synapse_objects
                        previous_slice_roi = roi
                    '''
                    if numpy.sum(synapse_cc)==0:
                        print "NO SYNAPSES IN THIS SLICE:", iz
                        timing_logger.debug( "ROI TIMER: {}".format( timer.seconds() ) )
                        continue
                    '''
                    synapseIds = set(synapse_objects.flat)
                    synapseIds.remove(0)
                    for sid in synapseIds:
                        #find the pixel positions of this synapse
                        syn_pixel_coords = numpy.where(synapse_objects == sid)
                        synapse_size = len( syn_pixel_coords[0] )
                        #syn_pixel_coords = numpy.unravel_index(syn_pixels, distances.shape)
                        #FIXME: offset by roi
                        syn_average_x = numpy.average(syn_pixel_coords[0])+roi[0][0]
                        syn_average_y = numpy.average(syn_pixel_coords[1])+roi[0][1]
                        
                        syn_distances = distances[syn_pixel_coords]
                        mindist = numpy.min(syn_distances)
                        
                        syn_distances_raw = distances_raw[syn_pixel_coords]
                        mindist_raw = numpy.min(syn_distances_raw)

                        if connector_distances is not None:
                            syn_distances_connector = connector_distances[syn_pixel_coords]
                            min_conn_distance = numpy.min(syn_distances_connector)
                            
                        elif connector_coords is not None:
                            euclidean_dists = [scipy.spatial.distance.euclidean(connector_coords, xy) for xy in zip(syn_pixel_coords[0], syn_pixel_coords[1])]
                            min_conn_distance = numpy.min(euclidean_dists)
                        else:
                            min_conn_distance = 99999.0
                            
                        # Determine average uncertainty
                        # Get probabilities for this synapse's pixels
                        flat_predictions = synapse_predictions.view(numpy.ndarray)[synapse_objects_4d[...,0] == sid]

                        # If we pulled the data from cache, there may be only one channel.
                        # In that case, we can't quite compute a proper uncertainty, 
                        #  so we'll just pretend there were only two prediction channels to begin with.
                        if flat_predictions.shape[-1] > 1:
                            # Sort along channel axis
                            flat_predictions.sort(axis=-1)
                            # What's the difference between the highest and second-highest class?
                            certainties = flat_predictions[:,-1] - flat_predictions[:,-2]
                        else:
                            # Pretend there were only two channels
                            certainties = flat_predictions[:,0] - (1 - flat_predictions[:,0])
                        avg_certainty = numpy.average(certainties)
                        avg_uncertainty = 1.0 - avg_certainty                        

                        fields = {}
                        fields["synapse_id"] = int(sid)
                        fields["x_px"] = int(syn_average_x + 0.5)
                        fields["y_px"] = int(syn_average_y + 0.5)
                        fields["z_px"] = iz
                        fields["size_px"] = synapse_size
                        fields["distance_hessian"] = mindist
                        fields["distance_raw_probs"] = mindist_raw
                        fields["detection_uncertainty"] = avg_uncertainty
                        fields["node_id"] = node_info.id
                        fields["node_x_px"] = node_info.x_px
                        fields["node_y_px"] = node_info.y_px
                        fields["node_z_px"] = node_info.z_px
                        if min_conn_distance!=99999.0:
                            connectors = node_to_connector[node_info.id]
                            connector_info = connector_infos_dict[connectors[0]]
                            fields["nearest_connector_id"] = connector_info.id
                            fields["nearest_connector_distance_nm"] = min_conn_distance
                            fields["nearest_connector_x_nm"] = connector_info.x_nm
                            fields["nearest_connector_y_nm"] = connector_info.y_nm
                            fields["nearest_connector_z_nm"] = connector_info.z_nm
                        else:
                            fields["nearest_connector_id"] = -1
                            fields["nearest_connector_distance_nm"] = min_conn_distance
                            fields["nearest_connector_x_nm"] = -1
                            fields["nearest_connector_y_nm"] = -1
                            fields["nearest_connector_z_nm"] = -1
                                        
                        

                        with f_out_lock:
                            csv_writer.writerow( fields )                                                
                            fout.flush()

                    with f_out_lock:
                        node_overall_index[0] += 1
                        progress_callback( ProgressInfo( node_overall_index[0], 
                                                         skeleton_node_count, 
                                                         branch_index, 
                                                         skeleton_branch_count, 
                                                         node_index_in_branch, 
                                                         branch_node_count,
                                                         maxLabelCurrent ) )
            
                        
                    #Sanity check
                    #outfile = outdir+"hessianUp/"+ "%.02d"%iz + ".tiff"
                    #vigra.impex.writeImage(eigenValues, outfile)
                    #outfile = outdir+"distances/"+ "%.02d"%iz + ".tiff"
                    #vigra.impex.writeImage(distances, outfile)
                timing_logger.debug( "ROI TIMER: {}".format( timer.seconds() ) )
예제 #28
0
def eccentricity(img,
                 distFunc="exponential",
                 showPathImage=False,
                 percentageOfPaths=100,
                 imgSaveName=""):

    img = img.astype(numpy.uint8)
    dim = len(img.shape)

    ## Enlarge image by one pixel on each side
    bigShape = []
    for s in img.shape:
        bigShape.append(s + 2)
    bigImg = numpy.ones(bigShape)
    slices = []
    for i in range(dim):
        slices.append(slice(1, bigImg.shape[i] - 1))
    bigImg[slices] = img
    inside = numpy.where(bigImg == 0)
    outside = numpy.where(bigImg == 1)

    ## Apply distanceTransform and modify (outside: high values, inside: low values)
    distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32))
    imgp = distImage.copy()
    # if showPathImage:
    #     imgp = distImage.copy()
    if distFunc == "exponential":
        distImage = numpy.exp(distImage * -gamma)
    elif distFunc == "linear":
        maxDist = distImage.max()
        distImage = maxDist - distImage + 10
    elif distFunc == "inverse":
        w = numpy.where(distImage != 0)
        distImage[w] = 1 / distImage[w]
    else:
        print "wrong parameters for distFunc in eccentricity"

    ## Distance in the inside between two pixels is 1.0
    #distImage = bigImg.copy().astype(numpy.float32)
    #distImage[inside]=1.0

    # if len(imgSaveName)>1:
    #     plt.imshow(numpy.swapaxes(distImage, 1, 0))
    #     plt.savefig(imgSaveName+"_dist.png")
    #     #scipy.misc.imsave(imgSaveName+"_dist.png", numpy.swapaxes(distImage, 1, 0))

    ## Set the outside to a very high value
    distImage[outside] = 1000.0

    ## Image copy to draw the paths in
    #imgp = distImage.copy()
    imgp[outside] = 100

    ## Get image graph and its path finder
    gridGraph = vigraph.gridGraph(bigImg.shape[0:dim], False)
    graphShape = []
    for s in distImage.shape:
        graphShape.append(s * 2 - 1)
    edgeWeights = vigra.resize(distImage, graphShape, order=0)
    #edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, edgeWeights)
    edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(
        gridGraph, edgeWeights)
    pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

    ## Find borders in img
    if dim == 2:
        bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8))
    else:
        bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8))
    rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg)
    node = vigraph.GridRegionAdjacencyGraph.nodeFromId(
        rag, long(bigLblImg[inside][0]))
    edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges,
                                  bigLblImg, node)

    borderImg = numpy.zeros(bigImg.shape)
    for edge in edges:
        slices = []
        for d in range(dim):
            slices.append(slice(edge[d], edge[d] + 1))
        borderImg[slices] = 1

    ## End points for paths (all points on the border)
    targets = numpy.where(borderImg == 1)
    nTargets = len(targets[0])

    ## Find the diameter (longest path)
    eccLength = numpy.empty(nTargets)
    eccLength.fill(-1)
    eccTargetPath = {}
    vpIndex = 0
    vpGraphIndex = []
    for d in range(dim):
        vpGraphIndex.append(int(targets[d][vpIndex]))
    vp = gridGraph.coordinateToNode(vpGraphIndex)
    visited = numpy.zeros(nTargets)
    while True:
        visited[vpIndex] = 1
        pathFinder.run(edgeWeights, vp)
        eccChanged = False
        for j in range(nTargets):
            targetIndex = []
            for d in range(dim):
                targetIndex.append(int(targets[d][j]))
            target = gridGraph.coordinateToNode(targetIndex)
            pathLength = pathFinder.distance(target)
            m = max(eccLength[j], pathLength)
            if m > eccLength[j]:
                eccChanged = True
                eccLength[j] = m
                eccTargetPath[j] = pathFinder.path(pathType='coordinates',
                                                   target=target)
        vpIndex = numpy.argmax(eccLength)
        vpGraphIndex = []
        for d in range(dim):
            vpGraphIndex.append(int(targets[d][vpIndex]))
        vp = gridGraph.coordinateToNode(vpGraphIndex)
        if visited[vpIndex] or not eccChanged:
            break

    ## Find the length of the diameter (non-weighted)
    path = eccTargetPath[vpIndex]
    dMax = 0
    for k in range(1, len(path)):
        diffCount = 0
        for d in range(dim):
            if path[k][d] != path[k - 1][d]:
                diffCount += 1
        dMax += numpy.sqrt(diffCount)

    ## Find the midpoint of the diameter
    dMax = dMax / 2
    if len(path) == 0:
        path = numpy.empty((1, 2), numpy.uint8)
        path[0][0] = targets[0][0]
        path[0][1] = targets[1][0]
    p1 = path[0]
    d1 = 0
    for k in range(1, len(path)):
        p2 = path[k]
        d2 = d1 + numpy.linalg.norm(p2 - p1)
        if d2 > dMax:
            if (abs(d2 - dMax) < abs(d1 - dMax)):
                p1 = p2
            break
        p1 = p2
        d1 = d2

    ## Compute eccentricity from center (p1) to all points on border
    sourceIndex = []
    for d in range(dim):
        sourceIndex.append(int(p1[d]))
    source = gridGraph.coordinateToNode(sourceIndex)
    pathFinder.run(edgeWeights, source)
    maxPathLength = 0
    for j in range(nTargets):
        targetIndex = []
        for d in range(dim):
            targetIndex.append(int(targets[d][j]))
        target = gridGraph.coordinateToNode(targetIndex)
        pathLength = pathFinder.distance(target)
        maxPathLength = max(maxPathLength, pathLength)
        imgp[targetIndex[0], targetIndex[1]] = 40 * pathLength

    imgp[path[:, 0], path[:, 1]] = 12 * maxPathLength
    imgp[sourceIndex[0], sourceIndex[1]] = 40 * maxPathLength
    plt.figure(distFunc)
    plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none')
    if len(imgSaveName) > 1:
        scipy.misc.imsave(imgSaveName + ".png", numpy.swapaxes(imgp, 1, 0))
예제 #29
0
import vigra.graphs as graphs
import pylab
import numpy as np

import sys
import matplotlib
import pylab as plt
import math
from matplotlib.widgets import Slider, Button, RadioButtons

# parameter:
imPath = ('holyRegion.h5', 'im')  # input image path
labPath = ('segMaskOnly.h5', 'data')  # labeled image path

# load volume
labels = vigra.impex.readHDF5(*labPath).astype(np.uint32)[:, :, 0:20]
volume = vigra.impex.readHDF5(*imPath)[:, :, 0:20]

gridGraph = graphs.gridGraph(labels.shape)
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

rand = np.random.rand(rag.edgeNum) * 2 - 1

gui = vigra.graphs.TinyEdgeLabelGui(rag=rag,
                                    img=volume,
                                    edgeLabels=None,
                                    labelMode=True)
gui.startGui()

print gui.edgeLabels
# load image and convert to LAB
img = vigra.impex.readImage(filepath)

# get super-pixels with slic on LAB image
imgLab = vigra.colors.transform_RGB2Lab(img)
labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight,
                                              superpixelDiameter)
labels = vigra.analysis.labelImage(labels)

# compute gradient on interpolated image
imgLabBig = vigra.resize(imgLab, [imgLab.shape[0]*2-1, imgLab.shape[1]*2-1])
gradMag = vigra.filters.gaussianGradientMagnitude(imgLabBig, sigmaGradMag)

# get 2D grid graph and  edgeMap for grid graph
# from gradMag of interpolated image
gridGraph = graphs.gridGraph(img.shape[0:2])
gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGraph,
                                                                  gradMag)
# get region adjacency graph from super-pixel labels
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

# accumulate edge weights from gradient magnitude
edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator)

# accumulate node features from grid graph node map
# which is just a plain image (with channels)
nodeFeatures = rag.accumulateNodeFeatures(imgLab)

# do agglomerativeClustering
labels = graphs.agglomerativeClustering(graph=rag, edgeWeights=edgeWeights,
                                        beta=beta, nodeFeatures=nodeFeatures,
예제 #31
0
print imgLab.shape

print "interpolate image"
imgLabBig = vigra.resize(imgLab,
                         [imgLab.shape[0] * 2 - 1, imgLab.shape[1] * 2 - 1])

# make a few edge weights

gradmag = numpy.squeeze(
    vigra.filters.gaussianGradientMagnitude(imgLabBig, sigma))
hessian = numpy.squeeze(
    vigra.filters.hessianOfGaussianEigenvalues(imgLabBig[:, :, 0],
                                               sigma))[:, :, 0]
hessian -= hessian.min()
raw = 256 - imgLabBig[:, :, 0].copy()
gridGraph = vigraph.gridGraph(imgLab.shape[:2], False)

weights = makeWeights(3.0)

pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

visuimg = img.copy()
ax = plt.gca()
fig = plt.gcf()
visuimg -= visuimg.min()
visuimg /= visuimg.max()
implot = ax.imshow(numpy.swapaxes(visuimg, 0, 1), cmap='gray')

clickList = []
frozen = False
예제 #32
0
파일: asdf.py 프로젝트: jhennies/py_devel
        options.stdDev = (4.5, )*3
        print "smooth"
        ev = bw.gaussianSmooth(ev, options)

        vigra.impex.writeHDF5(ev,"growmap.h5",'data')

    else:
        ev = vigra.impex.readHDF5("growmap.h5",'data')

    with vigra.Timer("watershedsNew"):
        labels, nseg = vigra.analysis.unionFindWatershed3D(ev,(100,100,100))


    print "gridGraph"
    gridGraph = graphs.gridGraph(labels.shape)
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)
    rag.writeHDF5("rag.h5",'data')
else:
    rag = vigra.graphs.loadGridRagHDF5("rag.h5",'data')
    labels=rag.labels









예제 #33
0
img/=img.max()
img*=255

print(imgLab.shape)


print("interpolate image")
imgLabSmall = imgLab

# make a few edge weights

gradmag = numpy.squeeze(vigra.filters.gaussianGradientMagnitude(imgLabSmall,sigma))
hessian = numpy.squeeze(vigra.filters.hessianOfGaussianEigenvalues(imgLabSmall[:,:,0],sigma))[:,:,0]
hessian-=hessian.min()
raw     = 256-imgLabSmall[:,:,0].copy()
gridGraph  = vigraph.gridGraph(imgLab.shape[:2],False)  



weights  = makeWeights(3.0)


pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)


visuimg =img.copy()
ax = plt.gca()
fig = plt.gcf()
visuimg-=visuimg.min()
visuimg/=visuimg.max()
implot = ax.imshow(numpy.swapaxes(visuimg,0,1),cmap='gray')
예제 #34
0
    grayData.append([dist, "dist"])
    grayData.append([res, "thinned"])

    print "write thinned pmap transform"
    vigra.impex.writeHDF5(res, opt['thinnedDistTransformPMap1'], "data")

    skneuro.addHocViewer(grayData, segData)

if False:
    print "make the graph and save it"

    print "read segmentation"
    seg = vigra.impex.readHDF5(opt['oversegL0'], "data")
    segData.append([seg, "seg"])

    gridGraph = graphs.gridGraph(seg.shape[0:3])
    print "make rag"
    # get region adjacency graph from super-pixel labels
    rag = graphs.regionAdjacencyGraph(gridGraph, seg, isDense=False)

    print "save rag to file"
    rag.writeHDF5(opt['ragL0'], 'data')

if False:
    print "accumulate edge weights and save them"

    print "load rag"
    # get region adjacency graph from super-pixel labels
    rag = graphs.loadGridRagHDF5(opt['ragL0'], 'data')
    gridGraph = rag.baseGraph
예제 #35
0
def eccentricity( img, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ):
    ## Enlarge image by one pixel on each side
    img = img.astype(numpy.uint8)
    bigImg = numpy.ones( (img.shape[0]+2, img.shape[1]+2) )
    bigImg[1:bigImg.shape[0]-1, 1:bigImg.shape[1]-1] = img

    ## Find borders in img (replace with graph functions)
    borderImg = numpy.zeros(bigImg.shape)
    for y in range(bigImg.shape[1]-1):
        for x in range(bigImg.shape[0]-1):
            if bigImg[x,y] == 0:
                if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1:
                    borderImg[x, y] = 1
            else:
                if bigImg[x+1,y] == 0:
                    borderImg[x+1, y] = 1
                if bigImg[x,y+1] == 0:
                    borderImg[x, y+1] = 1

## regionImageToCrackEdgeImage ( labelImage )


    # ## Apply distanceTransform and modify (outside: high values, inside: low values)
    # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32))
    # if showPathImage:
    #     imgp = distImage.copy()
    # if distFunc == "exponential":
    #     distImage = numpy.exp(distImage*-gamma)
    # elif distFunc == "linear":
    #     maxDist = distImage.max()
    #     distImage = maxDist - distImage
    # elif distFunc == "inverse":
    #     w = numpy.where(distImage!=0)
    #     distImage[w] = 1/distImage[w]
    # else:
    #     print "wrong parameters for distFunc in eccentricity"

    ## Distance in the inside between two pixels is 1.0
    distImage = bigImg.copy().astype(numpy.float32)
    distImage[numpy.where(bigImg==0)]=1.0

    ## Set the outside to a very high value
    distImage[numpy.where(bigImg==1)]=10000.0

    imgp = distImage.copy()

    ## Get image graph and its path finder
    gridGraph = vigraph.gridGraph(bigImg.shape[0:2],False)
    edgeWeights = vigra.resize(distImage,[distImage.shape[0]*2-1,distImage.shape[1]*2-1],order=0)
    edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph,edgeWeights)
    pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph)

    ## End points for paths (all points on the border)
    targets = numpy.where(borderImg==1)
    tx,ty = targets
    nTargets = len(tx)

    ## Indices of start points for paths (random)
    nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0))
    numpy.random.seed(42)
    starts = numpy.random.permutation(range(nTargets))[:nPoints]

    ## Compute paths
    maxPaths = []
    maxPathLengths = []
    for i in range(nPoints):
        source = gridGraph.coordinateToNode((int(tx[starts[i]]), int (ty[starts[i]])))
        pathFinder.run(edgeWeights, source)
        maxPathLength = 0
        for j in range(nTargets):
            target = gridGraph.coordinateToNode((int(tx[j]), int(ty[j])))
            path = pathFinder.path(pathType='coordinates', target=target)
            pathLength = pathFinder.distance(target)
            if pathLength > maxPathLength or maxPathLength == 0:
                maxPathLength = pathLength
                maxPath = path
        maxPaths.append(maxPath)
        maxPathLengths.append(maxPathLength)

    if showPathImage or len(imgSaveName)>1:
        val = (imgp.max()+imgp.min())/2
        for p in maxPaths:
            imgp[p[:,0], p[:,1]] = val
    if showPathImage:
        plt.figure(distFunc)
        plt.imshow(imgp, interpolation='none')
    if len(imgSaveName)>1:
        scipy.misc.imsave(imgSaveName, imgp)

    return maxPathLengths