def makeImg(filepaths, pixelType, loadImg, img_dimensions, matrices, cropInterval, preload): dims = Intervals.dimensionsAsLongArray(cropInterval) voldims = [dims[0], dims[1], len(filepaths)] cell_dimensions = [dims[0], dims[1], 1] grid = CellGrid(voldims, cell_dimensions) cellGet = TranslatedSectionGet(filepaths, loadImg, matrices, img_dimensions, cell_dimensions, cropInterval, preload=preload) return LazyCellImg(grid, pixelType(), cellGet), cellGet
# Unpack Cell dimensions: at margins, may be smaller than cell_width, cell_height width, height, _ = cellDims # ignore depth: it's 1 # Read cell from file into a byte array ra = RandomAccessFile(filepaths[z], 'r') read_width = width * bytesPerPixel bytes = zeros(read_width * height, 'b') # Initial offset to the Cell origin offset = (section_width * y + x) * bytesPerPixel n_read = 0 n_pixels = width * height # Read line by line while n_read < n_pixels: ra.seek(offset) ra.read(bytes, n_read, read_width) n_read += read_width offset += section_width * bytesPerPixel # Create a new Cell of the right pixel type return Cell(cellDims, cellMin, createAccess(bytes, bytesPerPixel)) except: print sys.exc_info() finally: if ra: ra.close() lazyImg = LazyCellImg(grid, createType(bytesPerPixel), SectionGet()) IL.wrap(lazyImg, "sections").show() # Extremely slow: each cell is loaded for each pixel read! # Took 20 seconds to read one single section.
] return Cell(self.cell_dimensions, [0, 0, 0, index], extractDataAccess(img, self.cell_dimensions)) first = getStack(timepoint_paths[0]) # One cell per time point dimensions = [ 1 * first.dimension(0), 1 * first.dimension(1), 1 * first.dimension(2), len(timepoint_paths) ] grid = CellGrid(dimensions, dimensions[0:3] + [1]) vol4d = LazyCellImg(grid, first.randomAccess().get().createVariable(), TimePointGet(timepoint_paths)) print dimensions # Visualization option 2: # Create a 4D VirtualStack manually # Need a fast way to copy pixel-wise w = Weaver.method( """ static public final void copy(final Cursor src, final Cursor tgt) { while (src.hasNext()) { src.fwd(); tgt.fwd(); final UnsignedShortType t1 = (UnsignedShortType) src.get(),
cell_width = imp.getWidth() + cell_padding * 2 cell_height = imp.getHeight() + cell_padding * 2 grid = CellGrid([n_cols * cell_width, n_rows * cell_height], [cell_width, cell_height]) print grid # shows: CellGrid( dims = (1572, 1640), cellDims = (131, 164) ) print "numDim:", grid.numDimensions() print "gridDim:", grid.getGridDimensions() print "x, y", grid.gridDimension(0), grid.gridDimension(1) print "imgDim:", grid.getImgDimensions() print "cellDim", grid.cellDimension(0), grid.cellDimension(1) cellMin = zeros(2, 'l') cellDims = zeros(2, 'i') grid.getCellDimensions(0, cellMin, cellDims) print "cellDim 0", cellMin, cellDims grid.getCellDimensions(5, cellMin, cellDims) print "cellDim 5", cellMin, cellDims grid.getCellDimensions(10, cellMin, cellDims) print "cellDim 10", cellMin, cellDims grid.getCellDimensions(20, cellMin, cellDims) print "cellDim 20", cellMin, cellDims # Padding color: 255 is white for 8-bit images getter = MontageSlice(imp, cell_padding, 255, grid) montage = LazyCellImg(grid, getter.t, getter) IL.wrap(montage, "Montage").show()
] return Cell(self.cell_dimensions, [0, 0, 0, index], extractDataAccess(img, self.cell_dimensions)) first = getStack(timepoint_paths[0]) # One cell per time point dimensions = [ 1 * first.dimension(0), 1 * first.dimension(1), 1 * first.dimension(2), len(timepoint_paths) ] grid = CellGrid(dimensions, dimensions[0:3] + [1]) vol4d = LazyCellImg(grid, first.randomAccess().get().createVariable(), TimePointGet(timepoint_paths)) print dimensions # Visualization option 1: # An automatically created 4D VirtualStack #IL.wrap(vol4d, "Volume 4D").show() # Visualization option 2: # Create a 4D VirtualStack manually # Need a fast way to copy pixel-wise w = Weaver.method( """ static public final void copy(final Cursor src, final Cursor tgt) {
for IFD in slices.IFDs: print IFD firstIFD = slices.IFDs[0] print firstIFD width, height = firstIFD["width"], firstIFD["height"] bitDepth = firstIFD["bitDepth"] pixel_type = { 8: UnsignedByteType, 16: UnsignedShortType, 32: FloatType }[bitDepth] grid = CellGrid([width, height, len(slices.IFDs)], [width, height, 1]) # The whole TIFF file as one Cell per slice, independently loadable imgTIFF = LazyCellImg(grid, pixel_type(), slices) # The whole file #imp = IL.wrap(imgTIFF, os.path.basename(filepath)) #imp.show() # Pick only from slices 3 to 6 view_3_to_6 = Views.interval(imgTIFF, [0, 0, 0], [width - 1, height - 1, 5]) imp_3_to_6 = IL.wrap(view_3_to_6, "3 to 6") imp_3_to_6.show() # Pick only every 3rd slice #view_every_3 = Views.subsample(imgTIFF, [1, 1, 3]) #imp_every_3 = IL.wrap(view_every_3, "every 3") #imp_every_3.show()
def makeCell(self, index): n_cols = self.grid.imgDimension(0) / self.grid.cellDimension(0) x0 = (index % n_cols) * self.grid.cellDimension(0) y0 = (index / n_cols) * self.grid.cellDimension(1) index += 1 # 1-based slice indices in ij.ImageStack if index < 1 or index > self.imp.getStack().size(): # Return blank image: a ByteAccess that always returns 255 return Cell( self.cell_dimensions, [x0, y0], type('ConstantValue', (ByteAccess, ), { 'getValue': lambda self, index: 255 })()) else: return Cell( self.cell_dimensions, [x0, y0], ByteArray(self.imp.getStack().getProcessor(index).getPixels())) n_cols = 12 n_rows = 10 cell_width = imp.getWidth() cell_height = imp.getHeight() grid = CellGrid([n_cols * cell_width, n_rows * cell_height], [cell_width, cell_height]) montage = LazyCellImg(grid, img.cursor().next().createVariable(), SliceGet(imp, grid)) IL.show(montage, "Montage")