private <T extends RealType<T>> Img<T> wrapToImg( final RandomAccessibleInterval<T> rai) { if (rai instanceof Img) return (Img<T>) rai; return ImgView.wrap(rai, imgFactory(rai)); }
@Override public ImgPlus<T> calculate(final ImgPlus<T> input, final Interval interval) { final ImgPlus<T> out = new ImgPlus<>(ImgView.wrap(cropper.calculate(input, interval), input.factory())); // TODO remove metadata-util MetadataUtil.copyAndCleanImgPlusMetadata(interval, input, out); return out; } }
/** * Adjusts the given {@link Img} to match the bounds of the specified * {@link Interval}. * * @param img An image whose min/max bounds might need adjustment. * @param minMax An {@link Interval} whose min/max bounds to use when * adjusting the image. If the provided {@code minMax} object is not * an {@link Interval}, no adjustment is performed. * @return A wrapped version of the input {@link Img} with bounds adjusted to * match the provided {@link Interval}, if any; or the input image * itself if no adjustment was needed/possible. */ public static <T extends Type<T>> Img<T> adjustMinMax(final Img<T> img, final Object minMax) { if (!(minMax instanceof Interval)) return img; final Interval interval = (Interval) minMax; final long[] min = new long[interval.numDimensions()]; interval.min(min); for (int d = 0; d < min.length; d++) { if (min[d] != 0) { return ImgView.wrap(Views.translate(img, min), img.factory()); } } return img; }
private ImgPlus<UnsignedByteType> splitColorChannels(ImgPlus<ARGBType> input) { Img<ARGBType> colored = input.getImg(); RandomAccessibleInterval<UnsignedByteType> colorStack = Views.stack( Converters.argbChannel( colored, 1 ), Converters.argbChannel( colored, 2 ), Converters.argbChannel( colored, 3 ) ); ImgPlus<UnsignedByteType> result = new ImgPlus<>(ImgView.wrap(colorStack, new PlanarImgFactory<>()), input.getName()); int lastAxis = colored.numDimensions(); for (int i = 0; i < lastAxis; i++) result.setAxis(input.axis(i).copy(), i); result.setAxis(new DefaultLinearAxis(Axes.CHANNEL), lastAxis); return ImgPlusViews.moveAxis(result, lastAxis, 2); }