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
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
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
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 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
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
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
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, gamma=gamma, edgeThreshold=edgeThreshold, scale=scale, iterations=iterations)
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
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 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
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 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 ragEdgeIndicator = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # get labels/segmentation for rag ragLabels = graphs.felzenszwalbSegmentation(rag, ragEdgeIndicator, k=10, nodeNumStop=1000) # get more corsair graph from labeled rag rag2 = graphs.regionAdjacencyGraph(graph=rag, labels=ragLabels) # accumulate new edge weights rag2EdgeIndicator = rag2.accumulateEdgeFeatures(ragEdgeIndicator, acc='mean')
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)
sys.stdout.write('\r') sys.stdout.write("[%-50s] %d%%" % ('='*int(float(i+1)/T*50), int(float(i+1)/T*100))) sys.stdout.flush() fileId = filename[:-4] trainingIds.append(fileId) img = vigra.impex.readImage(root + '/' + filename) trainingImgs.append(img) imgLab = vigra.colors.transform_RGB2Lab(img) gridGraph = graphs.gridGraph(img.shape[0:2]) slicLabels = vigra.analysis.labelImage(vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter, minSize=minSize)[0]) rag = graphs.regionAdjacencyGraph(gridGraph, slicLabels) trainingRags.append(rag) #gtWatershed = loadmat('trainingSet/groundTruth/' + fileId + '.mat')['groundTruth'][0,0][0][0][0] #gtLabel = maf.getSuperpixelLabelList(rag, gtWatershed) gtLabel = np.load('groundTruthLabels/' + fileId + '.npy') trainingGtLabels.append(gtLabel) trainingGtSols.append(maf.getGroundTruthSol(rag, gtLabel)) ### Feature Spaces # Training #testingFeatureSpacesPath = resultsPath + 'featureSpaces/training/'
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 app = QtGui.QApplication([])
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 print "read dmap" tmap = vigra.impex.readHDF5(opt['thinnedDistTransformPMap1'], 'data') tmap = numpy.require(tmap, dtype=numpy.float32)
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 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 ragEdgeIndicator = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # get labels/segmentation for rag ragLabels = graphs.felzenszwalbSegmentation(rag, ragEdgeIndicator, k=10, nodeNumStop=1000) # get more corsair graph from labeled rag rag2 = graphs.regionAdjacencyGraph(graph=rag, labels=ragLabels) # accumulate new edge weights rag2EdgeIndicator = rag2.accumulateEdgeFeatures(ragEdgeIndicator, acc='mean')