def __init__(self, labels): assert labels.min() == 1 self.labels = labels self.cgp = ncgp.TopologicalGrid2D(labels) self.n_cells = self.cgp.numberOfCells self.cell_bounds = self.cgp.extractCellsBounds() self.geometry = self.cgp.extractCellsGeometry() self.filled_tgrid = ncgp.FilledTopologicalGrid2D(self.cgp) # all junction of size 3 j3_labels = [] bounds0 = self.cell_bounds[0] for cell_0_index in range(self.n_cells[0]): if len(bounds0[cell_0_index]) == 3: cell_0_label = cell_0_index + 1 j3_labels.append(cell_0_label) self.j3_labels = j3_labels # build regular graph self.graph = ngraph.UndirectedGraph(self.n_cells[2]) uv_ids_cgp = numpy.array(self.cell_bounds[1])-1 self.graph.insertEdges(uv_ids_cgp) self.cgp_edge_to_graph_edge = self.graph.findEdges(uv_ids_cgp) self.cell_1_labels = self.cell_label_image(cell_type=1) self.bounded_by = { 1:self.cell_bounds[0].reverseMapping(), 2:self.cell_bounds[1].reverseMapping() }
def test_corner_case_3x3_grid_d(self): assertEq = self.assertEqual seg = [[1, 1, 1], [1, 2, 1], [1, 1, 3]] # 01234 # -------------------- # 0 |1|1|1| 0 # 1 |-*-*-| 1 # 2 |1|2|1| 2 # 3 |-*-*-| 3 # 4 |1|1|3| 4 # ---------------------- # 01234 seg = numpy.array(seg, dtype='uint32').T tGrid = ncgp.TopologicalGrid2D(seg) numberOfCells = tGrid.numberOfCells assertEq(numberOfCells, [0, 2, 3]) tShape = tGrid.topologicalGridShape assertEq(tShape, [5, 5]) shape = tGrid.shape assertEq(shape, [3, 3]) # check the bounds bounds = tGrid.extractCellsBounds() bounds0 = bounds[0] bounds1 = bounds[1] boundedBy1 = bounds0.reverseMapping() boundedBy2 = bounds1.reverseMapping() assertEq(len(bounds0), 0) assertEq(len(bounds1), 2) assertEq(len(boundedBy1), 2) assertEq(len(boundedBy2), 3) # check the geometry #print(seg) geometryFS = tGrid.extractCellsGeometry(fill=True, sort1Cells=True) geometryF = tGrid.extractCellsGeometry(fill=True, sort1Cells=False) geometryS = tGrid.extractCellsGeometry(fill=False, sort1Cells=True) geometry = tGrid.extractCellsGeometry(fill=False, sort1Cells=False) geos = [geometryFS, geometryF, geometryS, geometry] for geo in geos: for c in [0, 1, 2]: g = geo[c] assert len(g) == numberOfCells[c]
def test_corner_config(self): assertEq = self.assertEqual # 4 one cells are active # but still no junction seg = [[1, 1, 2], [1, 3, 1], [1, 1, 1]] seg = numpy.array(seg, dtype='uint32') tGrid = ncgp.TopologicalGrid2D(seg) numberOfCells = tGrid.numberOfCells assertEq(numberOfCells, [0, 2, 3]) tShape = tGrid.topologicalGridShape assertEq(tShape, [5, 5]) shape = tGrid.shape assertEq(shape, [3, 3])
def test_randomized_small(self): for x in range(3000): assertEq = self.assertEqual shape = (4, 3) size = shape[0] * shape[1] labels = numpy.random.randint(1, 5, size=size).reshape(shape) #print(labels) gg = nifty.graph.undirectedGridGraph(shape) cc = nifty.graph.connectedComponentsFromNodeLabels( gg, labels.ravel()) cc = cc.reshape(shape) + 1 cc = numpy.require(cc, dtype='uint32') tGrid = ncgp.TopologicalGrid2D(cc) numberOfCells = tGrid.numberOfCells assertEq(numberOfCells[2], cc.max()) # check the bounds bounds = tGrid.extractCellsBounds() boundedBy = { 1: bounds[0].reverseMapping(), 2: bounds[1].reverseMapping(), } try: # check the geometry geometryFS = tGrid.extractCellsGeometry(fill=True, sort1Cells=True) geometryF = tGrid.extractCellsGeometry(fill=True, sort1Cells=False) geometryS = tGrid.extractCellsGeometry(fill=False, sort1Cells=True) geometry = tGrid.extractCellsGeometry(fill=False, sort1Cells=False) geos = [geometryFS, geometryF, geometryS, geometry] except: print(cc) print("labels") print(labels) import sys sys.exit()
def test_corner_case_3x3_grid_a(self): assertEq = self.assertEqual # 4 one cells are active # but still no junction seg = [[1, 1, 2], [1, 3, 1], [1, 1, 1]] seg = numpy.array(seg, dtype='uint32') tGrid = ncgp.TopologicalGrid2D(seg) numberOfCells = tGrid.numberOfCells assertEq(numberOfCells, [0, 2, 3]) tShape = tGrid.topologicalGridShape assertEq(tShape, [5, 5]) shape = tGrid.shape assertEq(shape, [3, 3]) # check the bounds bounds = tGrid.extractCellsBounds() bounds0 = bounds[0] bounds1 = bounds[1] boundedBy1 = bounds0.reverseMapping() boundedBy2 = bounds1.reverseMapping() assertEq(len(bounds0), 0) assertEq(len(bounds1), 2) assertEq(len(boundedBy1), 2) assertEq(len(boundedBy2), 3) # check the geometry geometryFS = tGrid.extractCellsGeometry(fill=True, sort1Cells=True) geometryF = tGrid.extractCellsGeometry(fill=True, sort1Cells=False) geometryS = tGrid.extractCellsGeometry(fill=False, sort1Cells=True) geometry = tGrid.extractCellsGeometry(fill=False, sort1Cells=False) geos = [geometryFS, geometryF, geometryS, geometry] for geo in geos: for c in [0, 1, 2]: g = geo[c] assert len(g) == numberOfCells[c]
numberOfCells = tGrid.numberOfCells assertEq(numberOfCells, [0, 2, 3]) tShape = tGrid.topologicalGridShape assertEq(tShape, [5, 5]) shape = tGrid.shape assertEq(shape, [3, 3]) if False: seg = [[1, 1, 2], [1, 3, 1], [1, 1, 1]] seg = numpy.array(seg, dtype='uint32') tgrid = ncgp.TopologicalGrid2D(seg) ftgrid = ncgp.FilledTopologicalGrid2D(tgrid) numberOfCells = tgrid.numberOfCells tGridArray = numpy.array(tgrid) print(tGridArray) tGridArray = numpy.array(ftgrid) print(tGridArray) cellBounds = ncgp.Bounds2D(tgrid) cell0Bounds = numpy.array(cellBounds[0]) cell1Bounds = numpy.array(cellBounds[1]) cellGeometry = ncgp.Geometry2D(tgrid)
def __init__(self, raw, seg, gt=None, patchSize=[100, 100], **kwargs): #raw input data self.raw = numpy.squeeze(raw) self.seg = numpy.squeeze(seg) self.gt = numpy.squeeze(gt) # shorthands self.shape = self.raw.shape # settings self.patchSize = patchSize # apply paddings ps = self.patchSize padding = ((ps[0], ps[0]), (ps[1], ps[1])) pad = partial(numpy.pad, pad_width=padding) self.paddingMask = pad(numpy.zeros(self.shape), mode='constant', constant_values=1) self.paddedRaw = pad(self.raw, mode='reflect') self.paddedSeg = vigra.analysis.labelImage( pad(self.seg, mode='reflect')).squeeze() self.paddedGt = None if self.gt is not None: self.paddedGt = vigra.analysis.labelImage( pad(self.gt, mode='reflect')).squeeze() #self.paddedGt = pad(self.gt + 1, mode='constant', constant_values=0) # compute cgp self.tGrid = ncgp.TopologicalGrid2D(self.paddedSeg) self.tShape = self.tGrid.topologicalGridShape self.numberOfCells = self.tGrid.numberOfCells self.fGrid = ncgp.FilledTopologicalGrid2D(self.tGrid) self.cellGrids = [ self.fGrid.cellMask([1, 0, 0]), self.fGrid.cellMask([0, 1, 0]), None ] self.cellGrids[0][ self.cellGrids[0] != 0] -= self.fGrid.cellTypeOffset[0] self.cellGrids[1][ self.cellGrids[1] != 0] -= self.fGrid.cellTypeOffset[1] self.cellMasks = [numpy.clip(x, 0, 1) for x in self.cellGrids[0:2]] + [None] rawT = vigra.sampling.resize(self.paddedRaw, self.tShape) #rawT[self.cellMasks[1]!=0] = 0 self.cellsBounds = self.tGrid.extractCellsBounds() self.cellsGeometry = self.tGrid.extractCellsGeometry(fill=True) self.cells1Bounds = self.cellsBounds[1] self.cells1Geometry = self.cellsGeometry[1] # center of mass self.cell1CentersOfMass = self.cells1Geometry.centersOfMass() print(self.cell1CentersOfMass) # compute gt ol = Overlap(self.numberOfCells[2], self.paddedSeg, self.paddedGt) cell1Probs = ol.differentOverlaps(numpy.array(self.cells1Bounds)) with nifty.Timer("makeCellImage"): probImg = ncgp.makeCellImage(rawT, self.cellGrids[1], cell1Probs * 255.0) vigra.imshow(probImg.astype('uint8'), cmap='gist_heat') vigra.show() if False: vigra.imshow(self.paddingMask) vigra.show() vigra.imshow(self.paddedRaw) vigra.show() vigra.segShow(self.paddedRaw, self.paddedSeg) vigra.show() vigra.segShow(self.paddedRaw, self.paddedGt) vigra.show()
# watersheds overseg = nseg.seededWatersheds(edgeIndicator, method='node_weighted', acc='min') f = pylab.figure() f.add_subplot(2, 2, 1) pylab.imshow(img / 255) f.add_subplot(2, 2, 2) pylab.imshow(edgeIndicator) f.add_subplot(2, 2, 3) pylab.imshow(overseg, cmap=nseg.randomColormap(overseg.max() + 1)) f.add_subplot(2, 2, 4) pylab.imshow(nseg.markBoundaries(img, overseg)) pylab.show() # cgp assert overseg.min() == 1 tgrid = ncgp.TopologicalGrid2D(overseg) geometry = tgrid.extractCellsGeometry() bounds = tgrid.extractCellsBounds() boundedBy = {1: bounds[0].reverseMapping(), 2: bounds[1].reverseMapping()} # compute features cell1Features, cell1FeatureNames = ncgp.cell1Features(tgrid=tgrid, geometry=geometry, bounds=bounds, boundedBy=boundedBy) print(cell1Features.shape)