Example usage for org.opencv.core Mat channels

List of usage examples for org.opencv.core Mat channels

Introduction

In this page you can find the example usage for org.opencv.core Mat channels.

Prototype

public int channels() 

Source Link

Usage

From source file:frmMain.java

public static BufferedImage mat2Img(Mat in) {
    BufferedImage out;/*from   ww  w .  ja v a 2  s  . c  om*/
    byte[] data = new byte[in.width() * in.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(in.width(), in.height(), type);

    out.getRaster().setDataElements(0, 0, in.width(), in.height(), data);
    return out;
}

From source file:Video.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  .j  a v a  2  s  .  co 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:Ko.java

License:Open Source License

/**
* Converts a cv::Mat to a BufferedImage so that it plays nicely 
* with Java./*from   w ww  .  j  av  a2s .c  o m*/
* @param m
*          The Mat to convert.
* @return
*          A BufferedImage version of the Mat.
*/
private static BufferedImage toBufferedImage(Mat m) {
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    int bufferSize = m.channels() * m.cols() * m.rows();
    byte b[] = new byte[bufferSize];
    m.get(0, 0, b);
    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:OctoEye.java

License:Open Source License

public BufferedImage getBufferedImage(Mat m) {
    byte[] buffer = new byte[m.rows() * m.cols() * m.channels()];
    m.get(0, 0, buffer);/*from w  w  w  .  j  a  v  a 2  s.  com*/

    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
        for (int i = 0; i < buffer.length; i = i + 3) {
            byte b = buffer[i];
            buffer[i] = buffer[i + 2];
            buffer[i + 2] = b;
        }
    }

    BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
    System.arraycopy(buffer, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData(), 0,
            buffer.length);

    return image;
}

From source file:airhockeyjava.detection.PS3EyeFrameGrabber.java

License:Open Source License

/**
 * Convert matrix into an image//from   w  ww  .j a  v a  2 s  .  com
 * 
 * @param m
 *            matrix to be converted
 * @return Converted BufferedImage
 */
private static BufferedImage toBufferedImage(Mat m) {
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
        // System.out.println("3 Channel 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:arlocros.Imshow.java

License:Apache License

/**
 * @param opencvImage/*from  w  w  w . j  ava  2 s. c o m*/
 */
public static void show(Mat opencvImage) {

    Dimension frameSize = new Dimension(opencvImage.rows(), opencvImage.cols());
    if (frame == null) {
        frame = new Imshow("", frameSize.height, frameSize.width);
        frame.Window.setVisible(true);

        frame.Window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        if (frame.SizeCustom) {
            Imgproc.resize(opencvImage, opencvImage, new Size(frame.Height, frame.Width));
        }
    }
    BufferedImage bufImage = null;
    try {

        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (opencvImage.channels() > 1) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = opencvImage.channels() * opencvImage.cols() * opencvImage.rows();
        byte[] b = new byte[bufferSize];
        opencvImage.get(0, 0, b);
        BufferedImage bufferedImage = new BufferedImage(opencvImage.cols(), opencvImage.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        bufImage = bufferedImage;
        frame.image.setImage(bufImage);
        frame.Window.pack();
        frame.label.updateUI();
        //frame.Window.setVisible(true);
    } catch (RuntimeException e) {
        logger.info("Exception while visualizing.", e);
    }
}

From source file:attendance_system_adder.cv.image.java

public BufferedImage Mat2BufferedImage(Mat m) {
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }/*www.  j av  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:balldetection.Webcam.java

public static Image toBufferedImage(Mat m) {
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1) {
        Mat m2 = new Mat();
        Imgproc.cvtColor(m, m2, Imgproc.COLOR_BGR2RGB);
        type = BufferedImage.TYPE_3BYTE_BGR;
        m = m2;//ww w  .j  a  v a2  s.c  o  m
    }
    byte[] b = new byte[m.channels() * m.cols() * m.rows()];
    m.get(0, 0, b); // get all the pixels
    BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
    image.getRaster().setDataElements(0, 0, m.cols(), m.rows(), b);
    return image;
}

From source file:bgslibrary.Utils.java

License:Open Source License

static final public BufferedImage toBufferedImage(Mat m) {
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1)
        type = BufferedImage.TYPE_3BYTE_BGR;
    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:bollettini.BullettinCompiler.java

public void show() {
    //resize to show
    Size size = new Size(1100, 335);
    Mat resize = new Mat();
    Imgproc.resize(bullettin, resize, size);

    //create image
    int type = BufferedImage.TYPE_BYTE_GRAY;
    int bufferSize = resize.channels() * resize.cols() * resize.rows();
    byte[] b = new byte[bufferSize];
    resize.get(0, 0, b); // get all the pixels
    BufferedImage image = new BufferedImage(resize.cols(), resize.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);

    ImageIcon icon = new ImageIcon(image);

    //create image and show
    View view = new View();
    view.init(this);
    view.setIcon(icon);/* ww w  .  j  a  v a  2  s  . c o m*/
    view.setVisible(true);
    view.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}