def create(self, index): cell_dimensions = [ self.grid.cellDimension(0), self.grid.cellDimension(1) ] n_cols = grid.imgDimension(0) / cell_dimensions[0] x0 = (index % n_cols) * cell_dimensions[0] y0 = (index / n_cols) * cell_dimensions[1] index += 1 # 1-based slice indices in ij.ImageStack if index < 1 or index > self.stack.size(): # Return blank image: a ByteAccess that always returns 255 return Cell( cell_dimensions, [x0, y0], type('ConstantValue', (ByteAccess, ), { 'getValue': lambda self, index: 255 })()) else: # ImageJ stack slice indices are 1-based img = IL.wrap(ImagePlus("", self.stack.getProcessor(index))) # Create extended image with the padding color value imgE = Views.extendValue(img, self.t.copy()) # A view that includes the padding between slices minC = [-self.cell_padding for d in xrange(img.numDimensions())] maxC = [ img.dimension(d) - 1 + self.cell_padding for d in xrange(img.numDimensions()) ] imgP = Views.interval(imgE, minC, maxC) return Cell(cell_dimensions, [x0, y0], ProxyByteAccess(imgP, self.grid))
def get(self, index): ra = None try: # Read cell origin and dimensions for cell at index cellMin = zeros(3, 'l') # long, 3 dimensions cellDims = zeros(3, 'i') # integer, 3 dimensions grid.getCellDimensions(index, cellMin, cellDims) # Unpack Cell origin (in pixel coordinates) x, y, z = cellMin # 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()
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()))
def get(self, index): img = CellLoader.klb.readFull(timepoint_paths[index]).getImg() # Each cell has "1" as its dimension in the last axis (time) # and index as its min coordinate in the last axis (time) return Cell( Intervals.dimensionsAsIntArray(img) + array([1], 'i'), Intervals.minAsLongArray(img) + array([index], 'l'), extractArrayAccess(img))
def get(self, index): img = getStack(self.timepoint_paths[index]) if not self.cell_dimensions: self.cell_dimensions = [ img.dimension(0), img.dimension(1), img.dimension(2), 1 ] return Cell(self.cell_dimensions, [0, 0, 0, index], extractDataAccess(img, self.cell_dimensions))
def get(self, index): IFD = self.IFDs[index] ra = RandomAccessFile(self.filepath, 'r') try: cell_position = [0, 0, index] pixels = read_TIFF_plane(ra, IFD) # a native array access = self.types[IFD["bitDepth"]][0] # e.g. ByteArray, FloatArray ... return Cell(self.cell_dimensions, cell_position, access(pixels)) finally: ra.close()
def makeCell(self, index): self.preloadCells(index) # preload others in the background img = self.loadImg(self.filepaths[index]) affine = AffineTransform2D() affine.set(self.matrices[index]) imgI = Views.interpolate(Views.extendZero(img), NLinearInterpolatorFactory()) imgA = RealViews.transform(imgI, affine) imgT = Views.zeroMin(Views.interval(imgA, self.interval)) aimg = img.factory().create(self.interval) ImgUtil.copy(ImgView.wrap(imgT, aimg.factory()), aimg) return Cell(self.cell_dimensions, [0, 0, index], aimg.update(None))
def get(self, index): """ Assumes: - uncompressed image - one sample per pixel (one channel only) """ IFD = self.IFDs[index] ra = RandomAccessFile(self.filepath, 'r') try: cell_dimensions = [IFD["width"], IFD["height"], 1] cell_position = [0, 0, index] pixels = read_TIFF_plane(ra, IFD) return Cell(cell_dimensions, cell_position, self.types[pixels.typecode](pixels)) finally: ra.close()
def get(self, index): # Load one image: one slice of this virtual stack imp = IJ.openImage(self.filepaths[index]) # If the pixel type was different than that of others, could be converted here # # If image is to be filtered prior to showing it, here is the place # # Wrap the image in an ImgLib2 Cell # Dimensions: one stack slice, so z=1 cell_dimensions = [imp.getWidth(), imp.getHeight(), 1] # Position: at origin of coordinates for X,Y and at index for Z cell_position = [0, 0, index] # Pixel DataAccess: wrap the pixel array access = self.access_type(imp.getProcessor().getPixels()) # return Cell(cell_dimensions, cell_position, access)
def get(self, index): ra = None try: # Read cell origin and dimensions for cell at index cellMin = zeros(3, 'l') # long[3] cellDims = zeros(3, 'i') # integer[3] grid.getCellDimensions(index, cellMin, cellDims) # Unpack Cell origin (in pixel coordinates) x, y, z = cellMin # 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') # will contain the entire Cell pixel data # Initial offset to the Cell origin offset = (section_width * y + x) * bytesPerPixel n_pixels = width * height if width == section_width: # Read whole block in one go: cell data is continuous in the file ra.seek(offset) ra.read(bytes, 0, n_pixels * bytesPerPixel) else: # Read line by line n_read = 0 while n_read < n_pixels: ra.seek(offset) ra.read(bytes, n_read, read_width) n_read += read_width # ensure n_read advances in case file is truncated to avoid infinite loop 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()
def get(self, index): img = self.asArrayImg(index, self.loadFn(self.filepaths[index])) dims = Intervals.dimensionsAsLongArray(img) return Cell( list(dims) + [1], [0] * img.numDimensions() + [index], img.update(None))
def get(self, index): img = self.loader.load(self.filepaths[index]) if self.filterFn: img = self.filterFn(img) return Cell([img.dimension(0), img.dimension(1), 1], [0, 0, index], img.update(None))
def get(self, index): img = self.asArrayImg(index, self.loadFn(self.filepaths[index])) dims = Intervals.dimensionsAsLongArray(img) return Cell(list(dims) + [1], # cell dimensions [0] * img.numDimensions() + [index], # position in the grid: 0, 0, 0, Z-index img.update(None)) # get the underlying DataAccess