LK	
  	
  matching
                            SURF	
  matching
                             Kalman	
  Filter



OpenCV를 활용한 추적
   특징점 기반 추적


           Daesik	
  Jang
      dsjang@kunsan.ac.kr



                                               1
1.	
  LK	
  기반의	
  특징점	
  매칭


        • goodFeaturesToTrack	
  :	
  특징점	
  추출
        • calcOpBcalFlowPyrLK	
  :	
  LK	
  알고리즘을	
  기반으로	
  주어진	
  feature-­‐
          prev	
  특징점들을	
  image_next에서	
  찾아	
  features_next에	
  저장
                                                                                         //	
  Tracker	
  is	
  initialised	
  and	
  initial	
  features	
  are	
  stored	
  in	
  features_next
                                                                                         //	
  Now	
  iterate	
  through	
  rest	
  of	
  images
//	
  Obtain	
  first	
  image	
  and	
  set	
  up	
  two	
  feature	
  vectors          for(;;)
cv::Mat	
  image_prev,	
  image_next;                                                    {
std::vector<cv::Point>	
  features_prev,	
  features_next;                               	
  	
  	
  	
  image_prev	
  =	
  image_next.clone();
                                                                                         	
  	
  	
  	
  feature_prev	
  =	
  features_next;
image_next	
  =	
  getImage();                                                           	
  	
  	
  	
  image_next	
  =	
  getImage();	
  	
  //	
  Get	
  next	
  image

//	
  Obtain	
  initial	
  set	
  of	
  features                                         	
  	
  	
  	
  //	
  Find	
  position	
  of	
  feature	
  in	
  new	
  image
cv::goodFeaturesToTrack(image_next,	
  //	
  the	
  image	
                              	
  	
  	
  	
  cv::calcOpticalFlowPyrLK(
	
  	
  features_next,	
  	
  	
  //	
  the	
  output	
  detected	
  features            	
  	
  	
  	
  	
  	
  image_prev,	
  image_next,	
  //	
  2	
  consecutive	
  images
	
  	
  max_count,	
  	
  //	
  the	
  maximum	
  number	
  of	
  features	
             	
  	
  	
  	
  	
  	
  feature_prev,	
  //	
  input	
  point	
  positions	
  in	
  first	
  im
	
  	
  qlevel,	
  	
  	
  	
  	
  //	
  quality	
  level                                	
  	
  	
  	
  	
  	
  features_next,	
  //	
  output	
  point	
  positions	
  in	
  the	
  2nd
	
  	
  minDist	
  	
  	
  	
  	
  //	
  min	
  distance	
  between	
  two	
  features   	
  	
  	
  	
  	
  	
  status,	
  	
  	
  	
  //	
  tracking	
  success
);                                                                                       	
  	
  	
  	
  	
  	
  err	
  	
  	
  	
  	
  	
  //	
  tracking	
  error
                                                                                         	
  	
  	
  	
  );

                                                                                         	
  	
  	
  	
  if	
  (	
  stopTracking()	
  )	
  break;
                                                                                         }




                                                                                                                                                                                            2
2.	
  SURF	
  특징을	
  이용한	
  매칭

• SurfFeatureDetector	
  클래스와	
  detect()	
  메소드를	
  이용한	
  SURF	
  
  특징	
  추출
  – 추출된	
  특징은	
  keypoints	
  변수에	
  저장됨

               // vector of keypoints
                  std::vector<cv::KeyPoint> keypoints;
                  // Construct the SURF feature detector object
                  cv::SurfFeatureDetector surf(
                     2500.); // threshold
                  // Detect the SURF features
                  surf.detect(image,keypoints);

        // Draw the keypoints with scale and orientation information
           cv::drawKeypoints(image, // original image
             keypoints, // vector of keypoints

           featureImage, // the resulting image
           cv::Scalar(255,255,255),
           cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); //flag           3
2.	
  SURF	
  특징을	
  이용한	
  매칭




                                 4
2.	
  SURF	
  특징을	
  이용한	
  매칭

• SurfDescriptorExtractor	
  클래스를	
  이용하여	
  keypoint로부터	
  
  특징값을	
  추출하여	
  descriptor에	
  저장

             // Construction of the SURF descriptor extractor
                 cv::SurfDescriptorExtractor surfDesc;
                 // Extraction of the SURF descriptors
                 cv::Mat descriptors1;
                 surfDesc.compute(image1,keypoints1,descriptors1);




                                                                     5
2.	
  SURF	
  특징을	
  이용한	
  매칭


• BruteForceMatcher	
  클래스를	
  이용하여	
  descriptor들	
  사이의	
  매칭	
  
  결과를	
  matches에	
  저장한다.




              // Construction of the matcher
              cv::BruteForceMatcher<cv::L2<float>> matcher;
              // Match the two image descriptors
              std::vector<cv::DMatch> matches;
              matcher.match(descriptors1,descriptors2, matches);




                                                                      6
2.	
  SURF	
  특징을	
  이용한	
  매칭



• keypoint들과	
  matching	
  	
  결과를	
  선으로	
  연결하여	
  그림으로	
  
  그린다.

             cv::Mat imageMatches;
             cv::drawMatches(
              image1,keypoints1, // 1st image and its keypoints
              image2,keypoints2, // 2nd image and its keypoints
              matches,        // the matches
              imageMatches,       // the image produced
              cv::Scalar(255,255,255)); // color of the lines




                                                                  7
2.	
  SURF	
  특징을	
  이용한	
  매칭


• SURF	
  특징들의	
  매칭	
  결과




                                 8
3.	
  OpenCV의	
  Kalman	
  Filter를	
  이용한	
  예측


• KalmanFilter	
  객체의	
  시스템	
  설정

      KalmanFilter	
  KF(4,	
  2,	
  0);
      KF.transiBonMatrix	
  =	
  *(Mat_<float>(4,	
  4)	
  <<	
  1,0,1,0,	
  	
  	
  0,1,0,1,	
  	
  0,0,1,0,	
  	
  0,0,0,1);
      Mat_<float>	
  measurement(2,1);	
  measurement.setTo(Scalar(0));
      	
  
      //	
  init...
      KF.statePre.at<float>(0)	
  =	
  mouse_info.x;
      KF.statePre.at<float>(1)	
  =	
  mouse_info.y;
      KF.statePre.at<float>(2)	
  =	
  0;
      KF.statePre.at<float>(3)	
  =	
  0;
      setIdenBty(KF.measurementMatrix);
      setIdenBty(KF.processNoiseCov,	
  Scalar::all(1e-­‐4));
      setIdenBty(KF.measurementNoiseCov,	
  Scalar::all(1e-­‐1));
      setIdenBty(KF.errorCovPost,	
  Scalar::all(.1));


                                                                                                                                9
3.	
  OpenCV의	
  Kalman	
  Filter를	
  이용한	
  예측


• KF	
  객체를	
  이용한	
  예측과	
  수정
  //	
  First	
  predict,	
  to	
  update	
  the	
  internal	
  statePre	
  variable
  Mat	
  prediction	
  =	
  KF.predict();
  Point	
  
  predictPt(prediction.at<float>(0),prediction.at<float>(1));
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
  //	
  Get	
  mouse	
  point
  measurement(0)	
  =	
  mouse_info.x;
  measurement(1)	
  =	
  mouse_info.y;
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
  Point	
  measPt(measurement(0),measurement(1));
  	
  
  //	
  The	
  "correct"	
  phase	
  that	
  is	
  going	
  to	
  use	
  the	
  predicted	
  value	
  
  and	
  our	
  measurement
  Mat	
  estimated	
  =	
  KF.correct(measurement);
  Point	
  statePt(estimated.at<float>(0),estimated.at<float>(1));

                                                                                                         10

Open cv를 활용한 tracking

  • 1.
    LK    matching SURF  matching Kalman  Filter OpenCV를 활용한 추적 특징점 기반 추적 Daesik  Jang dsjang@kunsan.ac.kr 1
  • 2.
    1.  LK  기반의  특징점  매칭 • goodFeaturesToTrack  :  특징점  추출 • calcOpBcalFlowPyrLK  :  LK  알고리즘을  기반으로  주어진  feature-­‐ prev  특징점들을  image_next에서  찾아  features_next에  저장 //  Tracker  is  initialised  and  initial  features  are  stored  in  features_next //  Now  iterate  through  rest  of  images //  Obtain  first  image  and  set  up  two  feature  vectors for(;;) cv::Mat  image_prev,  image_next; { std::vector<cv::Point>  features_prev,  features_next;        image_prev  =  image_next.clone();        feature_prev  =  features_next; image_next  =  getImage();        image_next  =  getImage();    //  Get  next  image //  Obtain  initial  set  of  features        //  Find  position  of  feature  in  new  image cv::goodFeaturesToTrack(image_next,  //  the  image          cv::calcOpticalFlowPyrLK(    features_next,      //  the  output  detected  features            image_prev,  image_next,  //  2  consecutive  images    max_count,    //  the  maximum  number  of  features              feature_prev,  //  input  point  positions  in  first  im    qlevel,          //  quality  level            features_next,  //  output  point  positions  in  the  2nd    minDist          //  min  distance  between  two  features            status,        //  tracking  success );            err            //  tracking  error        );        if  (  stopTracking()  )  break; } 2
  • 3.
    2.  SURF  특징을  이용한  매칭 • SurfFeatureDetector  클래스와  detect()  메소드를  이용한  SURF   특징  추출 – 추출된  특징은  keypoints  변수에  저장됨 // vector of keypoints std::vector<cv::KeyPoint> keypoints; // Construct the SURF feature detector object cv::SurfFeatureDetector surf( 2500.); // threshold // Detect the SURF features surf.detect(image,keypoints); // Draw the keypoints with scale and orientation information cv::drawKeypoints(image, // original image keypoints, // vector of keypoints featureImage, // the resulting image cv::Scalar(255,255,255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); //flag 3
  • 4.
    2.  SURF  특징을  이용한  매칭 4
  • 5.
    2.  SURF  특징을  이용한  매칭 • SurfDescriptorExtractor  클래스를  이용하여  keypoint로부터   특징값을  추출하여  descriptor에  저장 // Construction of the SURF descriptor extractor cv::SurfDescriptorExtractor surfDesc; // Extraction of the SURF descriptors cv::Mat descriptors1; surfDesc.compute(image1,keypoints1,descriptors1); 5
  • 6.
    2.  SURF  특징을  이용한  매칭 • BruteForceMatcher  클래스를  이용하여  descriptor들  사이의  매칭   결과를  matches에  저장한다. // Construction of the matcher cv::BruteForceMatcher<cv::L2<float>> matcher; // Match the two image descriptors std::vector<cv::DMatch> matches; matcher.match(descriptors1,descriptors2, matches); 6
  • 7.
    2.  SURF  특징을  이용한  매칭 • keypoint들과  matching    결과를  선으로  연결하여  그림으로   그린다. cv::Mat imageMatches; cv::drawMatches( image1,keypoints1, // 1st image and its keypoints image2,keypoints2, // 2nd image and its keypoints matches, // the matches imageMatches, // the image produced cv::Scalar(255,255,255)); // color of the lines 7
  • 8.
    2.  SURF  특징을  이용한  매칭 • SURF  특징들의  매칭  결과 8
  • 9.
    3.  OpenCV의  Kalman  Filter를  이용한  예측 • KalmanFilter  객체의  시스템  설정 KalmanFilter  KF(4,  2,  0); KF.transiBonMatrix  =  *(Mat_<float>(4,  4)  <<  1,0,1,0,      0,1,0,1,    0,0,1,0,    0,0,0,1); Mat_<float>  measurement(2,1);  measurement.setTo(Scalar(0));   //  init... KF.statePre.at<float>(0)  =  mouse_info.x; KF.statePre.at<float>(1)  =  mouse_info.y; KF.statePre.at<float>(2)  =  0; KF.statePre.at<float>(3)  =  0; setIdenBty(KF.measurementMatrix); setIdenBty(KF.processNoiseCov,  Scalar::all(1e-­‐4)); setIdenBty(KF.measurementNoiseCov,  Scalar::all(1e-­‐1)); setIdenBty(KF.errorCovPost,  Scalar::all(.1)); 9
  • 10.
    3.  OpenCV의  Kalman  Filter를  이용한  예측 • KF  객체를  이용한  예측과  수정 //  First  predict,  to  update  the  internal  statePre  variable Mat  prediction  =  KF.predict(); Point   predictPt(prediction.at<float>(0),prediction.at<float>(1));                           //  Get  mouse  point measurement(0)  =  mouse_info.x; measurement(1)  =  mouse_info.y;                           Point  measPt(measurement(0),measurement(1));   //  The  "correct"  phase  that  is  going  to  use  the  predicted  value   and  our  measurement Mat  estimated  =  KF.correct(measurement); Point  statePt(estimated.at<float>(0),estimated.at<float>(1)); 10