use of org.bytedeco.javacpp.indexer.UShortIndexer in project qupath by qupath.
the class OpenCVTools method matToBufferedImage.
/**
* Convert a Mat to a BufferedImage.
* <p>
* If no ColorModel is specified, a grayscale model will be used for single-channel 8-bit
* images and RGB/ARGB for 3/4 channel 8-bit images.
* <p>
* For all other cases a ColorModel should be specified for meaningful display.
*
* @param mat
* @param colorModel
* @return
*/
public static BufferedImage matToBufferedImage(final Mat mat, ColorModel colorModel) {
int type;
int bpp = 0;
switch(mat.depth()) {
case opencv_core.CV_8U:
type = DataBuffer.TYPE_BYTE;
bpp = 8;
break;
case opencv_core.CV_8S:
// Byte is unsigned
type = DataBuffer.TYPE_SHORT;
bpp = 16;
break;
case opencv_core.CV_16U:
type = DataBuffer.TYPE_USHORT;
bpp = 16;
break;
case opencv_core.CV_16S:
type = DataBuffer.TYPE_SHORT;
bpp = 16;
break;
case opencv_core.CV_32S:
type = DataBuffer.TYPE_INT;
bpp = 32;
break;
case opencv_core.CV_32F:
type = DataBuffer.TYPE_FLOAT;
bpp = 32;
break;
default:
logger.warn("Unknown Mat depth {}, will default to CV64F ({})", mat.depth(), opencv_core.CV_64F);
case opencv_core.CV_64F:
type = DataBuffer.TYPE_DOUBLE;
bpp = 64;
}
// Create a suitable raster
int width = mat.cols();
int height = mat.rows();
int channels = mat.channels();
// We might generate an image for a special case
BufferedImage img = null;
// Handle some special cases
if (colorModel == null) {
if (type == DataBuffer.TYPE_BYTE) {
if (channels == 1) {
img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
// TODO: Set the bytes
} else if (channels == 3) {
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
} else if (channels == 4) {
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
}
} else if (colorModel instanceof IndexColorModel) {
img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, (IndexColorModel) colorModel);
}
// Create the image
WritableRaster raster;
if (img != null) {
raster = img.getRaster();
} else if (colorModel != null) {
raster = colorModel.createCompatibleWritableRaster(width, height);
img = new BufferedImage(colorModel, raster, false, null);
} else {
// Create some kind of raster we can use
var sampleModel = new BandedSampleModel(type, width, height, channels);
raster = WritableRaster.createWritableRaster(sampleModel, null);
// We do need a ColorModel or some description
colorModel = ColorModelFactory.getDummyColorModel(bpp * channels);
img = new BufferedImage(colorModel, raster, false, null);
}
MatVector matvector = new MatVector();
opencv_core.split(mat, matvector);
// We don't know which of the 3 supported array types will be needed yet...
int[] pixelsInt = null;
float[] pixelsFloat = null;
double[] pixelsDouble = null;
for (int b = 0; b < channels; b++) {
// Extract pixels for the current channel
Mat matChannel = matvector.get(b);
Indexer indexer = matChannel.createIndexer();
if (indexer instanceof UByteIndexer) {
if (pixelsInt == null)
pixelsInt = new int[width * height];
((UByteIndexer) indexer).get(0L, pixelsInt);
} else if (indexer instanceof UShortIndexer) {
if (pixelsInt == null)
pixelsInt = new int[width * height];
((UShortIndexer) indexer).get(0L, pixelsInt);
} else if (indexer instanceof FloatIndexer) {
if (pixelsFloat == null)
pixelsFloat = new float[width * height];
((FloatIndexer) indexer).get(0L, pixelsFloat);
} else if (indexer instanceof DoubleIndexer) {
if (pixelsDouble == null)
pixelsDouble = new double[width * height];
((DoubleIndexer) indexer).get(0L, pixelsDouble);
} else {
if (pixelsDouble == null)
pixelsDouble = new double[width * height];
// This is inefficient, but unlikely to occur too often
pixelsDouble = new double[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelsDouble[y * width + x] = indexer.getDouble(y, x, b);
}
}
}
// Set the samples
if (pixelsInt != null)
raster.setSamples(0, 0, width, height, b, pixelsInt);
else if (pixelsFloat != null)
raster.setSamples(0, 0, width, height, b, pixelsFloat);
else if (pixelsDouble != null)
raster.setSamples(0, 0, width, height, b, pixelsDouble);
}
return img;
}
use of org.bytedeco.javacpp.indexer.UShortIndexer in project qupath by qupath.
the class OpenCVTools method matToImageProcessor.
/**
* Convert a single-channel OpenCV {@code Mat} into an ImageJ {@code ImageProcessor}.
*
* @param mat
* @return
*/
public static ImageProcessor matToImageProcessor(Mat mat) {
if (mat.channels() != 1)
throw new IllegalArgumentException("Only a single-channel Mat can be converted to an ImageProcessor! Specified Mat has " + mat.channels() + " channels");
int w = mat.cols();
int h = mat.rows();
if (mat.depth() == opencv_core.CV_32F) {
FloatIndexer indexer = mat.createIndexer();
float[] pixels = new float[w * h];
indexer.get(0L, pixels);
return new FloatProcessor(w, h, pixels);
} else if (mat.depth() == opencv_core.CV_8U) {
UByteIndexer indexer = mat.createIndexer();
int[] pixels = new int[w * h];
indexer.get(0L, pixels);
ByteProcessor bp = new ByteProcessor(w, h);
for (int i = 0; i < pixels.length; i++) bp.set(i, pixels[i]);
return bp;
} else if (mat.depth() == opencv_core.CV_16U) {
UShortIndexer indexer = mat.createIndexer();
int[] pixels = new int[w * h];
indexer.get(0L, pixels);
short[] shortPixels = new short[pixels.length];
for (int i = 0; i < pixels.length; i++) shortPixels[i] = (short) pixels[i];
// TODO: Test!
return new ShortProcessor(w, h, shortPixels, null);
} else {
Mat mat2 = new Mat();
mat.convertTo(mat2, opencv_core.CV_32F);
ImageProcessor ip = matToImageProcessor(mat2);
mat2.close();
return ip;
}
}
use of org.bytedeco.javacpp.indexer.UShortIndexer in project javacpp by bytedeco.
the class IndexerTest method testUShortIndexer.
@Test
public void testUShortIndexer() {
System.out.println("UShortIndexer");
long size = 7 * 5 * 3 * 2;
long[] sizes = { 7, 5, 3, 2 };
long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
final ShortPointer ptr = new ShortPointer(size);
int start = 0x3000;
for (int i = 0; i < size; i++) {
ptr.position(i).put((short) (i + start));
}
UShortIndexer arrayIndexer = UShortIndexer.create(ptr.position(0), sizes, strides, false);
UShortIndexer directIndexer = UShortIndexer.create(ptr.position(0), sizes, strides, true);
int n = start;
for (int i = 0; i < sizes[0]; i++) {
assertEquals(n, arrayIndexer.get(i));
assertEquals(n, directIndexer.get(i));
for (int j = 0; j < sizes[1]; j++) {
assertEquals(n, arrayIndexer.get(i, j));
assertEquals(n, directIndexer.get(i, j));
for (int k = 0; k < sizes[2]; k++) {
assertEquals(n, arrayIndexer.get(i, j, k));
assertEquals(n, directIndexer.get(i, j, k));
for (int m = 0; m < sizes[3]; m++) {
long[] index = { i, j, k, m };
assertEquals(n, arrayIndexer.get(index));
assertEquals(n, directIndexer.get(index));
arrayIndexer.put(index, 2 * n);
directIndexer.put(index, 3 * n);
n++;
}
}
}
}
try {
arrayIndexer.get(size);
fail("IndexOutOfBoundsException should have been thrown.");
} catch (IndexOutOfBoundsException e) {
}
try {
directIndexer.get(size);
fail("IndexOutOfBoundsException should have been thrown.");
} catch (IndexOutOfBoundsException e) {
}
System.out.println("arrayIndexer" + arrayIndexer);
System.out.println("directIndexer" + directIndexer);
for (int i = 0; i < size; i++) {
assertEquals(3 * (i + start), ptr.position(i).get() & 0xFFFF);
}
arrayIndexer.release();
for (int i = 0; i < size; i++) {
assertEquals(2 * (i + start), ptr.position(i).get() & 0xFFFF);
}
System.gc();
if (Loader.sizeof(Pointer.class) > 4)
try {
long longSize = 0x80000000L + 8192;
final ShortPointer longPointer = new ShortPointer(longSize);
assertEquals(longSize, longPointer.capacity());
UShortIndexer longIndexer = UShortIndexer.create(longPointer);
assertEquals(longIndexer.pointer(), longPointer);
for (long i = 0; i < 8192; i++) {
longPointer.put(longSize - i - 1, (short) i);
}
for (long i = 0; i < 8192; i++) {
assertEquals(longIndexer.get(longSize - i - 1), i & 0xFFFF);
}
System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
} catch (OutOfMemoryError e) {
System.out.println(e);
}
System.out.println();
}
use of org.bytedeco.javacpp.indexer.UShortIndexer in project qupath by qupath.
the class OpenCVTools method putPixels.
/**
* Put the pixels for the specified raster into a preallocated Mat.
*
* @param raster
* @param mat
*/
private static void putPixels(WritableRaster raster, Mat mat) {
Indexer indexer = mat.createIndexer();
if (indexer instanceof UByteIndexer)
putPixels(raster, (UByteIndexer) indexer);
else if (indexer instanceof ShortIndexer)
putPixels(raster, (ShortIndexer) indexer);
else if (indexer instanceof UShortIndexer)
putPixels(raster, (UShortIndexer) indexer);
else if (indexer instanceof FloatIndexer)
putPixels(raster, (FloatIndexer) indexer);
else {
double[] pixels = null;
int width = raster.getWidth();
int height = raster.getHeight();
long[] indices = new long[3];
for (int b = 0; b < raster.getNumBands(); b++) {
pixels = raster.getSamples(0, 0, width, height, b, pixels);
indices[2] = b;
for (int y = 0; y < height; y++) {
indices[0] = y;
for (int x = 0; x < width; x++) {
indices[1] = x;
indexer.putDouble(indices, pixels[y * width + x]);
}
}
}
}
indexer.release();
}
Aggregations