List of usage examples for org.opencv.core Mat get
public int get(int row, int col, double[] data)
From source file:video.Mat2Image.java
BufferedImage getImage(Mat mat) { getSpace(mat); mat.get(0, 0, dat); img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat); return img; }
From source file:video.PictureView.java
public static BufferedImage mat2Img(Mat in) { BufferedImage out;// w w w.j av a 2 s. co m int width = in.cols(); int height = in.height(); byte[] data = new byte[width * height * (int) in.elemSize()]; int type; in.get(0, 0, data); if (in.channels() == 1) { type = BufferedImage.TYPE_BYTE_GRAY; } else { type = BufferedImage.TYPE_3BYTE_BGR; } out = new BufferedImage(width, height, type); out.getRaster().setDataElements(0, 0, width, height, data); return out; }
From source file:videostream.MulticastServer.java
/** * Transforms an OpenCV mat to a byte array * @param m The mat to transform/*from www . j a va 2 s.c o m*/ * @param format The desired format * @return The mat transformed to a byte array * @throws IOException */ public byte[] matToByteArray(Mat m, String format) throws IOException { // Check if image is grayscale or color int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } // Transfer bytes from Mat to BufferedImage 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); Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos1); writer.setOutput(ios); IIOImage Iimage = new IIOImage(image, null, null); writer.write(null, Iimage, iwp); writer.dispose(); return baos1.toByteArray(); }
From source file:videostream.MyFrame.java
public Image toBufferedImage(Mat m) { // Code from http://stackoverflow.com/questions/15670933/opencv-java-load-image-to-gui // Check if image is grayscale or color int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }// w ww .j av a2s. com // Transfer bytes from Mat to BufferedImage 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:webcamfacedetect.FaceDetect.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 ww w . j a v a 2 s. c o m public boolean MatToBufferedImage(Mat matBGR) { long startTime = System.nanoTime(); int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels(); byte[] sourcePixels = new byte[width * height * channels]; matBGR.get(0, 0, sourcePixels); // create new image and get reference to backing data image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); long endTime = System.nanoTime(); //System.out.println(String.format("Elapsed time: %.2f ms", (float) (endTime - startTime) / 1000000)); return true; }