def placePointsInVolumen(points, shape): volumen = numpy.zeros(shape) points = numpy.maximum(points, numpy.array((0, 0, 0))) points = numpy.minimum(points, numpy.array(shape) - 1) for point in (numpy.floor(points)).astype(int): volumen[point[0], point[1], point[2]] = 1 return volumen
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 addSample(self, f1, f2, label, pluginManager): # if self.labels == []: self.labels.append(label) # else: # self.labels = np.concatenate((np.array(self.labels),label)) # for adding batches of features features = self.constructSampleFeatureVector(f1, f2, pluginManager) if self._numSamples is None: # use vstack if self.mydata is None: self.mydata = features else: self.mydata = np.vstack((self.mydata, features)) else: # allocate full array once, then fill in row by row if self.mydata is None: self.mydata = np.zeros((self._numSamples, features.shape[0])) assert(self._nextIdx < self._numSamples) self.mydata[self._nextIdx, :] = features self._nextIdx += 1
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 computeGlobalFeatures(pmap, mitoIndex, membraneIndex, visu=True, dsetName='data'): f = h5py.File(pmap, 'r') raw = f[dsetName][0:300, 0:300, 300:400, 0] pMito = f[dsetName][0:300, 0:300, 300:400, membraneIndex].astype('float32') pMito -= pMito.min() pMito /= pMito.max() bMito = numpy.zeros(pMito.shape, dtype='uint8') bMito[pMito > 0.9] = 1 sdt = vigra.filters.signedDistanceTransform(bMito) grayData = [(raw, "raw"), (pMito, "pMito"), (bMito, "bMito"), (sdt, "sdt")] segData = [ #(labels, "labels") ] skneuro.addHocViewer(grayData, segData, visu=visu)
def computeGlobalFeatures(pmap, mitoIndex, membraneIndex, visu=True, dsetName='data'): f = h5py.File(pmap,'r') raw = f[dsetName][0:300,0:300,300:400,0] pMito = f[dsetName][0:300,0:300,300:400,membraneIndex].astype('float32') pMito-=pMito.min() pMito/=pMito.max() bMito = numpy.zeros(pMito.shape, dtype='uint8') bMito[pMito>0.9] = 1 sdt = vigra.filters.signedDistanceTransform(bMito) grayData = [ (raw,"raw"), (pMito,"pMito"), (bMito,"bMito"), (sdt,"sdt") ] segData = [ #(labels, "labels") ] skneuro.addHocViewer(grayData, segData,visu=visu)
def getLargeSeeds(raw, pmap, membraneWidth , threshold , rank, visu=False): with vigra.Timer("ballRankOrderFilter"): # make mebrane wider with ballRankOrderFilter r = int(membraneWidth*0.35 + 0.5) r = max(r, 1) widerPmap1 = denoise.ballRankOrderFilter(pmap, radius=r, rank=rank) widerPmap = denoise.ballRankOrderFilter(widerPmap1, radius=r, rank=rank) widerPmap1 = None with vigra.Timer("normalize"): # renormalize widerPmap -= widerPmap.min() widerPmap /= widerPmap.max() with vigra.Timer("binarize"): # binarize binaryPmap = numpy.zeros(widerPmap.shape, dtype='uint8') binaryPmap[pmap>threshold] = 1 #widerPmap = None # save mem if visu == False: widerPmap = None with vigra.Timer("multiBinaryDilation"): # morphology # 1) make membrane wider by r r = int(membraneWidth*1.2 + 0.5) r = max(r, 2) mBinaryPmapA = vigra.filters.multiBinaryDilation(binaryPmap,r) #binaryPmap = None # save mem if visu == False: binaryPmap = None #with vigra.Timer("multiBinaryErosion"): # # morphology # # 1) make membrane smaller by r # r = int(membraneWidth*0.1 + 0.5) # r = max(r, 1) # mBinaryPmapB = vigra.filters.multiBinaryErosion(mBinaryPmapA,r) # if visu == False: # mBinaryPmapA = None with vigra.Timer("labelVolumeWithBackground"): # get seeds invertedBinaryPmap = 1- mBinaryPmapA if visu == False: mBinaryPmapB = None invertedBinaryPmap = numpy.require(invertedBinaryPmap, dtype='uint32') ccImg = vigra.analysis.labelVolumeWithBackground(invertedBinaryPmap) if visu == False: invertedBinaryPmap = None if visu: grayData = [ (raw, "raw"), (pmap,"pmap"), (widerPmap,"widerPmap"), (binaryPmap,"binaryPmap"), (mBinaryPmapA,"mBinaryPmapA"), #(mBinaryPmapB,"mBinaryPmapB"), ] segData = [ (ccImg, "seeds"), #(seg, "seg") ] skneuro.addHocViewer(grayData, segData) return ccImg
# add all internal links of tracklets for v in uuidToTraxelMap.values(): prev = None for timestepIdTuple in v: if prev is not None: links.append((prev, timestepIdTuple)) prev = timestepIdTuple # group by timestep timesteps = [t for t in traxelIdPerTimestepToUniqueIdMap.keys()] linksPerTimestep = dict([(t, [(a[1], b[1]) for a, b in links if b[0] == int(t)]) for t in timesteps]) assert (len(linksPerTimestep['0']) == 0) # create output array resultVolume = np.zeros((len(timesteps), ) + shape, dtype='uint32') print("resulting volume shape: {}".format(resultVolume.shape)) progressBar = ProgressBar(stop=len(timesteps)) progressBar.show(0) # iterate over timesteps and label tracks from front to back in a distinct color nextUnusedColor = 1 lastFrameColorMap = {} lastFrameLabelImage = getLabelImageForFrame(args.labelImageFilename, args.labelImagePath, 0, shape) for t in range(1, len(timesteps)): progressBar.show() thisFrameColorMap = {} thisFrameLabelImage = getLabelImageForFrame(args.labelImageFilename, args.labelImagePath, t, shape)
def getLargeSeeds(raw, pmap, membraneWidth, threshold, rank, visu=False): with vigra.Timer("ballRankOrderFilter"): # make mebrane wider with ballRankOrderFilter r = int(membraneWidth * 0.35 + 0.5) r = max(r, 1) widerPmap1 = denoise.ballRankOrderFilter(pmap, radius=r, rank=rank) widerPmap = denoise.ballRankOrderFilter(widerPmap1, radius=r, rank=rank) widerPmap1 = None with vigra.Timer("normalize"): # renormalize widerPmap -= widerPmap.min() widerPmap /= widerPmap.max() with vigra.Timer("binarize"): # binarize binaryPmap = numpy.zeros(widerPmap.shape, dtype='uint8') binaryPmap[pmap > threshold] = 1 #widerPmap = None # save mem if visu == False: widerPmap = None with vigra.Timer("multiBinaryDilation"): # morphology # 1) make membrane wider by r r = int(membraneWidth * 1.2 + 0.5) r = max(r, 2) mBinaryPmapA = vigra.filters.multiBinaryDilation(binaryPmap, r) #binaryPmap = None # save mem if visu == False: binaryPmap = None #with vigra.Timer("multiBinaryErosion"): # # morphology # # 1) make membrane smaller by r # r = int(membraneWidth*0.1 + 0.5) # r = max(r, 1) # mBinaryPmapB = vigra.filters.multiBinaryErosion(mBinaryPmapA,r) # if visu == False: # mBinaryPmapA = None with vigra.Timer("labelVolumeWithBackground"): # get seeds invertedBinaryPmap = 1 - mBinaryPmapA if visu == False: mBinaryPmapB = None invertedBinaryPmap = numpy.require(invertedBinaryPmap, dtype='uint32') ccImg = vigra.analysis.labelVolumeWithBackground(invertedBinaryPmap) if visu == False: invertedBinaryPmap = None if visu: grayData = [ (raw, "raw"), (pmap, "pmap"), (widerPmap, "widerPmap"), (binaryPmap, "binaryPmap"), (mBinaryPmapA, "mBinaryPmapA"), #(mBinaryPmapB,"mBinaryPmapB"), ] segData = [ (ccImg, "seeds"), #(seg, "seg") ] skneuro.addHocViewer(grayData, segData) return ccImg
for v in uuidToTraxelMap.values(): prev = None for timestepIdTuple in v: if prev is not None: links.append((prev, timestepIdTuple)) prev = timestepIdTuple # group by timestep # timesteps = [t for t in traxelIdPerTimestepToUniqueIdMap.keys()] # there might be empty frames. We want them as output too. timesteps = [str(t).decode("utf-8") for t in range(int(min(traxelIdPerTimestepToUniqueIdMap.keys())) , int(max(traxelIdPerTimestepToUniqueIdMap.keys()))+1 )] linksPerTimestep = dict([(t, [(a[1], b[1]) for a, b in links if b[0] == int(t)]) for t in timesteps]) assert(len(linksPerTimestep['0']) == 0) # create output array resultVolume = np.zeros((len(timesteps),) + shape, dtype='uint32') print("resulting volume shape: {}".format(resultVolume.shape)) progressBar = ProgressBar(stop=len(timesteps)) progressBar.show(0) # iterate over timesteps and label tracks from front to back in a distinct color nextUnusedColor = 1 lastFrameColorMap = {} lastFrameLabelImage = getLabelImageForFrame(args.labelImageFilename, args.labelImagePath, 0, shape) for t in range(1,len(timesteps)): progressBar.show() thisFrameColorMap = {} thisFrameLabelImage = getLabelImageForFrame(args.labelImageFilename, args.labelImagePath, t, shape) for a, b in linksPerTimestep[str(t)]: # propagate color if possible, otherwise assign a new one if a in lastFrameColorMap:
if args.overwrite_channel is not None: threshold_channel = args.overwrite_channel print("Using user-provided channel: {}".format(threshold_channel)) else: print("Using channel from project-file: {}".format(threshold_channel)) print("Found PredictionMaps of shape {} (txyzc), using channel {}".format( predictionMaps.shape, threshold_channel)) progressBar = ProgressBar(stop=shape[0]) progressBar.show(0) with h5py.File(args.out, 'w') as h5file: if args.single_volume: # loop over timesteps fullLabelImage = np.zeros( [shape[0], shape[3], shape[2], shape[1], 1], dtype='uint32') for t in range(shape[0]): # smooth, threshold, and size filter from prediction map framePrediction = predictionMaps[t, ..., threshold_channel].squeeze() assert ndim == len(framePrediction.shape) smoothedFramePrediction = vigra.filters.gaussianSmoothing( framePrediction.astype('float32'), threshold_sigmas[:ndim]) foreground = np.zeros(smoothedFramePrediction.shape, dtype='uint32') foreground[smoothedFramePrediction > threshold_level] = 1 if ndim == 2: labelImage = vigra.analysis.labelImageWithBackground( foreground)
threshold_channel = args.overwrite_channel print("Using user-provided channel: {}".format(threshold_channel)) else: print("Using channel from project-file: {}".format(threshold_channel)) print("Found PredictionMaps of shape {} (txyzc), using channel {}".format( predictionMaps.shape, threshold_channel)) progressBar = ProgressBar(stop=shape[0]) progressBar.show(0) with h5py.File(args.out, 'w') as h5file: if args.single_volume: # loop over timesteps fullLabelImage = np.zeros([shape[0], shape[3], shape[2], shape[1], 1], dtype='uint32') for t in range(shape[0]): # smooth, threshold, and size filter from prediction map framePrediction = predictionMaps[t, ..., threshold_channel].squeeze() assert ndim == len(framePrediction.shape) smoothedFramePrediction = vigra.filters.gaussianSmoothing(framePrediction.astype('float32'), threshold_sigmas[:ndim]) foreground = np.zeros(smoothedFramePrediction.shape, dtype='uint32') foreground[smoothedFramePrediction > threshold_level] = 1 if ndim == 2: labelImage = vigra.analysis.labelImageWithBackground(foreground) else: labelImage = vigra.analysis.labelVolumeWithBackground(foreground) # filter too small / too large objects out