private UnaryOutputOperation<ImgPlus<T>, ImgPlus<V>> getSlidingOperation( final ImgPlus<T> img, final V type) { final Shape neighborhood = NeighborhoodType.getNeighborhood( NeighborhoodType.valueOf(m_neighborhoodType.getStringValue()), m_intervalExtend.getIntValue()); final OutOfBoundsFactory<T, ImgPlus<T>> outStrat = OutOfBoundsStrategyFactory.<T, ImgPlus<T>>getStrategy( m_outOfBoundsStrategy.getStringValue(), img.firstElement()); return getSlidingOperation(img, type, neighborhood, outStrat); }
@Override protected UnaryOutputOperation<ImgPlus<T>, ImgPlus<V>> op(final ImgPlus<T> vin) { final V outType = getOutType(vin.firstElement().createVariable()); return getSlidingOperation(vin, outType); }
/** * @param img * @param processorFactory * @param converter * @return wrapped {@link ImagePlus} */ @SuppressWarnings({"unchecked", "rawtypes"}) public static final <T extends RealType<T>> ImagePlus wrap( final ImgPlus<T> img, final ImageProcessorFactory processorFactory, final Converter<T, FloatType> converter) { // we always want to have 5 dimensions final RandomAccessibleInterval permuted = extendAndPermute(img); final int width = (int) permuted.dimension(0); final int height = (int) permuted.dimension(1); final ImagePlus r = new ImagePlus(); final ImageStack is = new ImageStack(width, height); final RandomAccessibleInterval<T> access = img.iterationOrder().equals(((IterableRealInterval<?>) permuted).iterationOrder()) ? img : permuted; final IntervalIterator ii = createIntervalIterator(access); final long[] min = new long[access.numDimensions()]; final long[] max = new long[access.numDimensions()]; max[0] = permuted.max(0); max[1] = permuted.max(1); // number of planes = num tasks int numSlices = 1; for (int d = 2; d < access.numDimensions(); d++) { numSlices *= access.dimension(d); } // parallelization final ImageProcessor[] slices = new ImageProcessor[numSlices]; final ExecutorService service = new ThreadPoolExecutorService( KNIMEConstants.GLOBAL_THREAD_POOL.createSubPool(KNIPConstants.THREADS_PER_NODE)); final ArrayList<Future<Void>> futures = new ArrayList<Future<Void>>(); final T inType = img.firstElement(); int i = 0; while (ii.hasNext()) { ii.fwd(); for (int d = 2; d < ii.numDimensions(); d++) { min[d] = ii.getIntPosition(d); max[d] = min[d]; } final int proxy = i++; futures.add( service.submit( new Callable<Void>() { final FinalInterval tmp = new FinalInterval(min, max); @Override public Void call() throws Exception { final Cursor<T> cursor = Views.iterable(Views.interval(access, tmp)).cursor(); final ImageProcessor ip = processorFactory.createProcessor(width, height, inType); final FloatType outProxy = new FloatType(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { converter.convert(cursor.next(), outProxy); ip.setf(x, y, outProxy.get()); } } slices[proxy] = ip; return null; } })); } for (final Future<Void> f : futures) { try { f.get(); } catch (final InterruptedException e) { e.printStackTrace(); } catch (final ExecutionException e) { e.printStackTrace(); } } // add slices to stack for (ImageProcessor slice : slices) { is.addSlice("", slice); } // set calibration final double[] newCalibration = getNewCalibration(img); Calibration cal = new Calibration(); cal.pixelWidth = newCalibration[0]; cal.pixelHeight = newCalibration[1]; cal.pixelDepth = newCalibration[3]; r.setCalibration(cal); r.setStack( is, (int) permuted.dimension(2), (int) permuted.dimension(3), (int) permuted.dimension(4)); r.setTitle(img.getName()); return r; }
/** * Wraps an {@link Img} using default IJ1Converter. * * @param img to be wrapped * @return wrapped {@link ImagePlus} */ public static final <T extends RealType<T>> ImagePlus wrap(final ImgPlus<T> img) { return wrap( img, new DefaultProcessorFactory(), new DefaultImgToIJ1Converter<T>(img.firstElement())); }