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:es.ugr.osgiliart.features.opencv.MatchImageNoBackground.java

License:Open Source License

public double match(String path) {

    Mat img = Highgui.imread(path);/* w  w  w .java2s .  c o m*/
    Mat imgResized = new Mat(SIZE, SIZE, img.type());
    Imgproc.resize(img, imgResized, new Size(SIZE, SIZE));

    double distance = 0;

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if ((imgResized.get(i, j)[0] == 255) && (imgResized.get(i, j)[1] == 255)
                    && (imgResized.get(i, j)[2] == 255)) {

                distance += PENALTY;
            } else {
                distance += Math.pow(imgResized.get(i, j)[0] - templateResized.get(i, j)[0], 2)
                        + Math.pow(imgResized.get(i, j)[1] - templateResized.get(i, j)[1], 2)
                        + Math.pow(imgResized.get(i, j)[2] - templateResized.get(i, j)[2], 2);
            }
        }
    }

    return -distance;
}

From source file:es.upv.riromu.platanus.image.ImageUtil.java

License:Open Source License

public static Mat matFromJson(String json) {
    JsonParser parser = new JsonParser();
    JsonObject JsonObject = parser.parse(json).getAsJsonObject();

    int rows = JsonObject.get("rows").getAsInt();
    int cols = JsonObject.get("cols").getAsInt();
    int type = JsonObject.get("type").getAsInt();

    String dataString = JsonObject.get("data").getAsString();
    byte[] data = Base64.decodeBase64(dataString.getBytes());

    Mat mat = new Mat(rows, cols, type);
    mat.put(0, 0, data);//w  w w  . j a v a  2  s. com

    return mat;
}

From source file:eu.fpetersen.robobrain.behavior.followobject.ColorBlobDetector.java

License:Open Source License

public void setHsvColor(Scalar hsvColor) {
    double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0] - mColorRadius.val[0] : 0;
    double maxH = (hsvColor.val[0] + mColorRadius.val[0] <= 255) ? hsvColor.val[0] + mColorRadius.val[0] : 255;

    mLowerBound.val[0] = minH;
    mUpperBound.val[0] = maxH;

    mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
    mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];

    mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
    mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];

    mLowerBound.val[3] = 0;
    mUpperBound.val[3] = 255;

    Mat spectrumHsv = new Mat(1, (int) (maxH - minH), CvType.CV_8UC3);

    for (int j = 0; j < maxH - minH; j++) {
        byte[] tmp = { (byte) (minH + j), (byte) 255, (byte) 255 };
        spectrumHsv.put(0, j, tmp);/*from  ww  w .j av a  2s  .  com*/
    }

    Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
}

From source file:eu.fpetersen.robobrain.ui.CameraViewActivity.java

License:Open Source License

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mDetector = new ColorBlobDetector(new Scalar(3.109375, 241, 186.640625));
    // ExternalStorageManager manager = new ExternalStorageManager(this);
    // File root = manager.getRoboBrainRoot();
    // File images = new File(root, "images");
    // File objectPicture = new File(images, "card2.jpg");
    // BitmapFactory.Options options = new BitmapFactory.Options();
    // options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    // Bitmap bitmap =
    // BitmapFactory.decodeFile(objectPicture.getAbsolutePath(), options);
    // Mat objectImage = new Mat();
    // Utils.bitmapToMat(bitmap, objectImage);
    // mDetector = new OrbObjectDetector(objectImage);
}

From source file:finalpro.FinalPro.java

public static String threshholding() {
    Mat destination = null;/*from ww w  .  j a  v a  2 s.c om*/
    Mat source = null;
    String str = "";
    try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        source = Imgcodecs.imread("C:/QuadPotroler/FinalPro/src/images/20151207_153915.jpg",
                Imgcodecs.CV_LOAD_IMAGE_COLOR);
        destination = new Mat(source.rows(), source.cols(), source.type());
        destination = source;
        Imgproc.threshold(source, destination, 127, 255, Imgproc.THRESH_TOZERO);
        Imgcodecs.imwrite("C:/QuadPotroler/FinalPro/src/images/threshdold.jpg", destination);
        str = "C:/QuadPotroler/FinalPro/src/images/threshdold.jpg";
    } catch (Exception e) {
        System.out.println("error: " + e.getMessage());
    }
    return str;
}

From source file:fr.olympicinsa.riocognized.facedetector.tools.ImageConvertor.java

/**
 * Converts/writes a BufferedImage into a Mat.
 *
 * @param image BufferedImage of type TYPE_3BYTE_BGR
 * @return Mat image of type CV_8UC3//  w ww  .j a v a 2  s  .com
 */
public static Mat bufferedImagetoMat(BufferedImage image) {
    log.debug("********bufferedImageToMat *********");
    log.debug("input : " + image.toString());
    int rows = image.getWidth();
    int cols = image.getHeight();
    byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    Mat mat = new Mat(cols, rows, CvType.CV_8UC3);
    mat.put(0, 0, data);
    log.debug("output : " + mat.toString());
    log.debug("***********************************");
    return mat;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

private void init(int w, int h) {
    width = w;/*from   w ww. j  a  v a 2  s. c  o  m*/
    height = h;
    welcome();
    setupWorkingImages();
    setupFlow();

    matR = new Mat(height, width, CvType.CV_8UC1);
    matG = new Mat(height, width, CvType.CV_8UC1);
    matB = new Mat(height, width, CvType.CV_8UC1);
    matA = new Mat(height, width, CvType.CV_8UC1);
    matGray = new Mat(height, width, CvType.CV_8UC1);

    matBGRA = new Mat(height, width, CvType.CV_8UC4);
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Helper to create a new OpenCV Mat whose channels and
 * bit-depth mask an existing Mat.//from w w  w .  j  a  va  2  s  .c o  m
 * 
 * @param m
 *       The Mat to match
 * @return
 *       A new Mat
 */
public static Mat imitate(Mat m) {
    return new Mat(m.height(), m.width(), m.type());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public void findSobelEdges(int dx, int dy) {
    Mat sobeled = new Mat(getCurrentMat().height(), getCurrentMat().width(), CvType.CV_32F);
    Imgproc.Sobel(getCurrentMat(), sobeled, CvType.CV_32F, dx, dy);
    sobeled.convertTo(getCurrentMat(), getCurrentMat().type());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /*from  w  w  w .  ja va2  s .  co m*/
 * @param src
 *       A Mat of type 8UC4 with channels arranged as BGRA.
 * @return
 *       A Mat of type 8UC1 in grayscale.
 */
public static Mat gray(Mat src) {
    Mat result = new Mat(src.height(), src.width(), CvType.CV_8UC1);
    Imgproc.cvtColor(src, result, Imgproc.COLOR_BGRA2GRAY);

    return result;
}