[JAVA] Bureau: OpenCV OpticalFlow PyrLK

Goal
Test OpenCV OpticalFlow PyrLK.

OpenCV_OpticalFlowPyrLK.java


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

import javax.swing.*;
import java.util.List;

public class OpenCV_OpticalFlowPyrLK {

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

        JFrame frame2 = new JFrame("OpticalFlowPyrLK");
        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();

        MatOfPoint2f mMOP2fptsThis=new MatOfPoint2f();
        MatOfPoint2f mMOP2fptsPrev=new MatOfPoint2f();
        MatOfPoint2f mMOP2fptsSafe = new MatOfPoint2f();
        MatOfFloat mMOFerr=new MatOfFloat();
        MatOfByte mMOBStatus=new MatOfByte();
        Mat matOpFlowThis = new Mat();
        Mat matOpFlowPrev = new Mat();
        List<Point> pts, corners, cornersThis, cornersPrev;
        List<Byte> byteStatus;
        int  x, y,iLineThickness = 3;
        Point pt, pt1, pt2;
        Scalar colorOpticalFlow = new Scalar(0, 255,0);

        Mat mRgba;


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

                    int qualityLevel = 40;
                    MatOfPoint MOPcorners = new MatOfPoint();

                    if (mMOP2fptsPrev.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);

                        // get prev corners
                        Imgproc.goodFeaturesToTrack(matOpFlowPrev, MOPcorners, qualityLevel, 0.05, 20);
                        mMOP2fptsPrev.fromArray(MOPcorners.toArray());

                        // get safe copy of this corners
                        mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
                    }
                    else
                    {
                        //Log.d("Baz", "Opflow");
                        // 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);

                        // get the corners for this mat
                        Imgproc.goodFeaturesToTrack(matOpFlowThis, MOPcorners, qualityLevel, 0.05, 20);
                        mMOP2fptsThis.fromArray(MOPcorners.toArray());

                        // retrieve the corners from the prev mat
                        // (saves calculating them again)
                        mMOP2fptsSafe.copyTo(mMOP2fptsPrev);

                        // and save this corners for next time through

                        mMOP2fptsThis.copyTo(mMOP2fptsSafe);
                    }


//	               	Parameters:
//	               		prevImg first 8-bit input image
//	               		nextImg second input image
//	               		prevPts vector of 2D points for which the flow needs to be found; point coordinates must be single-precision floating-point numbers.
//	               		nextPts output vector of 2D points (with single-precision floating-point coordinates) containing the calculated new positions of input features in the second image; when OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
//	               		status output status vector (of unsigned chars); each element of the vector is set to 1 if the flow for the corresponding features has been found, otherwise, it is set to 0.
//	               		err output vector of errors; each element of the vector is set to an error for the corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't found then the error is not defined (use the status parameter to find such cases).

                    Video.calcOpticalFlowPyrLK(matOpFlowPrev, matOpFlowThis, mMOP2fptsPrev, mMOP2fptsThis, mMOBStatus, mMOFerr);

                    cornersPrev = mMOP2fptsPrev.toList();
                    cornersThis = mMOP2fptsThis.toList();
                    byteStatus = mMOBStatus.toList();

                    y = byteStatus.size() - 1;

                    for (x = 0; x < y; x++) {
                        if (byteStatus.get(x) == 1) {
                            pt = cornersThis.get(x);
                            pt2 = cornersPrev.get(x);

                            Imgproc.circle(mRgba, pt, 5, colorOpticalFlow, iLineThickness - 1);

                            Imgproc.line(mRgba, pt, pt2, colorOpticalFlow, iLineThickness);
                        }
                    }


                    panel2.setimagewithMat(mRgba);  //
                    frame2.repaint();
                }
                else
                {
                    System.out.println("Pas de rendez-vous supplémentaire!");
                    break;
                }
            }
        }
        return;
    }

}
Result
![opencv_lk_tracking.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/56fdc6aa-f1de-2943-6c41-8a60406cf6e0.jpeg)

Recommended Posts

Bureau: OpenCV OpticalFlow PyrLK
Bureau: OpenCV OpticalFlow Farneback
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: OpenCV Denoise
Bureau: OpenCV Concat
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 Canny
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
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: espace colorimétrique OpenCV
Bureau: Filtre OpenCV SqrBox
Bureau: redimensionner l'image OpenCV
Bureau: Croquis au crayon OpenCV
Bureau: carte des couleurs OpenCV
Bureau: OpenCV Seamless Clone