Example usage for org.opencv.core Mat Mat

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

Introduction

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

Prototype

public Mat(Mat m, Range rowRange, Range colRange) 

Source Link

Usage

From source file:TelaTakeFoto.java

/**
 * Mtodo img2Mat : BufferedImage to Mat/*from  w w w  . jav  a  2 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:OCV_WarpAffine.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    Size size = new Size((double) imw, (double) imh);
    Mat mat = new Mat(2, 3, CvType.CV_64FC1);

    for (int i = 0; i < 2; i++) {
        mat.put(i, 0, new double[] { Double.valueOf(rt.getStringValue(0, i).replaceAll("\"|'", "")) });
        mat.put(i, 1, new double[] { Double.valueOf(rt.getStringValue(1, i).replaceAll("\"|'", "")) });
        mat.put(i, 2, new double[] { Double.valueOf(rt.getStringValue(2, i).replaceAll("\"|'", "")) });
    }// w  w  w  .j  a  va 2s.  co  m

    if (ip.getBitDepth() == 8) {
        byte[] srcdst_ar = (byte[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 16) {
        short[] srcdst_ar = (short[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_16UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_16UC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else if (ip.getBitDepth() == 24) {
        int[] srcdst_ar = (int[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3);

        OCV__LoadLibrary.intarray2mat(srcdst_ar, src_mat, imw, imh);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ar, imw, imh);
    } else if (ip.getBitDepth() == 32) {
        float[] srcdst_ar = (float[]) ip.getPixels();
        Mat src_mat = new Mat(imh, imw, CvType.CV_32FC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_32FC1);

        src_mat.put(0, 0, srcdst_ar);
        Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]);
        dst_mat.get(0, 0, srcdst_ar);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:MainBox.java

public static void main(String[] args) {

    try {/*from   ww w . ja  v  a  2 s  .  co  m*/

        int kernelSize = 9; //ao aumentar o valor a imagem fica mais embassada

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D://teste_gray.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);

        Mat destination = new Mat(source.rows(), source.cols(), source.type());

        Mat kernel = Mat.ones(kernelSize, kernelSize, CvType.CV_32F);

        for (int i = 0; i < kernel.rows(); i++) {

            for (int j = 0; j < kernel.cols(); j++) {

                double[] m = kernel.get(j, j);

                for (int k = 0; k < m.length; k++) {
                    m[k] = m[k] / (kernelSize * kernelSize);
                }
                kernel.put(i, j, m);
            }
        }

        Imgproc.filter2D(source, destination, -1, kernel);

        Highgui.imwrite("D://Box.jpg", destination);

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

From source file:OCV_Blur.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    if (ip.getBitDepth() == 8) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        byte[] srcdst_bytes = (byte[]) ip.getPixels();

        // mat// w  ww . ja  v  a 2 s.  c o m
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

        // run
        src_mat.put(0, 0, srcdst_bytes);
        Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_bytes);
    } else if (ip.getBitDepth() == 16) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        short[] srcdst_shorts = (short[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_16S);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_16S);

        // run
        src_mat.put(0, 0, srcdst_shorts);
        Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_shorts);
    } else if (ip.getBitDepth() == 24) {
        // dst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        int[] srcdst_ints = (int[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3);

        // run
        OCV__LoadLibrary.intarray2mat(srcdst_ints, src_mat, imw, imh);
        Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]);
        OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ints, imw, imh);
    } else if (ip.getBitDepth() == 32) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        float[] srcdst_floats = (float[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_32F);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_32F);

        // run
        src_mat.put(0, 0, srcdst_floats);
        Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_floats);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:OctoEye.java

License:Open Source License

public OctoEye(byte buffer[]) {
    start = System.currentTimeMillis();
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    src = new Mat(HEIGHT, WIDTH, CvType.CV_8UC1);
    src.put(0, 0, buffer);/*from   ww  w . java2  s  .co m*/

    tmp1 = new Mat(src.rows(), src.cols(), src.type());
    tmp2 = new Mat(src.rows(), src.cols(), src.type());

    dst = new Mat(src.rows(), src.cols(), CvType.CV_8UC3);
    Imgproc.cvtColor(src, dst, Imgproc.COLOR_GRAY2BGR);

    if (debug) {
        dbg = new Mat(src.rows(), src.cols(), CvType.CV_8UC3);
        dst.copyTo(dbg);
    }

    detectSymbols();
    detectPupil();

    end = System.currentTimeMillis();
    time = end - start;
}

From source file:OctoEye.java

License:Open Source License

public Mat getDst2x() {
    dst2 = new Mat(src.rows() * 2, src.cols() * 2, CvType.CV_8UC3);
    Imgproc.resize(dst, dst2, new Size(src.cols() * 2, src.rows() * 2));
    return dst2;/*from  ww w.java2s.c o m*/
}

From source file:OCV_LogPolar.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // srcdst//from   w  w  w  . j  a v a  2  s.com
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    byte[] srcdst_ar = (byte[]) ip.getPixels();

    // mat
    Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
    Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

    // run
    src_mat.put(0, 0, srcdst_ar);
    Imgproc.logPolar(src_mat, dst_mat, new Point(cx, cy), (double) rmax, TYPE_INT[type_ind]);
    dst_mat.get(0, 0, srcdst_ar);
}

From source file:OCV_GrabCut.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src (RGB)/*from   www  . j ava  2  s  .  c om*/
    int[] src_arr = (int[]) imp_src.getChannelProcessor().getPixels();
    int imw_src = imp_src.getWidth();
    int imh_src = imp_src.getHeight();
    Mat mat_src = new Mat(imh_src, imw_src, CvType.CV_8UC3);
    OCV__LoadLibrary.intarray2mat(src_arr, mat_src, imw_src, imh_src);

    // tmp (Gray)
    byte[] msk_arr = (byte[]) imp_msk.getChannelProcessor().getPixels();
    int imw_msk = imp_msk.getWidth();
    int imh_msk = imp_msk.getHeight();
    int numpix_msk = imw_msk * imh_msk;

    // output
    Mat mat_msk = new Mat(imh_msk, imw_msk, CvType.CV_8UC1);
    Mat bgdModel = new Mat();
    Mat fgdModel = new Mat();

    // run      
    mat_msk.put(0, 0, msk_arr);
    Imgproc.grabCut(mat_src, mat_msk, rect, bgdModel, fgdModel, iter, TYPE_VAL[ind_type]);
    mat_msk.get(0, 0, msk_arr);

    if (enFgd) {
        for (int i = 0; i < numpix_msk; i++) {
            if (msk_arr[i] == Imgproc.GC_FGD || msk_arr[i] == Imgproc.GC_PR_FGD) {
                msk_arr[i] = (byte) 255;
            } else {
                msk_arr[i] = (byte) 0;
            }
        }
    }
}

From source file:OCV_FeatureDetection.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // QueryImage
    int[] arr_query = (int[]) imp_query.getChannelProcessor().getPixels();
    int imw_query = imp_query.getWidth();
    int imh_query = imp_query.getHeight();
    Mat mat_query = new Mat(imh_query, imw_query, CvType.CV_8UC3);
    OCV__LoadLibrary.intarray2mat(arr_query, mat_query, imw_query, imh_query);

    // TrainImage
    int[] arr_train = (int[]) imp_train.getChannelProcessor().getPixels();
    int imw_train = imp_train.getWidth();
    int imh_train = imp_train.getHeight();
    Mat mat_train = new Mat(imh_train, imw_train, CvType.CV_8UC3);
    OCV__LoadLibrary.intarray2mat(arr_train, mat_train, imw_train, imh_train);

    // KeyPoint/* ww w .  j a va2s.  co m*/
    MatOfKeyPoint key_query = new MatOfKeyPoint();
    MatOfKeyPoint key_train = new MatOfKeyPoint();
    detector.detect(mat_query, key_query);
    detector.detect(mat_train, key_train);

    // Descriptor
    DescriptorExtractor extractor = DescriptorExtractor.create(type_ext);
    Mat desc_query = new Mat();
    Mat desc_train = new Mat();
    extractor.compute(mat_query, key_query, desc_query);
    extractor.compute(mat_train, key_train, desc_train);

    // Matcher
    DescriptorMatcher matcher = DescriptorMatcher.create(TYPE_VAL_MATCH[ind_match]);
    MatOfDMatch dmatch = new MatOfDMatch();
    matcher.match(desc_query, desc_train, dmatch);

    dmatch = showData(key_query, key_train, dmatch);

    // Output
    if (enDrawMatches) {
        Mat mat_dst = new Mat();
        Features2d.drawMatches(mat_query, key_query, mat_train, key_train, dmatch, mat_dst);

        String title_dst = WindowManager.getUniqueName("FeatureDetection");
        int imw_dst = mat_dst.cols();
        int imh_dst = mat_dst.rows();
        ImagePlus imp_dst = new ImagePlus(title_dst, new ColorProcessor(imw_dst, imh_dst));
        int[] arr_dst = (int[]) imp_dst.getChannelProcessor().getPixels();
        OCV__LoadLibrary.mat2intarray(mat_dst, arr_dst, imw_dst, imh_dst);
        imp_dst.show();
    }
}

From source file:OCV_Laplacian.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    if (ip.getBitDepth() == 8) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        byte[] srcdst_bytes = (byte[]) ip.getPixels();

        // mat/*from ww  w. jav a  2s.  com*/
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1);

        // run
        src_mat.put(0, 0, srcdst_bytes);
        Imgproc.Laplacian(src_mat, dst_mat, dst_mat.depth(), ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_bytes);
    } else if (ip.getBitDepth() == 16) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        short[] srcdst_shorts = (short[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_16S);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_16S);

        // run
        src_mat.put(0, 0, srcdst_shorts);
        Imgproc.Laplacian(src_mat, dst_mat, dst_mat.depth(), ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_shorts);
    } else if (ip.getBitDepth() == 24) {
        // dst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        int[] srcdst_ints = (int[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3);

        // run
        OCV__LoadLibrary.intarray2mat(srcdst_ints, src_mat, imw, imh);
        Imgproc.Laplacian(src_mat, dst_mat, dst_mat.depth(), ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ints, imw, imh);
    } else if (ip.getBitDepth() == 32) {
        // srcdst
        int imw = ip.getWidth();
        int imh = ip.getHeight();
        float[] srcdst_floats = (float[]) ip.getPixels();

        // mat
        Mat src_mat = new Mat(imh, imw, CvType.CV_32F);
        Mat dst_mat = new Mat(imh, imw, CvType.CV_32F);

        // run
        src_mat.put(0, 0, srcdst_floats);
        Imgproc.Laplacian(src_mat, dst_mat, dst_mat.depth(), ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_floats);
    } else {
        IJ.error("Wrong image format");
    }
}