List of usage examples for org.opencv.core Mat get
public int get(int row, int col, double[] data)
From source file:digimesh.xbee.gui.MeshGui.java
private BufferedImage Mat2BufferedImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }// w ww. j a v a 2 s . c o m int bufferSize = m.channels() * m.cols() * m.rows(); byte[] b = new byte[bufferSize]; m.get(0, 0, b); // get all the pixels BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; }
From source file:digitalassistant.Panel.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 w w. j av a 2s.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 image = new BufferedImage(cols, rows, type); image.getRaster().setDataElements(0, 0, cols, rows, data); return image; }
From source file:digitalassistant.Panel.java
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;/*from w w w . ja va2 s .c o m*/ 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 image = new BufferedImage(cols, rows, type); image.getRaster().setDataElements(0, 0, cols, rows, data); return image; }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
public int[] polySearch(Point point, Mat hierarchy, List<MatOfPoint> contours, int current) { // first index is whether the point is contained and second is which children has it. // Third is who is next int[] out = new int[3]; int[] hEntry = new int[4]; hierarchy.get(0, current, hEntry); int nextChild = hEntry[2], nextSibling = hEntry[0]; // If point is not within current contour return -1 if (Imgproc.pointPolygonTest(new MatOfPoint2f(contours.get(current).toArray()), point, false) < 0) { out[0] = -1;//from w ww . java 2 s. c o m out[1] = -1; } else { //Otherwise check if contours has children containing the point out[0] = 1; //Depth first search int[] ret = new int[3]; int childrenCount = 0; while (nextChild != -1) { ret = polySearch(point, hierarchy, contours, nextChild); childrenCount++; nextChild = ret[2]; } //If there is only one children return it. if (childrenCount == 1) { out[1] = ret[1]; } else {//If more than one children are contained returned the parent out[1] = current; } } out[2] = nextSibling; return out; }
From source file:facedetection.FaceDetector.java
public BufferedImage mat2BufferedImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*from w ww. java 2 s.c o m*/ int bufferSize = m.channels() * m.cols() * m.rows(); byte[] b = new byte[bufferSize]; m.get(0, 0, b); // get all the pixels BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; }
From source file:faceDetectionV1.FaceDetection.java
private BufferedImage convertMatToImage(Mat matrix) { int type = BufferedImage.TYPE_BYTE_GRAY; if (matrix.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*ww w. ja va 2 s .c o m*/ int bufferSize = matrix.channels() * matrix.cols() * matrix.rows(); byte bytes[] = new byte[bufferSize]; matrix.get(0, 0, bytes); BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type); final byte targetsize[] = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(bytes, 0, targetsize, 0, bytes.length); return image; }
From source file:formularios.FrmCamera.java
public BufferedImage MatToBufferedImage(Mat frame) { //Mat() to BufferedImage int type = 0; if (frame.channels() == 1) { type = BufferedImage.TYPE_BYTE_GRAY; } else if (frame.channels() == 3) { type = BufferedImage.TYPE_3BYTE_BGR; }//from ww w . j ava 2s .co m BufferedImage image = new BufferedImage(frame.width(), frame.height(), type); WritableRaster raster = image.getRaster(); DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); byte[] data = dataBuffer.getData(); frame.get(0, 0, data); return image; }
From source file:fr.olympicinsa.riocognized.facedetector.tools.ImageConvertor.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 w w . j a v a 2 s .c o m public static BufferedImage matToBufferedImage(Mat matrix) { log.debug("****** MatToBuffered Image **********"); log.debug("input : " + matrix.toString()); 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 image = new BufferedImage(cols, rows, type); image.getRaster().setDataElements(0, 0, cols, rows, data); log.debug("type: " + type); log.debug("output:" + image.toString()); log.debug("***********************************"); return image; }
From source file:gab.opencv.OpenCV.java
License:Open Source License
/** * //from www. j ava 2 s . c o m * Convert a 4 channel OpenCV Mat object into * pixels to be shoved into a 4 channel ARGB PImage's * pixel array. * * @param m * An RGBA Mat we want converted * @return * An int[] formatted to be the pixels of a PImage */ public int[] matToARGBPixels(Mat m) { int pImageChannels = 4; int numPixels = m.width() * m.height(); int[] intPixels = new int[numPixels]; byte[] matPixels = new byte[numPixels * pImageChannels]; m.get(0, 0, matPixels); ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels); return intPixels; }
From source file:gab.opencv.OpenCVProcessingUtils.java
License:Open Source License
/** * Convert an OpenCV Mat object into a PImage * to be used in other Processing code.//from www . j av a2 s . c om * Copies the Mat's pixel data into the PImage's pixel array. * Iterates over each pixel in the Mat, i.e. expensive. * * (Mainly used internally by OpenCV. Inspired by toCv() * from KyleMcDonald's ofxCv.) * * @param m * A Mat you want converted * @param img * The PImage you want the Mat converted into. */ public void toPImage(Mat m, PImage img) { img.loadPixels(); if (m.channels() == 3) { byte[] matPixels = new byte[width * height * 3]; m.get(0, 0, matPixels); for (int i = 0; i < m.width() * m.height() * 3; i += 3) { img.pixels[PApplet.floor(i / 3)] = parent.color(matPixels[i + 2] & 0xFF, matPixels[i + 1] & 0xFF, matPixels[i] & 0xFF); } } else if (m.channels() == 1) { byte[] matPixels = new byte[width * height]; m.get(0, 0, matPixels); for (int i = 0; i < m.width() * m.height(); i++) { img.pixels[i] = parent.color(matPixels[i] & 0xFF); } } else if (m.channels() == 4) { byte[] matPixels = new byte[width * height * 4]; m.get(0, 0, matPixels); for (int i = 0; i < m.width() * m.height() * 4; i += 4) { img.pixels[PApplet.floor(i / 4)] = parent.color(matPixels[i + 2] & 0xFF, matPixels[i + 1] & 0xFF, matPixels[i] & 0xFF, matPixels[i + 3] & 0xFF); } } img.updatePixels(); }