List of usage examples for org.opencv.core Mat channels
public int channels()
From source file:com.sikulix.api.Image.java
License:Open Source License
private static long getMatSize(Mat mat) { return mat.channels() * mat.width() * mat.height(); }
From source file:com.sikulix.core.Finder.java
License:Open Source License
private static void printMatI(Mat mat) { int[] data = new int[mat.channels()]; for (int r = 0; r < mat.rows(); r++) { for (int c = 0; c < mat.cols(); c++) { mat.get(r, c, data);/*from w w w . j av a2 s .co m*/ log.trace("(%d, %d) %s", r, c, Arrays.toString(data)); } } }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
/** * The corresponding Java primitive array type depends on the Mat type: * CV_8U and CV_8S -> byte[]//from w ww. j a va 2s .co m * CV_16U and CV_16S -> short[] * CV_32S -> int[] * CV_32F -> float[] * CV_64F-> double[] */ static byte[] getByteArray(final Mat mat) { if (CvType.CV_8UC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_8UC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_byteBuff.length != size) { _byteBuff = new byte[size]; } mat.get(0, 0, _byteBuff); // 0 for row and col means the WHOLE Matrix return _byteBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static int[] getIntArray(final Mat mat) { if (CvType.CV_32SC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_32SC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_intBuff.length != size) { _intBuff = new int[size]; }/* w w w.jav a 2 s .co m*/ mat.get(0, 0, _intBuff); // 0 for row and col means the WHOLE Matrix return _intBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static float[] getFloatArray(final Mat mat) { if (CvType.CV_32FC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_32FC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_floatBuff.length != size) { _floatBuff = new float[size]; }// w w w .j a v a2 s . c o m mat.get(0, 0, _floatBuff); // 0 for row and col means the WHOLE Matrix return _floatBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static double[] getDoubleArray(final Mat mat) { if (CvType.CV_64F != mat.type()) throw new IllegalArgumentException( "Expected type is CV_64F, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_doubleBuff.length != size) { _doubleBuff = new double[size]; }/*from ww w . java 2s. c o m*/ mat.get(0, 0, _doubleBuff); // 0 for row and col means the WHOLE Matrix return _doubleBuff; }
From source file:com.ttolley.pongbot.controller.CvPanel.java
/** * Converts/writes a Mat into a BufferedImage. * * @param matrix Mat of type CV_8UC3 or CV_8UC1 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY *///from w ww .j a v a2 s. co m public BufferedImage matToBufferedImage(Mat matrix) { int cols = matrix.cols(); int rows = matrix.rows(); int elemSize = (int) matrix.elemSize(); byte[] data = new byte[cols * rows * elemSize]; int type; matrix.get(0, 0, data); switch (matrix.channels()) { case 1: type = BufferedImage.TYPE_BYTE_GRAY; break; case 3: type = BufferedImage.TYPE_3BYTE_BGR; // bgr to rgb byte b; for (int i = 0; i < data.length; i = i + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } BufferedImage image2 = new BufferedImage(cols, rows, type); image2.getRaster().setDataElements(0, 0, cols, rows, data); return image2; }
From source file:com.untref.gui.Editar.java
public static BufferedImage matToBufferedImage(Mat matrix) { BufferedImage bimg = new BufferedImage(1, 1, 1); if (matrix != null) { int cols = matrix.cols(); int rows = matrix.rows(); int elemSize = (int) matrix.elemSize(); byte[] data = new byte[cols * rows * elemSize]; int type; matrix.get(0, 0, data);/*from w w w. java 2 s . c o m*/ switch (matrix.channels()) { case 1: type = BufferedImage.TYPE_BYTE_GRAY; break; case 3: type = BufferedImage.TYPE_3BYTE_BGR; // bgr to rgb byte b; for (int i = 0; i < data.length; i = i + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } // Reuse existing BufferedImage if possible if (bimg == null || bimg.getWidth() != cols || bimg.getHeight() != rows || bimg.getType() != type) { bimg = new BufferedImage(cols, rows, type); } bimg.getRaster().setDataElements(0, 0, cols, rows, data); } else { // mat was null bimg = null; } return bimg; }
From source file:cv.recon.util.MatFXUtils.java
License:Open Source License
/** * Convert from OpenCV Mat to JavaFX WritableImage to be displayed in * ImageView./* ww w . j av a 2 s. c o m*/ * @param mat Mat to be converted * @param writableImage Optional WritableImage, if non-null, the Mat will be * written in this WritableImage * @return A WritableImage to be used for JavaFX, return null if already * supplied with WritableImage */ public static WritableImage toFXImage(Mat mat, WritableImage writableImage) { int width = mat.width(); int height = mat.height(); int channels = mat.channels(); byte[] sourcePixels = new byte[width * height * channels]; mat.get(0, 0, sourcePixels); BufferedImage bufferedImage; if (mat.channels() > 1) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); if (writableImage == null) { WritableImage outputImage = SwingFXUtils.toFXImage(bufferedImage, null); return outputImage; } else { SwingFXUtils.toFXImage(bufferedImage, writableImage); return null; } }
From source file:cx.uni.jk.mms.iaip.mat.MatModel.java
License:Open Source License
/** * Loads an image from a file into this model. * /* w w w . j a v a 2 s . c o m*/ * The image file type must be supported by ImageIO and must be 8 bit gray * scale due to limitations of the used methods. The image must be of even * width and even height in order to be processed by OpenCV's DCT/IDCT * methods. * * This implementation uses {@link Path} instead of {@link File} in order to * read the jar from the inside. * * @param path * @throws IllegalSizeException * @throws IOException * @throws UnsupportedImageTypeException */ public void loadImage(Path path) throws IllegalSizeException, IOException, UnsupportedImageTypeException { this.logger .fine(String.format("MatModel \"%s\" loading iamge from path %s", this.getName(), path.toString())); Mat matRead = null; matRead = this.loadAndDecodeImageWithJavaImageIO(path); // matRead = loadImageWithJavaImageIOAndDecodeWithOpenCV(path); // matRead = loadImageWithOpenCV(path); this.logger.finer("image type = " + matRead.type()); this.logger.finer("image channels = " + matRead.channels()); this.logger.finer("image depth = " + matRead.depth()); /** images must have size larger than 0x0 */ if (matRead.width() <= 0 || matRead.height() <= 0) { throw new IllegalSizeException("Image must have width and height > 0."); } /** dct images must have odd width or height */ if (matRead.width() % 2 == 1 || matRead.height() % 2 == 1) { throw new IllegalSizeException("Image must have even width and even height to perform DCT/IDCT."); } /** we need a float mat to do DCT/IDCT */ this.mat = matRead; // just a reference this.logger.finer("convert to internal format"); this.mat.convertTo(this.mat, MAT_TYPE); this.logger.finer("image type = " + this.mat.type()); this.logger.finer("image channels = " + this.mat.channels()); this.logger.finer("image depth = " + this.mat.depth()); /** remember last file loaded successfully */ this.lastPath = path; }