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:OCV_Canny.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // srcdst/*from  w w w .  j  a  va2  s  .c o m*/
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    byte[] srcdst_bytes = (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_bytes);
    Imgproc.Canny(src_mat, dst_mat, thr1, thr2, SIZE_VAL[ind_size], l2grad);
    dst_mat.get(0, 0, srcdst_bytes);
}

From source file:ImagetoPDF.java

public static void enhance(String fileName) throws IOException {

    Mat source = Imgcodecs.imread(fileName, Imgcodecs.CV_LOAD_IMAGE_COLOR);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    //    Imgproc.cvtColor(source, destination, Imgproc.COLOR_BGR2GRAY);
    Imgproc.cvtColor(source, source, Imgproc.COLOR_BGR2GRAY);
    Mat imageMat = source;/*  www  .  j a  va2 s .c o  m*/
    Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
    Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5,
            4);
    Imgcodecs.imwrite(fileName, imageMat);
}

From source file:OCV_InvertAffineTransform.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    if (isPerspective) {
        Mat mat_src = new Mat(3, 3, CvType.CV_64FC1);
        Mat mat_dst = new Mat(3, 3, CvType.CV_64FC1);

        mat_src.put(0, 0, new double[] { Double.valueOf(rt.getStringValue(0, 0).replaceAll("\"|'", "")) });
        mat_src.put(0, 1, new double[] { Double.valueOf(rt.getStringValue(1, 0).replaceAll("\"|'", "")) });
        mat_src.put(0, 2, new double[] { Double.valueOf(rt.getStringValue(2, 0).replaceAll("\"|'", "")) });
        mat_src.put(1, 0, new double[] { Double.valueOf(rt.getStringValue(0, 1).replaceAll("\"|'", "")) });
        mat_src.put(1, 1, new double[] { Double.valueOf(rt.getStringValue(1, 1).replaceAll("\"|'", "")) });
        mat_src.put(1, 2, new double[] { Double.valueOf(rt.getStringValue(2, 1).replaceAll("\"|'", "")) });
        mat_src.put(2, 0, new double[] { Double.valueOf(rt.getStringValue(0, 2).replaceAll("\"|'", "")) });
        mat_src.put(2, 1, new double[] { Double.valueOf(rt.getStringValue(1, 2).replaceAll("\"|'", "")) });
        mat_src.put(2, 2, new double[] { Double.valueOf(rt.getStringValue(2, 2).replaceAll("\"|'", "")) });

        Imgproc.invertAffineTransform(mat_src, mat_dst);

        rt.reset();//from ww  w .  j  av  a  2s  . c  om
        rt.incrementCounter();
        rt.addValue("Column01", String.valueOf(mat_dst.get(0, 0)[0]));
        rt.addValue("Column02", String.valueOf(mat_dst.get(0, 1)[0]));
        rt.addValue("Column03", String.valueOf(mat_dst.get(0, 2)[0]));
        rt.incrementCounter();
        rt.addValue("Column01", String.valueOf(mat_dst.get(1, 0)[0]));
        rt.addValue("Column02", String.valueOf(mat_dst.get(1, 1)[0]));
        rt.addValue("Column03", String.valueOf(mat_dst.get(1, 2)[0]));
        rt.incrementCounter();
        rt.addValue("Column01", String.valueOf(mat_dst.get(2, 0)[0]));
        rt.addValue("Column02", String.valueOf(mat_dst.get(2, 1)[0]));
        rt.addValue("Column03", String.valueOf(mat_dst.get(2, 2)[0]));
        rt.show("Results");
    } else {
        Mat mat_src = new Mat(2, 3, CvType.CV_64FC1);
        Mat mat_dst = new Mat(2, 3, CvType.CV_64FC1);

        mat_src.put(0, 0, new double[] { Double.valueOf(rt.getStringValue(0, 0).replaceAll("\"|'", "")) });
        mat_src.put(0, 1, new double[] { Double.valueOf(rt.getStringValue(1, 0).replaceAll("\"|'", "")) });
        mat_src.put(0, 2, new double[] { Double.valueOf(rt.getStringValue(2, 0).replaceAll("\"|'", "")) });
        mat_src.put(1, 0, new double[] { Double.valueOf(rt.getStringValue(0, 1).replaceAll("\"|'", "")) });
        mat_src.put(1, 1, new double[] { Double.valueOf(rt.getStringValue(1, 1).replaceAll("\"|'", "")) });
        mat_src.put(1, 2, new double[] { Double.valueOf(rt.getStringValue(2, 1).replaceAll("\"|'", "")) });

        Imgproc.invertAffineTransform(mat_src, mat_dst);

        rt.reset();
        rt.incrementCounter();
        rt.addValue("Column01", String.valueOf(mat_dst.get(0, 0)[0]));
        rt.addValue("Column02", String.valueOf(mat_dst.get(0, 1)[0]));
        rt.addValue("Column03", String.valueOf(mat_dst.get(0, 2)[0]));
        rt.incrementCounter();
        rt.addValue("Column01", String.valueOf(mat_dst.get(1, 0)[0]));
        rt.addValue("Column02", String.valueOf(mat_dst.get(1, 1)[0]));
        rt.addValue("Column03", String.valueOf(mat_dst.get(1, 2)[0]));
        rt.show("Results");

    }
}

From source file:OCV_Threshold.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    int imw = ip.getWidth();
    int imh = ip.getHeight();

    if (ip.getBitDepth() == 8) {
        // srcdst
        byte[] srcdst_bytes = (byte[]) ip.getPixels();

        // mat/*from www .  j  a v  a2 s .  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.threshold(src_mat, dst_mat, thresh, maxVal, INT_TYPE[idxType]);
        dst_mat.get(0, 0, srcdst_bytes);
    } else if (ip.getBitDepth() == 32) {
        // srcdst
        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.threshold(src_mat, dst_mat, thresh, maxVal, INT_TYPE[idxType]);
        dst_mat.get(0, 0, srcdst_floats);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:OCV_MatchTemplate.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src//from w  w w  .j  a v a2  s.c  om
    byte[] arr_src = (byte[]) 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_8UC1);
    mat_src.put(0, 0, arr_src);

    // tmp
    byte[] arr_tmp = (byte[]) imp_tmp.getChannelProcessor().getPixels();
    int imw_tmp = imp_tmp.getWidth();
    int imh_tmp = imp_tmp.getHeight();
    Mat mat_tmp = new Mat(imh_tmp, imw_tmp, CvType.CV_8UC1);
    mat_tmp.put(0, 0, arr_tmp);

    // dst
    String title_dst = WindowManager.getUniqueName(title_src + "_MatchTemplate");
    int imw_dst = imw_src - imw_tmp + 1;
    int imh_dst = imh_src - imh_tmp + 1;
    ImagePlus imp_dst = new ImagePlus(title_dst, new FloatProcessor(imw_dst, imh_dst));
    float[] arr_dst = (float[]) imp_dst.getChannelProcessor().getPixels();
    Mat mat_dst = new Mat();

    // run
    Imgproc.matchTemplate(mat_src, mat_tmp, mat_dst, TYPE_VAL[ind_type]);
    mat_dst.get(0, 0, arr_dst);
    imp_dst.show();

    if (TYPE_VAL[ind_type] == Imgproc.TM_SQDIFF_NORMED) {
        substracted_from_one(arr_dst);
    }

    IJ.run(imp_dst, "Enhance Contrast", "saturated=0.35");

    // show data
    if (enResult) {
        if (enSearchMax) {
            showData_enSearchMaxPoint(imp_dst, thr_res, imw_tmp, imh_tmp);
        } else {
            showData(arr_dst, imw_dst, imh_dst, imw_tmp, imh_tmp);
        }
    }
}

From source file:MainBorder.java

public static void main(String[] args) {

    try {/*from   w  w  w.j  av a 2  s . c o  m*/

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D:\\teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

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

        int top, bottom, left, right;

        int borderType;

        //initialize arguments for the filter border

        top = (int) (0.05 * source.rows());
        bottom = (int) (0.05 * source.rows());

        left = (int) (0.15 * source.cols());
        right = (int) (0.15 * source.cols());

        destination = source;

        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_WRAP); //borda com a imagem
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_CONSTANT); //borda preta
        Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_DEFAULT); //borda transparente
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_ISOLATED); // borda escura??
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT); //borda com reflexo da prpria imagem
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REFLECT_101);
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_REPLICATE); //borda efeito movimento
        //Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_TRANSPARENT); //borda no funciona

        Highgui.imwrite("D:\\borderWrap.jpg", destination);

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

}

From source file:OCV_WarpPerspective.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(3, 3, CvType.CV_64FC1);

    for (int i = 0; i < 3; 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("\"|'", "")) });
    }/*from   w w  w  . jav  a 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.warpPerspective(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.warpPerspective(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.warpPerspective(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.warpPerspective(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:OCV_ConnectedComponentsWithStats.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src/*from   w  ww  .ja v a  2s. c om*/
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    byte[] src_arr = (byte[]) ip.getPixels();
    Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);

    // dst
    String titleDst = WindowManager
            .getUniqueName(impSrc.getTitle() + "_Connect" + String.valueOf(TYPE_INT[type_ind]));
    ImagePlus impDst = new ImagePlus(titleDst, new FloatProcessor(imw, imh));
    float[] dst_arr = (float[]) impDst.getChannelProcessor().getPixels();
    Mat dst_mat_32s = new Mat(imh, imw, CvType.CV_32S);
    Mat dst_mat_32f = new Mat(imh, imw, CvType.CV_32F);
    Mat stats_mat = new Mat();
    Mat cens_mat = new Mat();

    // run
    src_mat.put(0, 0, src_arr);
    int output_con = Imgproc.connectedComponentsWithStats(src_mat, dst_mat_32s, stats_mat, cens_mat,
            TYPE_INT[type_ind], CvType.CV_32S);
    dst_mat_32s.convertTo(dst_mat_32f, CvType.CV_32F);
    dst_mat_32f.get(0, 0, dst_arr);

    // show data
    if (1 < output_con) {
        showData(dst_arr, imw, imh, output_con, stats_mat, cens_mat);
    }

    // finish
    if (1 < output_con && enOutImg) {
        impDst.show();
    } else {
        impDst.close();
    }
}

From source file:OCV_LinearPolar.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // srcdst//ww  w  .  j a v a2  s  .  c om
    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.linearPolar(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_Sobel.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   w  w  w . j  av a  2s  . c  om
        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.Sobel(src_mat, dst_mat, src_mat.depth(), dx, dy, 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.Sobel(src_mat, dst_mat, src_mat.depth(), dx, dy, ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_shorts);
    } 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.Sobel(src_mat, dst_mat, src_mat.depth(), dx, dy, ksize, scale, delta,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_floats);
    } else {
        IJ.error("Wrong image format");
    }
}