Example usage for org.opencv.core Mat get

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

Introduction

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

Prototype

public int get(int row, int col, double[] data) 

Source Link

Usage

From source file:OctoEye.java

License:Open Source License

private boolean detectSymbol(int[] shape, int ystart) {
    // create a 12x12 pixel buffer for the area of a star or ring shape
    byte buff[] = new byte[12 * 12];

    // fetch the area of a star or ring shape from the image and write it to the buffer
    Mat m = src.submat(new Rect(WIDTH - 30, ystart, 12, 12));
    m.get(0, 0, buff);

    int shapePixel = -64;
    int correctPixels = 0;
    int totalPixels = 0;
    for (int i = 0; i < 12 * 12; i++) {
        correctPixels += buff[i] == shapePixel && shape[i] == 1 ? 1 : 0;
        totalPixels += shape[i];//from www .  j  a  v a2  s . com
    }
    return correctPixels == totalPixels && correctPixels > 0;
}

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);

    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];/*from  w  ww .  j a v  a  2 s.c o  m*/
            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:OCV_LogPolar.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // srcdst/*  w w  w . j a va  2 s  .  c  o  m*/
    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)/*ww w.  ja  va  2s .c  o m*/
    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:LicenseDetection.java

public BufferedImage matToBufferedImage(Mat mat) {
    BufferedImage bimg;/*  w  w w.  ja  va2s.  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: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   w  ww. j  a  va 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.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");
    }
}

From source file:OCV_DistanceTransform.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // srcdst//from  w w w.j  a v  a 2 s  .  c o m
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    float[] srcdst_floats = (float[]) ip.getPixels();

    // mat
    Mat src_mat_32f = new Mat(imh, imw, CvType.CV_32FC1);
    Mat src_mat_8u = new Mat(imh, imw, CvType.CV_8UC1);
    Mat dst_mat_32f = new Mat(imh, imw, CvType.CV_32FC1);

    // run
    src_mat_32f.put(0, 0, srcdst_floats);
    src_mat_32f.convertTo(src_mat_8u, CvType.CV_8UC1);
    Imgproc.distanceTransform(src_mat_8u, dst_mat_32f, INT_DISTANCETYPE[indDistType],
            INT_DISTANCETRANSFORMMASKS[indMskSize]);
    dst_mat_32f.get(0, 0, srcdst_floats);
}

From source file:OCV_Watershed.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src (RGB)/*www.  ja  va  2 s . c  om*/
    int[] arr_src_rgb = (int[]) imp_src.getChannelProcessor().getPixels();
    int imw_src = imp_src.getWidth();
    int imh_src = imp_src.getHeight();
    Mat mat_src_rgb = new Mat(imh_src, imw_src, CvType.CV_8UC3);

    // map (32bit)
    float[] arr_map_32f = (float[]) imp_map.getChannelProcessor().getPixels();
    int imw_map = imp_map.getWidth();
    int imh_map = imp_map.getHeight();
    Mat mat_map_32f = new Mat(imh_map, imw_map, CvType.CV_32FC1);
    Mat mat_map_32s = new Mat(imh_map, imw_map, CvType.CV_32SC1);

    // run
    OCV__LoadLibrary.intarray2mat(arr_src_rgb, mat_src_rgb, imw_src, imh_src);
    mat_map_32f.put(0, 0, arr_map_32f);
    mat_map_32f.convertTo(mat_map_32s, CvType.CV_32SC1);

    Imgproc.watershed(mat_src_rgb, mat_map_32s);

    mat_map_32s.convertTo(mat_map_32f, CvType.CV_32FC1);
    mat_map_32f.get(0, 0, arr_map_32f);
}

From source file:OCV_GaussianBlur.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 ww  . j  a va  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.GaussianBlur(src_mat, dst_mat, ksize, sigma_x, sigma_y, 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.GaussianBlur(src_mat, dst_mat, ksize, sigma_x, sigma_y, 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.GaussianBlur(src_mat, dst_mat, ksize, sigma_x, sigma_y, INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_floats);
    } else {
        IJ.error("Wrong image format");
    }
}

From source file:OCV_BilateralFilter.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 . ja v a 2 s . co  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.bilateralFilter(src_mat, dst_mat, diameter, sigmaColor, sigmaSpace,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_bytes);
    } 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.bilateralFilter(src_mat, dst_mat, diameter, sigmaColor, sigmaSpace,
                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_bytes = (float[]) ip.getPixels();

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

        // run
        src_mat.put(0, 0, srcdst_bytes);
        Imgproc.bilateralFilter(src_mat, dst_mat, diameter, sigmaColor, sigmaSpace,
                INT_BORDERTYPE[indBorderType]);
        dst_mat.get(0, 0, srcdst_bytes);
    } else {
        IJ.error("Wrong image format");
    }
}