def pmapWatershed(pmap, raw, visu=True, seedThreshold=0.6): viewGrayData = [(pmap, "pmap") ] viewLabelData= [] print "densoise" pmapD = denoise.tvBregman(pmap, weight=4.5, isotropic=True).astype(numpy.float32) pmapG = vigra.filters.gaussianSmoothing(pmap, 1.0) viewGrayData.append((pmapG, "pmapGauss")) viewGrayData.append((pmapD, "pmapTotalVariation")) viewGrayData.append((raw, "raw")) #addHocViewer(viewGrayData, viewLabelData, visu=visu) print "compute local minima " localMin = vigra.analysis.extendedLocalMinima3D(pmapD,neighborhood=26) localMin2 = localMin.astype(numpy.float32) print "tweak min" localMin2 *= pmap whereZero = numpy.where(localMin == 0) localMin2[whereZero] = 100.0 whereMin = numpy.where(localMin2 <= seedThreshold) filteredLocalMin = numpy.zeros(localMin.shape, dtype=numpy.uint8) filteredLocalMin[whereMin] = 1 viewGrayData.append([localMin,"localMin"]) viewGrayData.append([filteredLocalMin,"filteredLocalMin"]) # compute connected components seeds = vigra.analysis.labelVolumeWithBackground(filteredLocalMin, neighborhood=26) viewLabelData.append([seeds, "seeds"]) print "watersheds" seg, nseg = vigra.analysis.watersheds(pmapG.astype(numpy.float32), seeds=seeds.astype(numpy.uint32)) print "nseg",nseg viewLabelData.append([seg, "seg"]) addHocViewer(viewGrayData, viewLabelData, visu=visu) return se
def pmapWatershed(pmap, raw, visu=True, seedThreshold=0.6): viewGrayData = [(pmap, "pmap")] viewLabelData = [] print "densoise" pmapD = denoise.tvBregman(pmap, weight=4.5, isotropic=True).astype(numpy.float32) pmapG = vigra.filters.gaussianSmoothing(pmap, 1.0) viewGrayData.append((pmapG, "pmapGauss")) viewGrayData.append((pmapD, "pmapTotalVariation")) viewGrayData.append((raw, "raw")) #addHocViewer(viewGrayData, viewLabelData, visu=visu) print "compute local minima " localMin = vigra.analysis.extendedLocalMinima3D(pmapD, neighborhood=26) localMin2 = localMin.astype(numpy.float32) print "tweak min" localMin2 *= pmap whereZero = numpy.where(localMin == 0) localMin2[whereZero] = 100.0 whereMin = numpy.where(localMin2 <= seedThreshold) filteredLocalMin = numpy.zeros(localMin.shape, dtype=numpy.uint8) filteredLocalMin[whereMin] = 1 viewGrayData.append([localMin, "localMin"]) viewGrayData.append([filteredLocalMin, "filteredLocalMin"]) # compute connected components seeds = vigra.analysis.labelVolumeWithBackground(filteredLocalMin, neighborhood=26) viewLabelData.append([seeds, "seeds"]) print "watersheds" seg, nseg = vigra.analysis.watersheds(pmapG.astype(numpy.float32), seeds=seeds.astype(numpy.uint32)) print "nseg", nseg viewLabelData.append([seg, "seg"]) addHocViewer(viewGrayData, viewLabelData, visu=visu) return se
def restrictedLocalMinima(image, minAllowed, neighborhood=26): """ discard any min where minAllowed[x,y,y]==0 """ localMin = vigra.analysis.extendedLocalMinima3D(numpy.ensure(image,dtype=numpy.float32),neighborhood=neighborhood) minNotAllowed = numpy.where(minAllowed==0) localMin[minNotAllowed]=0 return localMin
def restrictedLocalMinima(image, minAllowed, neighborhood=26): """ discard any min where minAllowed[x,y,y]==0 """ localMin = vigra.analysis.extendedLocalMinima3D(numpy.ensure( image, dtype=numpy.float32), neighborhood=neighborhood) minNotAllowed = numpy.where(minAllowed == 0) localMin[minNotAllowed] = 0 return localMin
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 getValidRegionCentersAndTheirIDs(featureDict, countFeatureName='Count', regionCenterName='RegionCenter'): """ From the feature dictionary of a certain frame, find all objects with pixel count > 0, and return their region centers and ids. """ validObjectMask = featureDict[countFeatureName] > 0 validObjectMask[0] = False regionCenters = featureDict[regionCenterName][validObjectMask, :] objectIds = list(np.where(validObjectMask)[0]) return regionCenters, objectIds
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 agglomerative_clustering_2d(img, labels, use_gradient = False): labels = vigra.analysis.labelImage(labels) imgBig = vigra.resize(img, [img.shape[0]*2-1, img.shape[1]*2-1]) if use_gradient: # compute the gradient on interpolated image sigmaGradMag = 2.0 gradMag = vigra.filters.gaussianGradientMagnitude(imgBig, sigmaGradMag) # get 2D grid graph and edgeMap for grid graph # from gradMag of interpolated image or from plain image (depending on use_gradient) gridGraph = vigra.graphs.gridGraph(img.shape[0:2]) gridGraphEdgeIndicator = [] if use_gradient: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, gradMag) else: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, imgBig) # get region adjacency graph from super-pixel labels rag = vigra.graphs.regionAdjacencyGraph(gridGraph, labels) # accumalate edge weights from gradient magintude edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # agglomerative clustering beta = 0.5 nodeNumStop = 20 clustering = vigra.graphs.agglomerativeClustering(graph = rag, edgeWeights = edgeWeights, beta = beta, nodeNumStop = nodeNumStop) res = np.zeros( labels.shape ) for c in range(len(clustering)): res[ np.where( labels== (c-1) ) ] = clustering[c] res = vigra.Image(res) res = vigra.analysis.labelImage(res) return res
def volumeToListOfPoints(seedsVolume, threshold=0.): return numpy.array(numpy.where(seedsVolume > threshold)).transpose()