use of mpicbg.imglib.type.numeric.integer.UnsignedByteType in project TrakEM2 by trakem2.
the class IntegralImageMipMaps method wrap.
private static final Image<UnsignedByteType> wrap(final byte[] p, final int[] dims) {
final Array<UnsignedByteType, ByteArray> c = new Array<UnsignedByteType, ByteArray>(new ArrayContainerFactory(), new ByteArray(p), dims, 1);
final UnsignedByteType t = new UnsignedByteType(c);
c.setLinkedType(t);
return new Image<UnsignedByteType>(c, t);
}
use of mpicbg.imglib.type.numeric.integer.UnsignedByteType in project TrakEM2 by trakem2.
the class IntegralImageMipMaps method createGRAY8.
@SuppressWarnings({ "unused", "unchecked", "null" })
private static final BufferedImage[] createGRAY8(final Patch patch, final ByteProcessor ip, final ByteProcessor mask) {
final int w = ip.getWidth();
final int h = ip.getHeight();
final int[] dims = new int[] { w, h };
final ScaleAreaAveraging2d<LongType, UnsignedByteType> saai, saam;
{
// Integral of the image
final IntegralImage<UnsignedByteType, LongType> oa = new IntegralImage<UnsignedByteType, LongType>(wrap((byte[]) ip.getPixels(), dims), new LongType(), new IntegerTypeConverter<UnsignedByteType, LongType>());
oa.process();
saai = new ScaleAreaAveraging2d<LongType, UnsignedByteType>(oa.getResult(), new UnsignedByteType(), dims);
// Integral of the mask, if any
if (null != mask) {
final IntegralImage<UnsignedByteType, LongType> ma = new IntegralImage<UnsignedByteType, LongType>(wrap((byte[]) mask.getPixels(), dims), new LongType(), new IntegerTypeConverter<UnsignedByteType, LongType>());
ma.process();
saam = new ScaleAreaAveraging2d<LongType, UnsignedByteType>(ma.getResult(), new UnsignedByteType(), dims);
} else {
saam = null;
}
}
// Generate images
final BufferedImage[] bis = new BufferedImage[Loader.getHighestMipMapLevel(patch) + 1];
//
if (null == saam) {
// mask is null
// Save images as grayscale
// sharing the byte[]
bis[0] = ImageSaver.createGrayImage((byte[]) ip.getPixels(), w, h);
for (int i = 1; i < bis.length; i++) {
final int K = (int) Math.pow(2, i), wk = w / K, hk = h / K;
// An image of the scaled size
saai.setOutputDimensions(wk, hk);
saai.process();
bis[i] = ImageSaver.createGrayImage(((Array<UnsignedByteType, ByteArray>) saai.getResult().getContainer()).update(null).getCurrentStorageArray(), wk, hk);
}
} else {
// Save images as RGBA, where all 3 color channels are the same
bis[0] = ImageSaver.createARGBImage(blend((byte[]) ip.getPixels(), (byte[]) mask.getPixels()), w, h);
for (int i = 1; i < bis.length; i++) {
final int K = (int) Math.pow(2, i), wk = w / K, hk = h / K;
// An image of the scaled size
saai.setOutputDimensions(wk, hk);
saai.process();
// A mask of the scaled size
saam.setOutputDimensions(wk, hk);
saam.process();
//
bis[i] = ImageSaver.createARGBImage(blend(((Array<UnsignedByteType, ByteArray>) saai.getResult().getContainer()).update(null).getCurrentStorageArray(), ((Array<UnsignedByteType, ByteArray>) saam.getResult().getContainer()).update(null).getCurrentStorageArray()), wk, hk);
}
}
return bis;
}
use of mpicbg.imglib.type.numeric.integer.UnsignedByteType in project TrakEM2 by trakem2.
the class IntegralImageMipMaps method saa.
private static final ScaleAreaAveraging2d<LongType, UnsignedByteType> saa(final byte[] b, final int[] dims) {
final IntegralImage<UnsignedByteType, LongType> ii = new IntegralImage<UnsignedByteType, LongType>(wrap(b, dims), new LongType(), new IntegerTypeConverter<UnsignedByteType, LongType>());
ii.process();
return new ScaleAreaAveraging2d<LongType, UnsignedByteType>(ii.getResult(), new UnsignedByteType(), dims);
}
use of mpicbg.imglib.type.numeric.integer.UnsignedByteType in project TrakEM2 by trakem2.
the class Patch method addAlphaMaskLocal.
/**
* Add the given area, in local coordinates, to the alpha mask, using the given fill value.
*/
public void addAlphaMaskLocal(final Area aLocal, int value) {
if (value < 0)
value = 0;
if (value > 255)
value = 255;
//
CoordinateTransform ct = null;
if (hasCoordinateTransform() && null == (ct = getCT())) {
return;
}
// When the area is larger than the image, sometimes the area fails to be set at all
// Also, intersection accelerates calls to contains(x,y) for complex polygons
final Area a = new Area(new Rectangle(0, 0, (int) (width + 1), (int) (height + 1)));
a.intersect(aLocal);
if (M.isEmpty(a)) {
Utils.log("ROI does not intersect the active image!");
return;
}
ByteProcessor mask = getAlphaMask();
// Use imglib to bypass all the problems with ShapeROI
// Create a Shape image with background and the Area on it with 'value'
final int background = (null != mask && 255 == value) ? 0 : 255;
final ShapeList<UnsignedByteType> shapeList = new ShapeList<UnsignedByteType>(new int[] { (int) width, (int) height, 1 }, new UnsignedByteType(background));
shapeList.addShape(a, new UnsignedByteType(value), new int[] { 0 });
final mpicbg.imglib.image.Image<UnsignedByteType> shapeListImage = new mpicbg.imglib.image.Image<UnsignedByteType>(shapeList, shapeList.getBackground(), "mask");
ByteProcessor rmask = (ByteProcessor) ImageJFunctions.copyToImagePlus(shapeListImage, ImagePlus.GRAY8).getProcessor();
if (hasCoordinateTransform()) {
// inverse the coordinate transform
final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
final TransformMeshMapping mapping = new TransformMeshMapping(mesh);
rmask = (ByteProcessor) mapping.createInverseMappedImageInterpolated(rmask);
}
if (null == mask) {
// There wasn't a mask, hence just set it
mask = rmask;
} else {
final byte[] b1 = (byte[]) mask.getPixels();
final byte[] b2 = (byte[]) rmask.getPixels();
// Whatever is not background in the new mask gets set on the old mask
for (int i = 0; i < b1.length; i++) {
// background pixel in new mask
if (background == (b2[i] & 0xff))
continue;
// replace old pixel with new pixel
b1[i] = b2[i];
}
}
setAlphaMask(mask);
}
Aggregations