Opencv 关键点和描述符(一)—— 关键点及跟踪基础
Opencv 关键点和描述符(三)—— 核心关键点检测方法
关键点就是一小块图像,而描述符是一种数学结构,通常为一组浮点数。而如何更好地将图像信息抽象为描述符将是一个主要的问题。同时,对于描述符,它应该能够针对不同的场景,给出一定的旋转不变性。
关键点和描述符的是三个主要的应用场景就是跟踪,目标识别和立体建模。不过不管是哪种用途,根本的处理逻辑确实类似的:首先找出图像中的关键点,之后选取一种描述符,最后基于关键点的描述符查找匹配 —— detect-describe-match。
为了描述关键点,Opencv 关键点的类定义如下
class cv::KeyPoint {
public:
cv::Point2f pt; // coordinates of the keypoint
float size; // diameter of the meaningful keypoint neighborhood
float angle; // computed orientation of the keypoint (-1 if none)
float response; // response for which the keypoints was selected
int octave; // octave (pyramid layer) keypoint was extracted from
int class_id; // object id, can be used to cluster keypoints by object
cv::KeyPoint(
cv::Point2f _pt,
float _size,
float _angle = -1,
float _response = 0,
int _octave = 0,
int _class_id = -1
);
cv::KeyPoint(
float x,
float y,
float _size,
float _angle = -1,
float _response = 0,
int _octave = 0,
int _class_id = -1
);
...
};
参数说明:
为了查找并计算描述符,Opencv 定义了如下抽象类。
class cv::Feature2D : public cv::Algorithm {
public:
virtual void detect(
cv::InputArray image, // Image on which to detect
vector< cv::KeyPoint >& keypoints, // Array of found keypoints
cv::InputArray mask = cv::noArray()
) const;
virtual void detect(
cv::InputArrayOfArrays images, // Images on which to detect
vector >& keypoints, // keypoints for each image
cv::InputArrayOfArrays masks = cv::noArray()
) const;
virtual void compute(
cv::InputArray image, // Image where keypoints are located
std::vector& keypoints, // input/output vector of keypoints
cv::OutputArray descriptors); // computed descriptors, M x N matrix,
// where M is the number of keypoints
// and N is the descriptor size
virtual void compute(
cv::InputArrayOfArrays image, // Images where keypoints are located
std::vector >& keypoints, //I/O vec of keypnts
cv::OutputArrayOfArrays descriptors); // computed descriptors,
// vector of (Mi x N) matrices, where
// Mi is the number of keypoints in
// the i-th image and N is the
// descriptor size
virtual void detectAndCompute(
cv::InputArray image, // Image on which to detect
cv::InputArray mask, // Optional region of interest mask
std::vector& keypoints, // found or provided keypoints
cv::OutputArray descriptors, // computed descriptors
bool useProvidedKeypoints = false); // if true,
// the provided keypoints are used,
// otherwise they are detected
virtual int descriptorSize() const; // size of each descriptor in elements
virtual int descriptorType() const; // type of descriptor elements
virtual int defaultNorm() const; // the recommended norm to be used
// for comparing descriptors.
// Usually, it's NORM_HAMMING for
// binary descriptors and NORM_L2
// for all others.
virtual void read(const cv::FileNode&);
virtual void write(cv::FileStorage&) const;
...
};
函数说明:
一个实际的实现可以只实现其中的某个或几个方法:
通常,一个匹配器尝试在一副或一组图中匹配一幅图中的关键点,如果匹配成功,将返回 cv::DMatch 的列表。
class cv::DMatch {
public:
DMatch(); // sets this->distance
// to std::numeric_limits::max()
DMatch(int _queryIdx, int _trainIdx, float _distance);
DMatch(int _queryIdx, int _trainIdx, int _imgIdx, float _distance);
int queryIdx; // query descriptor index
int trainIdx; // train descriptor index
int imgIdx; // train image index
float distance;
bool operator<(const DMatch &m) const; // Comparison operator
// based on 'distance'
}
成员说明:
通常匹配器被应用在目标识别和跟踪两个场景中。其中目标识别需要我们训练匹配器——给出已知物体最大区分度的描述符,然后根据我们给出的描述符给出字典中哪个描述符与之相匹配。而跟踪则要求在给定两组描述符的条件下,给出它们之间的匹配情况。
cv::DescriptorMatcher 提供了 match(),knnMatch() 和 radiusMatch() 三个函数,其中每个函数都有针对目标检测和跟踪的两个版本,其中识别需要输入一个特性列表和训练好的字典,而跟踪则需输入两个特性列表。
class cv::DescriptorMatcher {
public:
virtual void add(InputArrayOfArrays descriptors); // Add train descriptors
virtual void clear(); // Clear train descriptors
virtual bool empty() const; // true if no descriptors
void train(); // Train matcher
virtual bool isMaskSupported() const = 0; // true if supports masks
const vector& getTrainDescriptors() const; // Get train descriptors
// methods to match descriptors from one list vs. "trained" set (recognition)
//
void match(
InputArray queryDescriptors,
vector& matches,
InputArrayOfArrays masks = noArray()
);
void knnMatch(
InputArray queryDescriptors,
vector< vector >& matches,
int k,
InputArrayOfArrays masks = noArray(),
bool compactResult = false
);
void radiusMatch(
InputArray queryDescriptors,
vector< vector >& matches,
float maxDistance,
InputArrayOfArrays masks = noArray(),
bool compactResult = false
);
// methods to match descriptors from two lists (tracking)
//
// Find one best match for each query descriptor
void match(
InputArray queryDescriptors,
InputArray trainDescriptors,
vector& matches,
InputArray mask = noArray()
) const;
// Find k best matches for each query descriptor (in increasing
// order of distances)
void knnMatch(
InputArray queryDescriptors,
InputArray trainDescriptors,
vector< vector >& matches,
int k,
InputArray mask = noArray(),
bool compactResult = false
) const;
// Find best matches for each query descriptor with distance less
// than maxDistance
void radiusMatch(
InputArray queryDescriptors,
InputArray trainDescriptors,
vector< vector >& matches,
float maxDistance,
InputArray mask = noArray(),
bool compactResult = false
) const;
virtual void read(const FileNode&); // Reads matcher from a file node
virtual void write(FileStorage&) const; // Writes matcher to a file storage
virtual cv::Ptr clone(
bool emptyTrainData = false
) const = 0;
static cv::Ptr create(
const string& descriptorMatcherType
);
...
};
函数说明:
关键点滤波器用于从现有的关键点中查找更佳的关键点或者去除相同的关键点。
class cv::KeyPointsFilter {
public:
static void runByImageBorder(
vector< cv::KeyPoint >& keypoints, // in/out list of keypoints
cv::Size imageSize, // Size of original image
int borderSize // Size of border in pixels
);
static void runByKeypointSize(
vector< cv::KeyPoint >& keypoints, // in/out list of keypoints
float minSize, // Smallest keypoint to keep
float maxSize = FLT_MAX // Largest one to keep
);
static void runByPixelsMask(
vector< cv::KeyPoint >& keypoints, // in/out list of keypoints
const cv::Mat& mask // Keep where mask is nonzero
);
static void removeDuplicated(
vector< cv::KeyPoint >& keypoints // in/out list of keypoints
);
static void retainBest(
vector< cv::KeyPoint >& keypoints, // in/out list of keypoints
int npoints // Keep this many
);
}
函数说明: