Example usage for org.opencv.video Video calcOpticalFlowFarneback

List of usage examples for org.opencv.video Video calcOpticalFlowFarneback

Introduction

In this page you can find the example usage for org.opencv.video Video calcOpticalFlowFarneback.

Prototype

public static void calcOpticalFlowFarneback(Mat prev, Mat next, Mat flow, double pyr_scale, int levels,
            int winsize, int iterations, int poly_n, double poly_sigma, int flags) 

Source Link

Usage

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  www.java  2 s  . co  m
    } 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;
}

From source file:org.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[][][]>();

    try {/*from  w w w .ja  va 2 s . c om*/
        LOG.info("opening video file " + filename.toString());
        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);
        } 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 (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();
        }
    } catch (Exception e) {
        e.printStackTrace();
        LOG.log(Level.SEVERE, "Exception in getOpticalHistograms ", e);
    }
    return histograms;
}