@Override @SuppressWarnings("unchecked") public O calculate(final I kernel, final Dimensions paddedDimensions) { Dimensions paddedFFTInputDimensions; // if an fftsize op has been set recompute padded size if (fftSizeOp != null) { long[][] sizes = fftSizeOp.calculate(paddedDimensions); paddedFFTInputDimensions = new FinalDimensions(sizes[0]); } else { paddedFFTInputDimensions = paddedDimensions; } // compute where to place the final Interval for the kernel so that the // coordinate in the center // of the kernel is shifted to position (0,0). final Interval kernelConvolutionInterval = paddingIntervalCentered.calculate(kernel, paddedFFTInputDimensions); final Interval kernelConvolutionIntervalOrigin = paddingIntervalOrigin.calculate(kernel, kernelConvolutionInterval); return (O) Views.interval( Views.extendPeriodic( Views.interval( Views.extendValue(kernel, Util.getTypeFromInterval(kernel).createVariable()), kernelConvolutionInterval)), kernelConvolutionIntervalOrigin); }
/** * Walk through an Interval on an Img using a Cursor * * @param img * @param interval */ protected static void walkThrough(final Img<IntType> img, final Interval interval) { final Cursor<IntType> c = Views.interval(img, interval).cursor(); int i = 0; while (c.hasNext()) { c.fwd(); i += c.get().get(); } j = i; }
@Test public void testRandomAccessibleView() { @SuppressWarnings("unchecked") final RandomAccessible<ByteType> res = (RandomAccessible<ByteType>) ops.run(MapViewRandomAccessToRandomAccess.class, in, op, new ByteType()); final Cursor<ByteType> iterable = Views.iterable(Views.interval(res, in)).cursor(); while (iterable.hasNext()) { assertEquals((byte) 10, iterable.next().get()); } }
/** * Walk through an Interval on an Img using a LocalizingCursor, localizing on every step. * * @param img * @param interval */ protected static void localizingWalkThrough(final Img<IntType> img, final Interval interval) { final Cursor<IntType> c = Views.interval(img, interval).localizingCursor(); final long[] pos = new long[interval.numDimensions()]; int i = 0; while (c.hasNext()) { c.fwd(); i += c.get().get(); c.localize(pos); } j = (int) pos[0] + i; }
public static <T extends Type<T> & Comparable<T>> int countLocalMaxima( final RandomAccessibleInterval<T> img, final Shape shape) { int nMaxima = 0; final RandomAccessibleInterval<T> source = Views.interval(img, Intervals.expand(img, -1)); final Cursor<T> center = Views.iterable(source).cursor(); A: for (final Neighborhood<T> neighborhood : shape.neighborhoods(source)) { final T c = center.next(); for (final T t : neighborhood) if (t.compareTo(c) > 0) continue A; ++nMaxima; } return nMaxima; }
public static <T extends Type<T> & Comparable<T>> int findLocalMaximaNeighborhood2( final RandomAccessibleInterval<T> img) { // final ArrayList< Point > maxima = new ArrayList< Point >(); int nMaxima = 0; final Cursor<T> center = Views.iterable(Views.interval(img, Intervals.expand(img, -1))).localizingCursor(); final LocalNeighborhood2<T> neighborhood = new LocalNeighborhood2<T>(img, center); final LocalNeighborhoodCursor2<T> nc = neighborhood.cursor(); A: while (center.hasNext()) { final T t = center.next(); neighborhood.updateCenter(center); nc.reset(); while (nc.hasNext()) { final T n = nc.next(); if (n.compareTo(t) > 0) continue A; } // maxima.add( new Point( center ) ); ++nMaxima; } return nMaxima; }
/** * Fuses one stack, i.e. all angles/illuminations for one timepoint and channel * * @param timepoint * @param channel * @return */ public boolean fuseStacksAndGetPSFs( final TimePoint timepoint, final Channel channel, final ImgFactory<FloatType> imgFactory, final int osemIndex, double osemspeedup, WeightType weightType, final HashMap<Channel, ChannelPSF> extractPSFLabels, final long[] psfSize, final HashMap<Channel, ArrayList<Pair<Pair<Angle, Illumination>, String>>> psfFiles, final boolean transformLoadedPSFs) { // TODO: get rid of this hack if (files != null) { weightType = WeightType.LOAD_WEIGHTS; IOFunctions.println("WARNING: LOADING WEIGHTS FROM IMAGES, files.length()=" + files.length); } // get all views that are fused for this timepoint & channel this.viewDescriptions = FusionHelper.assembleInputData(spimData, timepoint, channel, viewIdsToProcess); if (this.viewDescriptions.size() == 0) return false; this.imgs = new HashMap<ViewId, RandomAccessibleInterval<FloatType>>(); this.weights = new HashMap<ViewId, RandomAccessibleInterval<FloatType>>(); final Img<FloatType> overlapImg; if (weightType == WeightType.WEIGHTS_ONLY) overlapImg = imgFactory.create(bb.getDimensions(), new FloatType()); else overlapImg = null; final boolean extractPSFs = (extractPSFLabels != null) && (extractPSFLabels.get(channel).getLabel() != null); final boolean loadPSFs = (psfFiles != null); if (extractPSFs) ePSF = new ExtractPSF<FloatType>(); else if (loadPSFs) ePSF = loadPSFs(channel, viewDescriptions, psfFiles, transformLoadedPSFs); else { ePSF = assignOtherChannel(channel, extractPSFLabels); } if (ePSF == null) return false; // remember the extracted or loaded PSFs extractPSFLabels.get(channel).setExtractPSFInstance(ePSF); // we will need to run some batches until all is fused for (int i = 0; i < viewDescriptions.size(); ++i) { final ViewDescription vd = viewDescriptions.get(i); IOFunctions.println( "Transforming view " + i + " of " + (viewDescriptions.size() - 1) + " (viewsetup=" + vd.getViewSetupId() + ", tp=" + vd.getTimePointId() + ")"); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Reserving memory for transformed & weight image."); // creating the output RandomAccessibleInterval<FloatType> transformedImg; // might be null if WEIGHTS_ONLY final RandomAccessibleInterval<FloatType> weightImg; // never null (except LOAD_WEIGHTS which is not implemented yet) if (weightType == WeightType.WEIGHTS_ONLY) transformedImg = overlapImg; else transformedImg = imgFactory.create(bb.getDimensions(), new FloatType()); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Transformed image factory: " + imgFactory.getClass().getSimpleName()); // loading the input if necessary final RandomAccessibleInterval<FloatType> img; if (weightType == WeightType.WEIGHTS_ONLY && !extractPSFs) { img = null; } else { IOFunctions.println("(" + new Date(System.currentTimeMillis()) + "): Loading image."); img = ProcessFusion.getImage(new FloatType(), spimData, vd, true); if (Img.class.isInstance(img)) IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Input image factory: " + ((Img<FloatType>) img).factory().getClass().getSimpleName()); } // initializing weights IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Initializing transformation & weights: " + weightType.name()); spimData.getViewRegistrations().getViewRegistration(vd).updateModel(); final AffineTransform3D transform = spimData.getViewRegistrations().getViewRegistration(vd).getModel(); final long[] offset = new long[] {bb.min(0), bb.min(1), bb.min(2)}; if (weightType == WeightType.PRECOMPUTED_WEIGHTS || weightType == WeightType.WEIGHTS_ONLY) weightImg = imgFactory.create(bb.getDimensions(), new FloatType()); else if (weightType == WeightType.NO_WEIGHTS) weightImg = Views.interval( new ConstantRandomAccessible<FloatType>( new FloatType(1), transformedImg.numDimensions()), transformedImg); else if (weightType == WeightType.VIRTUAL_WEIGHTS) { final Blending blending = getBlending(img, blendingBorder, blendingRange, vd); weightImg = new TransformedRealRandomAccessibleInterval<FloatType>( blending, new FloatType(), transformedImg, transform, offset); } else // if ( processType == ProcessType.LOAD_WEIGHTS ) { IOFunctions.println("WARNING: LOADING WEIGHTS FROM: '" + new File(files[i]) + "'"); ImagePlus imp = StackImgLoaderIJ.open(new File(files[i])); weightImg = imgFactory.create(bb.getDimensions(), new FloatType()); StackImgLoaderIJ.imagePlus2ImgLib2Img(imp, (Img<FloatType>) weightImg, false); imp.close(); if (debugImport) { imp = ImageJFunctions.show(weightImg); imp.setTitle("ViewSetup " + vd.getViewSetupId() + " Timepoint " + vd.getTimePointId()); } } // split up into many parts for multithreading final Vector<ImagePortion> portions = FusionHelper.divideIntoPortions( Views.iterable(transformedImg).size(), Threads.numThreads() * 4); // set up executor service final ExecutorService taskExecutor = Executors.newFixedThreadPool(Threads.numThreads()); final ArrayList<Callable<String>> tasks = new ArrayList<Callable<String>>(); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Transforming image & computing weights."); for (final ImagePortion portion : portions) { if (weightType == WeightType.WEIGHTS_ONLY) { final Interval imgInterval = new FinalInterval( ViewSetupUtils.getSizeOrLoad( vd.getViewSetup(), vd.getTimePoint(), spimData.getSequenceDescription().getImgLoader())); final Blending blending = getBlending(imgInterval, blendingBorder, blendingRange, vd); tasks.add( new TransformWeights( portion, imgInterval, blending, transform, overlapImg, weightImg, offset)); } else if (weightType == WeightType.PRECOMPUTED_WEIGHTS) { final Blending blending = getBlending(img, blendingBorder, blendingRange, vd); tasks.add( new TransformInputAndWeights( portion, img, blending, transform, transformedImg, weightImg, offset)); } else if (weightType == WeightType.NO_WEIGHTS || weightType == WeightType.VIRTUAL_WEIGHTS || weightType == WeightType.LOAD_WEIGHTS) { tasks.add(new TransformInput(portion, img, transform, transformedImg, offset)); } else { throw new RuntimeException(weightType.name() + " not implemented yet."); } } try { // invokeAll() returns when all tasks are complete taskExecutor.invokeAll(tasks); } catch (final InterruptedException e) { IOFunctions.println("Failed to compute fusion: " + e); e.printStackTrace(); return false; } taskExecutor.shutdown(); // extract PSFs if wanted if (extractPSFs) { final ArrayList<double[]> llist = getLocationsOfCorrespondingBeads( timepoint, vd, extractPSFLabels.get(channel).getLabel()); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Extracting PSF for viewsetup " + vd.getViewSetupId() + " using label '" + extractPSFLabels.get(channel).getLabel() + "'" + " (" + llist.size() + " corresponding detections available)"); ePSF.extractNextImg(img, vd, transform, llist, psfSize); } if (weightType != WeightType.WEIGHTS_ONLY) imgs.put(vd, transformedImg); weights.put(vd, weightImg); } // normalize the weights final ArrayList<RandomAccessibleInterval<FloatType>> weightsSorted = new ArrayList<RandomAccessibleInterval<FloatType>>(); for (final ViewDescription vd : viewDescriptions) weightsSorted.add(weights.get(vd)); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Computing weight normalization for deconvolution."); final WeightNormalizer wn; if (weightType == WeightType.WEIGHTS_ONLY || weightType == WeightType.PRECOMPUTED_WEIGHTS || weightType == WeightType.LOAD_WEIGHTS) wn = new WeightNormalizer(weightsSorted); else if (weightType == WeightType.VIRTUAL_WEIGHTS) wn = new WeightNormalizer(weightsSorted, imgFactory); else // if ( processType == ProcessType.NO_WEIGHTS ) wn = null; if (wn != null && !wn.process()) return false; // put the potentially modified weights back for (int i = 0; i < viewDescriptions.size(); ++i) weights.put(viewDescriptions.get(i), weightsSorted.get(i)); this.minOverlappingViews = wn.getMinOverlappingViews(); this.avgOverlappingViews = wn.getAvgOverlappingViews(); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Minimal number of overlapping views: " + getMinOverlappingViews() + ", using " + (this.minOverlappingViews = Math.max(1, this.minOverlappingViews))); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Average number of overlapping views: " + getAvgOverlappingViews() + ", using " + (this.avgOverlappingViews = Math.max(1, this.avgOverlappingViews))); if (osemIndex == 1) osemspeedup = getMinOverlappingViews(); else if (osemIndex == 2) osemspeedup = getAvgOverlappingViews(); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Adjusting for OSEM speedup = " + osemspeedup); if (weightType == WeightType.WEIGHTS_ONLY) displayWeights(osemspeedup, weightsSorted, overlapImg, imgFactory); else adjustForOSEM(weights, weightType, osemspeedup); IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): Finished precomputations for deconvolution."); return true; }
@Override public Cursor<T> cursor() { return new RandomAccessibleIntervalCursor<T>(Views.interval(rai, new long[dims.length], dims)); }