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: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   w  w w .  j av a  2  s.  com
    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 ww  . jav 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

/**
 * Convert an OpenCV Mat object into a PImage
 * to be used in other Processing code./* w  ww .j a v  a 2s .co  m*/
 * 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) {
        Mat m2 = new Mat();
        Imgproc.cvtColor(m, m2, Imgproc.COLOR_RGB2RGBA);
        img.pixels = matToARGBPixels(m2);
    } else if (m.channels() == 1) {
        Mat m2 = new Mat();
        Imgproc.cvtColor(m, m2, Imgproc.COLOR_GRAY2RGBA);
        img.pixels = matToARGBPixels(m2);
    } else if (m.channels() == 4) {
        img.pixels = matToARGBPixels(m);
    }

    img.updatePixels();
}

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   ww  w .  ja va2 s.co m*/
 * 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();
}

From source file:houghtransform.Capture.java

private static BufferedImage convertMatToBufferedImage(Mat mat) {
    byte[] data = new byte[mat.width() * mat.height() * (int) mat.elemSize()];
    int type;/*from  w ww  . java  2  s. com*/
    mat.get(0, 0, data);

    switch (mat.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:
        throw new IllegalStateException("Unsupported number of channels");
    }

    BufferedImage out = new BufferedImage(mat.width(), mat.height(), type);

    out.getRaster().setDataElements(0, 0, mat.width(), mat.height(), data);

    return out;
}

From source file:imagegame.Camera.java

public static BufferedImage mat2BufferedImage(Mat mat) {
    //        MatOfByte buffer = new MatOfByte();
    //        Imgcodecs.imencode(".png", mat, buffer);
    int type = mat.channels() > 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;
    BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
    mat.get(0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData());
    return image;
    //return new Image(new ByteArrayInputStream(buffer.toArray()));
}

From source file:imageprocess.PixelProcessor.java

public void salt(Mat image, int n) {
    for (int k = 0; k < n; k++) {
        int i = (int) (Math.random() * image.cols());
        int j = (int) (Math.random() * image.rows());
        if (image.channels() == 1) {
            image.put(j, i, 255);//  ww w  .ja  v a 2 s  .c o m
        } else if (image.channels() == 3) {
            image.put(j, i, new byte[] { (byte) 255, (byte) 255, (byte) 255 });
        }
    }
}

From source file:interactivespaces.service.image.vision.opencv.MatUtils.java

License:Apache License

/**
 * Converts a {@link Mat} into a {@link BufferedImage}.
 *
 * @param matrix/*from   w ww. j a va 2 s . com*/
 *          Mat of type CV_8UC3 or CV_8UC1
 *
 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
 *
 * @throws SimpleInteractiveSpacesException
 *           the OpenCV Mat type is not supported
 */
public static BufferedImage matToBufferedImage(Mat matrix) throws SimpleInteractiveSpacesException {
    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;
        for (int i = 0; i < data.length; i = i + 3) {
            byte b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;
    default:
        throw new SimpleInteractiveSpacesException("The OpenCV Mat type is not supported");
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);

    return image;
}

From source file:io.github.jakejmattson.facialrecognition.ImageFrame.java

License:Open Source License

/**
 * Convert an OpenCV Mat to a Java BufferedImage.
 *
 * @param matrix/*w ww . j av  a 2  s  .co m*/
 *       OpenCV Mat
 *
 * @return BufferedImage
 */
private static BufferedImage convertMatToImage(Mat matrix) {
    int width = matrix.width();
    int height = matrix.height();
    int type = matrix.channels() != 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;

    if (type == BufferedImage.TYPE_3BYTE_BGR)
        Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB);

    byte[] data = new byte[width * height * (int) matrix.elemSize()];
    matrix.get(0, 0, data);

    BufferedImage out = new BufferedImage(width, height, type);
    out.getRaster().setDataElements(0, 0, width, height, data);

    return out;
}

From source file:io.smartspaces.service.image.vision.opencv.MatUtils.java

License:Apache License

/**
 * Converts a {@link Mat} into a {@link BufferedImage}.
 *
 * @param matrix/*from  ww  w  . j  a v a  2  s  .co  m*/
 *          Mat of type CV_8UC3 or CV_8UC1
 *
 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
 *
 * @throws SimpleSmartSpacesException
 *           the OpenCV Mat type is not supported
 */
public static BufferedImage matToBufferedImage(Mat matrix) throws SimpleSmartSpacesException {
    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;
        for (int i = 0; i < data.length; i = i + 3) {
            byte b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;
    default:
        throw new SimpleSmartSpacesException("The OpenCV Mat type is not supported");
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);

    return image;
}