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

Source Link

Usage

From source file:fuzzycv.MainFrame.java

/**
 * Get the average value of the histogram representing the image Hue
 * component//from  w w  w.  j  a v  a  2  s  .  co  m
 *
 * @param hsvImg
 *            the current frame in HSV
 * @param hueValues
 *            the Hue component of the current frame
 * @return the average value
 */
private double getHistoAvg(Mat hsvImg, Mat hueValues) {
    double average = 0.0;
    Mat hist_hue = new Mat();
    MatOfInt histSize = new MatOfInt(180);
    List<Mat> hue = new ArrayList<>();
    hue.add(hueValues);

    //compute the histogram
    Imgproc.calcHist(hue, new MatOfInt(0), new Mat(), hist_hue, histSize, new MatOfFloat(0, 179), true);
    // get the average for each bin
    for (int h = 0; h < 180; h++) {
        average += (hist_hue.get(h, 0)[0] * h);
    }
    return average = average / hsvImg.size().height / hsvImg.size().width;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Dilate the image. Dilation is a morphological operation (i.e. it affects the shape) often used to
 * close holes in contours. It expands white areas of the image.
 * /*  www .  j a  va 2  s.  c om*/
 * See:
 * http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)
 * 
 */
public void dilate() {
    Imgproc.dilate(getCurrentMat(), getCurrentMat(), new Mat());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Erode the image. Erosion  is a morphological operation (i.e. it affects the shape) often used to
 * close holes in contours. It contracts white areas of the image.
 * /*from  w ww  . j  av a 2 s  .c o  m*/
 * See:
 * http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#erode(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)
 * 
 */
public void erode() {
    Imgproc.erode(getCurrentMat(), getCurrentMat(), new Mat());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public ArrayList<Contour> findContours(boolean findHoles, boolean sort) {
    ArrayList<Contour> result = new ArrayList<Contour>();

    ArrayList<MatOfPoint> contourMat = new ArrayList<MatOfPoint>();
    try {//from  www .  j  av a  2  s  . c  om
        int contourFindingMode = (findHoles ? Imgproc.RETR_LIST : Imgproc.RETR_EXTERNAL);

        Imgproc.findContours(getCurrentMat(), contourMat, new Mat(), contourFindingMode,
                Imgproc.CHAIN_APPROX_NONE);
    } catch (CvException e) {
        PApplet.println("ERROR: findContours only works with a gray image.");
    }
    for (MatOfPoint c : contourMat) {
        result.add(new Contour(parent, c));
    }

    if (sort) {
        Collections.sort(result, new ContourComparator());
    }

    return result;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public ArrayList<Line> findLines(int threshold, double minLineLength, double maxLineGap) {
    ArrayList<Line> result = new ArrayList<Line>();

    Mat lineMat = new Mat();
    Imgproc.HoughLinesP(getCurrentMat(), lineMat, 1, PConstants.PI / 180.0, threshold, minLineLength,
            maxLineGap);//from  w  ww.j  a  v a 2s. c  o m
    for (int i = 0; i < lineMat.width(); i++) {
        double[] coords = lineMat.get(0, i);
        result.add(new Line(coords[0], coords[1], coords[2], coords[3]));
    }

    return result;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public Histogram findHistogram(Mat mat, int numBins, boolean normalize) {

    MatOfInt channels = new MatOfInt(0);
    MatOfInt histSize = new MatOfInt(numBins);
    float[] r = { 0f, 256f };
    MatOfFloat ranges = new MatOfFloat(r);
    Mat hist = new Mat();

    ArrayList<Mat> images = new ArrayList<Mat>();
    images.add(mat);/*from   ww  w .  j ava2 s  .c om*/

    Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);

    if (normalize) {
        Core.normalize(hist, hist);
    }

    return new Histogram(parent, hist);
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Convert an OpenCV Mat object into a PImage
 * to be used in other Processing code.//from   w  ww .  j av  a 2 s . c o  m
 * Copies the Mat's pixel data into the PImage's pixel array.
 * Iterates over each pixel in the Mat, i.e. expensive.
 * 
 * (Mainly used internally by OpenCV. Inspired by toCv()
 * from KyleMcDonald's ofxCv.)
 * 
 * @param m
 *          A Mat you want converted
 * @param img
 *          The PImage you want the Mat converted into.
 */
public void toPImage(Mat m, PImage img) {
    img.loadPixels();

    if (m.channels() == 3) {
        Mat m2 = new Mat();
        Imgproc.cvtColor(m, m2, Imgproc.COLOR_RGB2RGBA);
        img.pixels = matToARGBPixels(m2);
    } else if (m.channels() == 1) {
        Mat m2 = new Mat();
        Imgproc.cvtColor(m, m2, Imgproc.COLOR_GRAY2RGBA);
        img.pixels = matToARGBPixels(m2);
    } else if (m.channels() == 4) {
        img.pixels = matToARGBPixels(m);
    }

    img.updatePixels();
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public void dilate() {
    Imgproc.dilate(getCurrentMat(), getCurrentMat(), new Mat());
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public void erode() {
    Imgproc.erode(getCurrentMat(), getCurrentMat(), new Mat());
}

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

License:Apache License

static ArrayList<double[][][]> getOpticalHistograms(Path filename, int w_d, int h_d, int o_d)
        throws PoTException {
    ArrayList<double[][][]> histograms = new ArrayList<double[][][]>();

    VideoCapture capture = new VideoCapture(filename.toString());

    if (!capture.isOpened()) {
        LOG.warning("video file " + filename.getFileName() + " could not be opened.");

        double[][][] hist = new double[w_d][h_d][o_d];
        histograms.add(hist);//from ww  w .j  a v a2 s  .c  om
    } else {
        // variables for processing images
        Mat original_frame = new Mat();

        Mat frame = new Mat();
        Mat frame_gray = new Mat();
        Mat prev_frame_gray = new Mat();
        MatOfPoint2f flow = new MatOfPoint2f();

        // computing a list of histogram of optical flows (i.e. a list of 5*5*8
        // arrays)
        for (int frame_index = 0;; frame_index++) {
            // capturing the video images
            capture.read(original_frame);

            if (original_frame.empty()) {
                if (original_frame.empty()) {
                    if (frame_index == 0) {
                        throw new PoTException("Could not read the video file");
                    } else
                        break;
                }
            } else {
                // resizing the captured frame and converting it to the gray scale
                // image.
                Imgproc.resize(original_frame, frame, new Size(frame_width, frame_height));
                Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);

                double[][][] hist = new double[w_d][h_d][o_d];
                histograms.add(hist);

                // from frame #2
                if (frame_index > 0) {
                    // calculate optical flows
                    Video.calcOpticalFlowFarneback(prev_frame_gray, frame_gray, flow, 0.5, 1, 10, 2, 7, 1.5, 0); // 0.5, 1, 15, 2, 7, 1.5, 0

                    // update histogram of optical flows
                    updateOpticalHistogram(histograms.get(frame_index), flow);
                }

                Mat temp_frame = prev_frame_gray;
                prev_frame_gray = frame_gray;
                frame_gray = temp_frame;
            }
        }

        capture.release();
    }

    return histograms;
}