Example usage for org.opencv.core Mat elemSize

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

Introduction

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

Prototype

public long elemSize() 

Source Link

Usage

From source file:frmMain.java

public static BufferedImage mat2Img(Mat in) {
    BufferedImage out;/* w  w w.  j  a v  a2s . c o m*/
    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:MainShapeConversion.java

public static void main(String[] args) {

    try {//ww w .  ja  va  2s .  c  o m

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        File input = new File("D://teste.png");

        BufferedImage image = ImageIO.read(input);

        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

        Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);

        mat.put(0, 0, data);

        Mat mat1 = new Mat(image.getWidth(), image.getHeight(), CvType.CV_8UC3);

        Core.flip(mat, mat1, -1); //-1 invert , 1 normal

        byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())];

        mat1.get(0, 0, data1);

        BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);

        image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

        File output = new File("D://hsv.jpg");

        ImageIO.write(image1, "jpg", output);

    } catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());

    }
}

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;//  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:TelaTakeFoto.java

/**
 * Mtodo img2Mat : BufferedImage to Mat//  www . java2  s.  c  o  m
 *
 * @param in
 * @return
 */
public static Mat img2Mat(BufferedImage in) {

    Mat out = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC3);

    byte[] data = new byte[in.getWidth() * in.getHeight() * (int) out.elemSize()];

    int[] dataBuff = in.getRGB(0, 0, in.getWidth(), in.getHeight(), null, 0, in.getWidth());

    for (int i = 0; i < dataBuff.length; i++) {

        data[i * 3] = (byte) ((dataBuff[i]));

        data[i * 3 + 1] = (byte) ((dataBuff[i]));

        data[i * 3 + 2] = (byte) ((dataBuff[i]));

    }

    out.put(0, 0, data);
    return out;
}

From source file:LicenseDetection.java

public BufferedImage matToBufferedImage(Mat mat) {
    BufferedImage bimg;//from ww w .  j  a v a  2  s . c o m

    if (mat != null) {
        int cols = mat.cols();
        int rows = mat.rows();
        int elemSize = (int) mat.elemSize();
        byte[] data = new byte[cols * rows * elemSize];
        int type = BufferedImage.TYPE_BYTE_GRAY;
        mat.get(0, 0, data);

        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:classes.Util.java

public static BufferedImage mat2Img(Mat in) {
    BufferedImage out;/* w  w  w. j  av a  2 s . c  o  m*/
    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:classes.Util.java

public static Mat img2Mat(BufferedImage in) {

    int width = in.getWidth();
    int height = in.getHeight();

    Mat out;
    byte[] data;/*from  w  w w .j a v a2s  .com*/
    int r, g, b;

    if (in.getType() == BufferedImage.TYPE_INT_RGB) {
        out = new Mat(height, width, CvType.CV_8UC3);
        data = new byte[width * height * (int) out.elemSize()];
        int[] dataBuff = in.getRGB(0, 0, width, height, null, 0, width);
        for (int i = 0; i < dataBuff.length; i++) {
            data[i * 3] = (byte) ((dataBuff[i] >> 16) & 0xFF);
            data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
            data[i * 3 + 2] = (byte) ((dataBuff[i] >> 0) & 0xFF);
        }
    } else {
        out = new Mat(height, width, CvType.CV_8UC1);
        data = new byte[width * height * (int) out.elemSize()];
        int[] dataBuff = in.getRGB(0, 0, width, height, null, 0, width);
        for (int i = 0; i < dataBuff.length; i++) {
            r = (byte) ((dataBuff[i] >> 16) & 0xFF);
            g = (byte) ((dataBuff[i] >> 8) & 0xFF);
            b = (byte) ((dataBuff[i] >> 0) & 0xFF);
            data[i] = (byte) ((0.21 * r) + (0.71 * g) + (0.07 * b)); //luminosity
        }
    }
    out.put(0, 0, data);
    return out;
}

From source file:com.astrocytes.core.ImageHelper.java

License:Open Source License

public static BufferedImage convertMatToBufferedImage(Mat in) {
    BufferedImage out;/*from   ww  w  .  j a v  a2  s  . c  o  m*/
    byte[] data = new byte[in.cols() * in.rows() * (int) in.elemSize()];
    in.get(0, 0, data);
    int type = BufferedImage.TYPE_3BYTE_BGR;

    switch (in.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;
    }

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

    return out;
}

From source file:com.example.yannic.remotefacedetection.agent.FaceDetectionAgent.java

License:Open Source License

public static BufferedImage matToBufferedImage(Mat matrix, BufferedImage bimg) {
    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.  j a v a 2  s . com
        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;
        }

        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 {
        bimg = null;
    }
    return bimg;
}

From source file:com.Linguist.model.grayscaleClass.java

@Override
public File imagePreprocessing(String image, String extnsn) {

    BufferedImage bImge = null;/*from  ww w  . j  av  a 2s .c o m*/
    BufferedImage bImage2 = null;
    File grayscle = null;

    try {

        // loadOpenCV_Lib();
        //String path = "opencv\\build\\java\\x64\\opencv_java300.dll";
        FileInputStream fileName = new FileInputStream(
                "C:\\Users\\User\\Documents\\GitHub\\Linguist\\web\\uploadedImage\\" + image);
        InputStream input = fileName;
        bImge = ImageIO.read(input);
        byte[] imgeByte = ((DataBufferByte) bImge.getRaster().getDataBuffer()).getData();
        Mat mat1 = new Mat(bImge.getHeight(), bImge.getWidth(), CvType.CV_8UC3);
        mat1.put(0, 0, imgeByte);
        Mat mat2 = new Mat(bImge.getHeight(), bImge.getWidth(), CvType.CV_8UC1);
        Imgproc.cvtColor(mat1, mat2, Imgproc.COLOR_RGB2GRAY);
        byte[] imageData = new byte[mat2.rows() * mat2.cols() * (int) (mat2.elemSize())];
        mat2.get(0, 0, imageData);
        bImage2 = new BufferedImage(mat2.cols(), mat2.rows(), BufferedImage.TYPE_BYTE_GRAY);
        bImage2.getRaster().setDataElements(0, 0, mat2.cols(), mat2.rows(), imageData);

        String extn = null;
        switch (extnsn) {
        case ".jpg":
            extn = "jpg";
            break;
        case ".png":
            extn = "png";
            break;
        case ".pdf":
            extn = "pdf";
            break;
        case ".tiff":
            extn = "tif";
            break;

        }
        //writing the grayscale image to the folder
        grayscle = new File(
                "C:\\Users\\User\\Documents\\GitHub\\Linguist\\web\\uploadedImage\\grayscale" + "." + extn);
        ImageIO.write(bImage2, "jpg", grayscle);
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    } catch (Exception ex) {
        Logger.getLogger(grayscaleClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    return grayscle;

}