[JAVA] Bureau: OpenCV OpticalFlow Farneback

Goal
Test OpenCV opticalflow farneback.

OpenCV_OpticalFlowFarneback.java


import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;

import javax.swing.*;

public class OpenCV_OpticalFlowFarneback {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    public static void main(String arg[]) throws Exception{

        JFrame frame2 = new JFrame("OpticalFlowFarneback");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setSize(640,480);
        frame2.setBounds(300,100, frame2.getWidth()+50, 50+frame2.getHeight());
        Panel panel2 = new Panel();
        frame2.setContentPane(panel2);
        frame2.setVisible(true);
        //-- 2. Read the video stream
        VideoCapture capture = new VideoCapture();
        capture.open(0); //0 Tableau CCD 1ère branche,1 deuxième branche
        Mat webcam_image=new Mat();

        Mat matOpFlowThis = new Mat();
        Mat matOpFlowPrev = new Mat();

        Mat mRgba;
        System.out.println("OPTFLOW_USE_INITIAL_FLOW="+Video.OPTFLOW_USE_INITIAL_FLOW);
        System.out.println("OPTFLOW_FARNEBACK_GAUSSIAN="+Video.OPTFLOW_FARNEBACK_GAUSSIAN);

        capture.read(webcam_image);
        frame2.setSize(webcam_image.width()+40,webcam_image.height()+60);

        if( capture.isOpened())
        {
            while( true )
            {
                Thread.sleep(200);


                capture.read(webcam_image);
                if( !webcam_image.empty() )
                {
                    mRgba=webcam_image.clone();


                    if (matOpFlowPrev.rows() == 0) {

                        // first time through the loop so we need prev and this mats
                        // plus prev points
                        // get this mat
                        Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

                        // copy that to prev mat
                        matOpFlowThis.copyTo(matOpFlowPrev);

                    }
                    else
                    {
                        // we've been through before so
                        // this mat is valid. Copy it to prev mat
                        matOpFlowThis.copyTo(matOpFlowPrev);

                        // get this mat
                        Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

                    }


//	               	Parameters:

                    Mat flow=new Mat(mRgba.size(), CvType.CV_8UC1);

                    //Video.calcOpticalFlowFarneback(matOpFlowPrev, matOpFlowThis, flow, 0.5,1,1,1,7,1.5,1);
                    Video.calcOpticalFlowFarneback(matOpFlowPrev, matOpFlowThis, flow, 0.5,3,15,3,5,1.2,0);


                    //System.out.println(flow.dump());

                    float[] fdata=new float[2];
                    for(int i=0;i<matOpFlowPrev.cols();i=i+10){
                        for(int j=0;j<matOpFlowPrev.rows();j=j+10){

                            flow.get(j, i,fdata);
                            //0.Core.circle(mRgba, new Point(j,i), 1, new Scalar(0, 0, 255), 1);
                            Imgproc.circle(mRgba, new Point(i+(int)fdata[0],j+(int)fdata[1]), 1, new Scalar(0, 0, 255), 1);
                            //0.Core.line(mRgba, new Point(j,i), new Point(j+(int)fdata[0],i+(int)fdata[1]), new Scalar(0, 255, 255) , 1);
                            Imgproc.line(mRgba, new Point(i,j), new Point(i+(int)fdata[0],j+(int)fdata[1]), new Scalar(0, 255, 255) , 1);

                        }
                    }





                    panel2.setimagewithMat(mRgba);  //
                    frame2.repaint();
                }
                else
                {
                    System.out.println("Pas de rendez-vous supplémentaire!");
                    break;
                }
            }
        }
        return;
    }
}
Result
![opencv_farneback.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/b26e1a7b-cb46-fd89-9e26-2e77c3283679.jpeg)

Recommended Posts

Bureau: OpenCV OpticalFlow Farneback
Bureau: OpenCV OpticalFlow PyrLK
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: OpenCV Concat
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: OpenCV Watershed
Bureau: Texte OpenCV
Bureau: OpenCV Inpaint
Bureau: OpenCV NormalizeBlur
Bureau: OpenCV StereoSGBM
Bureau: OpenCV Spot
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
Bureau: Changement d'éclairage OpenCV
Bureau: OpenCV Add WaterMark
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Changement de couleur OpenCV
Bureau: Filtre Freichennel OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV Sobel Filter2
Bureau: OpenCV pyrMeanShift Filter
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: aperçu de la webcam Opencv
Bureau: Filtre laplacien OpenCV
Bureau: Flou médian OpenCV
Bureau: OpenCV Add Broad
Bureau: OpenCV Robinson Filter
Bureau: enregistrement vidéo OpenCV
Bureau: Aplatissement de la texture OpenCV
Bureau: OpenCV Flood Fill
Bureau: Filtre OpenCV SqrBox
Bureau: redimensionner l'image OpenCV