Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpKltOpencv.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented
32 * with opencv.
33 */
34
41
42#ifndef VP_KLT_OPENCV_H
43#define VP_KLT_OPENCV_H
44
45#include <visp3/core/vpConfig.h>
46
47#if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
48
49#include <visp3/core/vpColor.h>
50#include <visp3/core/vpImage.h>
51
52#include <opencv2/highgui/highgui.hpp>
53#include <opencv2/imgproc/imgproc.hpp>
54#include <opencv2/video/tracking.hpp>
55
56#if defined(VISP_HAVE_NLOHMANN_JSON)
57#include VISP_NLOHMANN_JSON(json.hpp)
58#endif
59
82class VISP_EXPORT vpKltOpencv
83{
84public:
92 vpKltOpencv(const vpKltOpencv &copy);
96 virtual ~vpKltOpencv();
97
105 void addFeature(const float &x, const float &y);
106
118 void addFeature(const long &id, const float &x, const float &y);
119
125 void addFeature(const cv::Point2f &f);
126
134 void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::red, unsigned int thickness = 1) const;
143 static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
144 const vpColor &color = vpColor::green, unsigned int thickness = 1);
153 static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
154 const vpColor &color = vpColor::green, unsigned int thickness = 1);
164 static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
165 const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
166 unsigned int thickness = 1);
176 static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
177 const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
178 unsigned int thickness = 1);
179
181 int getBlockSize() const { return m_blockSize; }
193 void getFeature(const int &index, long &id, float &x, float &y) const;
195 std::vector<cv::Point2f> getFeatures() const { return m_points[1]; }
196 // CvPoint2D32f* getFeatures() const {return features;}
198 std::vector<long> getFeaturesId() const { return m_points_id; }
199 // long* getFeaturesId() const {return featuresid;}
201 double getHarrisFreeParameter() const { return m_harris_k; }
203 // bool *getListOfLostFeature() const { return lostDuringTrack; }
205 int getMaxFeatures() const { return m_maxCount; }
208 double getMinDistance() const { return m_minDistance; }
210 int getNbFeatures() const { return static_cast<int>(m_points[1].size()); }
212 int getNbPrevFeatures() const { return static_cast<int>(m_points[0].size()); }
213 // void getPrevFeature(int index, int &id, float &x, float &y) const;
215 std::vector<cv::Point2f> getPrevFeatures() const { return m_points[0]; }
216 // CvPoint2D32f* getPrevFeatures() const {return prev_features;}
218 // long* getPrevFeaturesId() const {return prev_featuresid;}
220 int getPyramidLevels() const { return m_pyrMaxLevel; }
223 double getQuality() const { return m_qualityLevel; }
225 int getWindowSize() const { return m_winSize; }
226
237 void initTracking(const cv::Mat &I, const cv::Mat &mask = cv::Mat());
238
246 void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
247
256 void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
257
261 vpKltOpencv &operator=(const vpKltOpencv &copy);
262
268 void track(const cv::Mat &I);
269
279 void setBlockSize(int blockSize) { m_blockSize = blockSize; }
280
287 void setHarrisFreeParameter(double harris_k) { m_harris_k = harris_k; }
288
301 void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
302
319 void setInitialGuess(const std::vector<cv::Point2f> &init_pts, const std::vector<cv::Point2f> &guess_pts,
320 const std::vector<long> &fid);
327 void setMaxFeatures(int maxCount) { m_maxCount = maxCount; }
328
336 void setMinDistance(double minDistance) { m_minDistance = minDistance; }
337
345 void setMinEigThreshold(double minEigThreshold) { m_minEigThreshold = minEigThreshold; }
346
355 void setPyramidLevels(int pyrMaxLevel) { m_pyrMaxLevel = pyrMaxLevel; }
356
368 void setQuality(double qualityLevel) { m_qualityLevel = qualityLevel; }
369
372 void setTrackerId(int tid) { (void)tid; }
373
380 void setUseHarris(int useHarrisDetector) { m_useHarrisDetector = useHarrisDetector; }
381
389 void setWindowSize(int winSize) { m_winSize = winSize; }
390
396 void suppressFeature(const int &index);
397
398#ifdef VISP_HAVE_NLOHMANN_JSON
399 friend void to_json(nlohmann::json &j, const vpKltOpencv &array);
400 friend void from_json(const nlohmann::json &j, vpKltOpencv &array);
401#endif
402
403protected:
404 cv::Mat m_gray;
405 cv::Mat m_prevGray;
406 std::vector<cv::Point2f> m_points[2];
407 std::vector<long> m_points_id;
409 cv::TermCriteria m_termcrit;
414 double m_harris_k;
420};
421
422#ifdef VISP_HAVE_NLOHMANN_JSON
423inline void to_json(nlohmann::json &j, const vpKltOpencv &klt)
424{
425 j = nlohmann::json {
426 {"maxFeatures", klt.getMaxFeatures()},
427 {"windowSize", klt.getWindowSize()},
428 {"quality", klt.getQuality()},
429 {"minDistance", klt.getMinDistance()},
430 {"useHarris", klt.m_useHarrisDetector},
431 {"harris", klt.getHarrisFreeParameter()},
432 {"blockSize", klt.getBlockSize()},
433 {"pyramidLevels", klt.getPyramidLevels()}
434 };
435}
436
437inline void from_json(const nlohmann::json &j, vpKltOpencv &klt)
438{
439 klt.setMaxFeatures(j.value("maxFeatures", 10000));
440 klt.setWindowSize(j.value("windowSize", 5));
441 klt.setQuality(j.value("quality", 0.01));
442 klt.setMinDistance(j.value("minDistance", 5));
443 klt.setUseHarris(j.value("useHarris", 1));
444 klt.setHarrisFreeParameter(j.value("harris", 0.01));
445 klt.setBlockSize(j.value("blockSize", 3));
446 klt.setPyramidLevels(j.value("pyramidLevels", 3));
447}
448#endif
449
450END_VISP_NAMESPACE
451#endif
452#endif
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
Definition of the vpImage class member functions.
Definition vpImage.h:131
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition vpKltOpencv.h:83
std::vector< long > m_points_id
Keypoint id.
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1) const
int m_useHarrisDetector
true to use Harris detector
int m_maxCount
Max number of keypoints.
double getQuality() const
int getMaxFeatures() const
Get the list of lost feature.
void setBlockSize(int blockSize)
friend void to_json(nlohmann::json &j, const vpKltOpencv &array)
int m_blockSize
Block size.
void setQuality(double qualityLevel)
int getNbPrevFeatures() const
Get the number of previous features.
void setTrackerId(int tid)
int getWindowSize() const
Get the window size used to refine the corner locations.
double m_minDistance
Mins distance between keypoints.
int getNbFeatures() const
Get the number of current features.
cv::TermCriteria m_termcrit
Term criteria.
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
double m_minEigThreshold
Min eigen threshold.
void setHarrisFreeParameter(double harris_k)
int m_pyrMaxLevel
Pyramid max level.
std::vector< cv::Point2f > m_points[2]
Previous [0] and current [1] keypoint location.
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
double m_qualityLevel
Quality level.
bool m_initial_guess
true when initial guess is provided
cv::Mat m_gray
Gray image.
void setMaxFeatures(int maxCount)
void setMinEigThreshold(double minEigThreshold)
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
void addFeature(const float &x, const float &y)
double m_harris_k
Harris parameter.
friend void from_json(const nlohmann::json &j, vpKltOpencv &array)
double getMinDistance() const
int m_winSize
Window criteria.
long m_next_points_id
Id for the newt keypoint.
cv::Mat m_prevGray
Previous gray image.
void setMinDistance(double minDistance)
int getBlockSize() const
Get the size of the averaging block used to track the features.
int getPyramidLevels() const
Get the list of features id.
void setUseHarris(int useHarrisDetector)
void setWindowSize(int winSize)
void setPyramidLevels(int pyrMaxLevel)
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.