2014 새로운 추적 알고리즘 KCF 노트 - 계속 (코드 부분)

KCF 추적 은 opencv 3.1 에 통합 되 었 습 니 다. opencvcontrib / tracking 중, opencvcontrib 이것 은 opencv 3.1 을 다시 컴 파일 해 야 get. windows 의 컴 파일 방법 은 다음 과 같 습 니 다.
http://blog.csdn.net/yomo127/article/details/50474955
git 에서 직접 다운로드 할 수 있 습 니 다. 주 소 는 다음 과 같 습 니 다.
https://github.com/Itseez/opencv_contrib
git 에서 직접 다운로드 한 경우, KCF 부분 에 추 가 된 내용 은 다음 과 같 습 니 다. (git log 가능)
/*---------------STEP 1---------------------*/
/* modify this file
*  opencv2/tracking/tracker.hpp
*  and put several lines of snippet similar to 
*  the following:
*/ 
/*------------------------------------------*/

class CV_EXPORTS_W TrackerKCF : public Tracker
{
 public:
  struct CV_EXPORTS Params
  {
    Params();
    void read( const FileNode& /*fn*/ );
    void write( FileStorage& /*fs*/ ) const;
  };

  /** @brief Constructor
    @param parameters KCF parameters TrackerKCF::Params
     */
  BOILERPLATE_CODE("KCF",TrackerKCF);
};


/*---------------STEP 2---------------------*/
/* modify this file
*  src/tracker.cpp
*  add one line in function  
*  Ptr<Tracker> Tracker::create( const String& trackerType )
*/ 
/*------------------------------------------*/

Ptr<Tracker> Tracker::create( const String& trackerType )
{
  BOILERPLATE_CODE("MIL",TrackerMIL);
  BOILERPLATE_CODE("BOOSTING",TrackerBoosting);
  BOILERPLATE_CODE("MEDIANFLOW",TrackerMedianFlow);
  BOILERPLATE_CODE("TLD",TrackerTLD);
  BOILERPLATE_CODE("KCF",TrackerKCF); // add this line!
  return Ptr<Tracker>();
}


/*---------------STEP 3---------------------*/
/* make a new file and paste the snippet below
*  and modify it according to your needs.
*  also make sure to put the LICENSE part.
*  src/trackerKCF.cpp
*/
/*------------------------------------------*/

/*---------------------------
|  TrackerKCFModel
|---------------------------*/
namespace cv{
   /**
  * \brief Implementation of TrackerModel for MIL algorithm
  */
  class TrackerKCFModel : public TrackerModel{
  public:
    TrackerKCFModel(TrackerKCF::Params /*params*/){}
    ~TrackerKCFModel(){}
  protected:
    void modelEstimationImpl( const std::vector<Mat>& responses ){}
    void modelUpdateImpl(){}
  };
} /* namespace cv */


/*---------------------------
|  TrackerKCF
|---------------------------*/
namespace cv{
  
  /*
 * Prototype
 */
  class TrackerKCFImpl : public TrackerKCF{
  public:
    TrackerKCFImpl( const TrackerKCF::Params ¶meters = TrackerKCF::Params() );
    void read( const FileNode& fn );
    void write( FileStorage& fs ) const;
  
  protected:
    bool initImpl( const Mat& image, const Rect2d& boundingBox );
    bool updateImpl( const Mat& image, Rect2d& boundingBox );
    
    TrackerKCF::Params params;
  };
    
  /*
 * Constructor
 */
  Ptr<TrackerKCF> TrackerKCF::createTracker(const TrackerKCF::Params ¶meters){
      return Ptr<TrackerKCFImpl>(new TrackerKCFImpl(parameters));
  }
  TrackerKCFImpl::TrackerKCFImpl( const TrackerKCF::Params ¶meters ) :
      params( parameters )
  {
    isInit = false;
  }
  
  void TrackerKCFImpl::read( const cv::FileNode& fn ){
    params.read( fn );
  }

  void TrackerKCFImpl::write( cv::FileStorage& fs ) const{
    params.write( fs );
  }
  
  
  bool TrackerKCFImpl::initImpl( const Mat& image, const Rect2d& boundingBox ){
    model=Ptr<TrackerKCFModel>(new TrackerKCFModel(params));
    return true;
  }
  bool TrackerKCFImpl::updateImpl( const Mat& image, Rect2d& boundingBox ){return true;}
  
  /*
 * Parameters
 */
  TrackerKCF::Params::Params(){
      
  }

  void TrackerKCF::Params::read( const cv::FileNode& fn ){
    
  }

  void TrackerKCF::Params::write( cv::FileStorage& fs ) const{
    
  }
  
} /* namespace cv */
그리고 다시 한 번 보 겠 습 니 다. / modules / tracking / include / opencv 2 / tracking / tracker. hpp 에서 kcf 에 대한 정 의 는 두 마디 더 하 겠 습 니 다. 이 안 에는 온라인 으로 추적 하 는 MILtracker, OBatrker, 그리고 particle filter ing tracker, TLD tracker, KCF tracker 등 많은 추적 방법 이 정의 되 어 있 습 니 다. 이 Tracker 의 성명 은 다음 과 같 습 니 다.
class CV_EXPORTS_W Tracker : public virtual Algorithm
{
 public:

  virtual ~Tracker();

  /** @brief Initialize the tracker with a know bounding box that surrounding the target
    @param image The initial frame
    @param boundingBox The initial boundig box

    @return True if initialization went succesfully, false otherwise
     */
  CV_WRAP bool init( const Mat& image, const Rect2d& boundingBox );

  /** @brief Update the tracker, find the new most likely bounding box for the target
    @param image The current frame
    @param boundingBox The boundig box that represent the new target location, if true was returned, not
    modified otherwise

    @return True means that target was located and false means that tracker cannot locate target in
    current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
    missing from the frame (say, out of sight)
     */
  CV_WRAP bool update( const Mat& image, CV_OUT Rect2d& boundingBox );

  /** @brief Creates a tracker by its name.
    @param trackerType Tracker type

    The following detector types are supported:

    -   "MIL" -- TrackerMIL
    -   "BOOSTING" -- TrackerBoosting
     */
  CV_WRAP static Ptr<Tracker> create( const String& trackerType );

  virtual void read( const FileNode& fn )=0;
  virtual void write( FileStorage& fs ) const=0;

  Ptr<TrackerModel> getModel()
  {
	  return model;
  }

 protected:

  virtual bool initImpl( const Mat& image, const Rect2d& boundingBox ) = 0;
  virtual bool updateImpl( const Mat& image, Rect2d& boundingBox ) = 0;

  bool isInit;

  Ptr<TrackerFeatureSet> featureSet;
  Ptr<TrackerSampler> sampler;
  Ptr<TrackerModel> model;
};

다음은 Tracker 라 는 KCF Tracker 를 계승 한 이 알고리즘 의 논문 이 이전 블 로그 에 링크 되 어 있 습 니 다.
/** @brief KCF is a novel tracking framework that utilizes properties of circulant matrix to enhance the processing speed.
 * This tracking method is an implementation of @cite KCF_ECCV which is extended to KFC with color-names features (@cite KCF_CN).
 * The original paper of KCF is available at <http://home.isr.uc.pt/~henriques/circulant/index.html>
 * as well as the matlab implementation. For more information about KCF with color-names features, please refer to
 * <http://www.cvl.isy.liu.se/research/objrec/visualtracking/colvistrack/index.html>.
 */
class CV_EXPORTS TrackerKCF : public Tracker
{
public:
	/**
	* \brief Feature type to be used in the tracking grayscale, colornames, compressed color-names
	* The modes available now:
	-   "GRAY" -- Use grayscale values as the feature
	-   "CN" -- Color-names feature
	*/
	enum MODE {
		GRAY = (1u << 0),
		CN = (1u << 1),
		CUSTOM = (1u << 2)
	};

	struct CV_EXPORTS Params
	{
		/**
		* \brief Constructor
		*/
		Params();

		/**
		* \brief Read parameters from file, currently unused
		*/
		void read(const FileNode& /*fn*/);

		/**
		* \brief Read parameters from file, currently unused
		*/
		void write(FileStorage& /*fs*/) const;

		double sigma;                 //!<  gaussian kernel bandwidth
		double lambda;                //!<  regularization
		double interp_factor;         //!<  linear interpolation factor for adaptation
		double output_sigma_factor;   //!<  spatial bandwidth (proportional to target)
		double pca_learning_rate;     //!<  compression learning rate
		bool resize;                  //!<  activate the resize feature to improve the processing speed
		bool split_coeff;             //!<  split the training coefficients into two matrices
		bool wrap_kernel;             //!<  wrap around the kernel values
		bool compress_feature;        //!<  activate the pca method to compress the features
		int max_patch_size;           //!<  threshold for the ROI size
		int compressed_size;          //!<  feature size after compression
		unsigned int desc_pca;        //!<  compressed descriptors of TrackerKCF::MODE
		unsigned int desc_npca;       //!<  non-compressed descriptors of TrackerKCF::MODE
	};

	virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false);

	/** @brief Constructor
	@param parameters KCF parameters TrackerKCF::Params
	*/
	BOILERPLATE_CODE("KCF", TrackerKCF);
};

자, 위 에 서 는 opencv 에서 KCF 의 인 터 페 이 스 를 소개 하 였 을 뿐 입 니 다. 다음은 예 를 들 어 어떻게 호출 하 는 지 보 겠 습 니 다.
//     opencv3.1
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>

using namespace std;
using namespace cv;

int main( ){
  // declares all required variables
  //! [vars]
  Rect2d roi;
  Mat frame;
  //! [vars]

  // create a tracker object
  //! create           -   "MIL" -- TrackerMIL
   //! -   "BOOSTING" -- TrackerBoosting ,     "TLD","MEDIANFLOW"
  Ptr<Tracker> tracker = Tracker::create( "KCF" );
  //! [create]

  // set input video
  //! [setvideo]
  std::string video = "test.avi";
  VideoCapture cap(video);
  //! [setvideo]

  // get bounding box
  //! [getframe]
  cap >> frame;
  //! [getframe]
  //! [selectroi]    roi GUI   
  roi=selectROI("tracker",frame);
  //! [selectroi]

  //quit if ROI was not selected
  if(roi.width==0 || roi.height==0)
    return 0;

  // initialize the tracker
  //! [init]
  tracker->init(frame,roi);
  //! [init]

  // perform the tracking process
  printf("Start the tracking process
"); for ( ;; ){ // get frame from the video cap >> frame; // stop the program if no more images if(frame.rows==0 || frame.cols==0) break; // update the tracking result //! [update] tracker->update(frame,roi); //! [update] //! [visualization] // draw the tracked object rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 ); // show image with the tracked object imshow("tracker",frame); //! [visualization] //quit on ESC button if(waitKey(1)==27)   break; } return 0; }

좋은 웹페이지 즐겨찾기