wrapped FlannBasedMatcher (and extended DescriptorMatcher wrapper)
updated feature_homography.py sample to use new features
This commit is contained in:
parent
d174c3db04
commit
028c44531f
@ -2237,24 +2237,24 @@ public:
|
|||||||
* Add descriptors to train descriptor collection.
|
* Add descriptors to train descriptor collection.
|
||||||
* descriptors Descriptors to add. Each descriptors[i] is a descriptors set from one image.
|
* descriptors Descriptors to add. Each descriptors[i] is a descriptors set from one image.
|
||||||
*/
|
*/
|
||||||
virtual void add( const vector<Mat>& descriptors );
|
CV_WRAP virtual void add( const vector<Mat>& descriptors );
|
||||||
/*
|
/*
|
||||||
* Get train descriptors collection.
|
* Get train descriptors collection.
|
||||||
*/
|
*/
|
||||||
const vector<Mat>& getTrainDescriptors() const;
|
CV_WRAP const vector<Mat>& getTrainDescriptors() const;
|
||||||
/*
|
/*
|
||||||
* Clear train descriptors collection.
|
* Clear train descriptors collection.
|
||||||
*/
|
*/
|
||||||
virtual void clear();
|
CV_WRAP virtual void clear();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return true if there are not train descriptors in collection.
|
* Return true if there are not train descriptors in collection.
|
||||||
*/
|
*/
|
||||||
virtual bool empty() const;
|
CV_WRAP virtual bool empty() const;
|
||||||
/*
|
/*
|
||||||
* Return true if the matcher supports mask in match methods.
|
* Return true if the matcher supports mask in match methods.
|
||||||
*/
|
*/
|
||||||
virtual bool isMaskSupported() const = 0;
|
CV_WRAP virtual bool isMaskSupported() const = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Train matcher (e.g. train flann index).
|
* Train matcher (e.g. train flann index).
|
||||||
@ -2267,7 +2267,7 @@ public:
|
|||||||
* if it has not trained yet or if new descriptors have been added to the train
|
* if it has not trained yet or if new descriptors have been added to the train
|
||||||
* collection).
|
* collection).
|
||||||
*/
|
*/
|
||||||
virtual void train();
|
CV_WRAP virtual void train();
|
||||||
/*
|
/*
|
||||||
* Group of methods to match descriptors from image pair.
|
* Group of methods to match descriptors from image pair.
|
||||||
* Method train() is run in this methods.
|
* Method train() is run in this methods.
|
||||||
@ -2291,9 +2291,9 @@ public:
|
|||||||
* Group of methods to match descriptors from one image to image set.
|
* Group of methods to match descriptors from one image to image set.
|
||||||
* See description of similar methods for matching image pair above.
|
* See description of similar methods for matching image pair above.
|
||||||
*/
|
*/
|
||||||
void match( const Mat& queryDescriptors, vector<DMatch>& matches,
|
CV_WRAP void match( const Mat& queryDescriptors, CV_OUT vector<DMatch>& matches,
|
||||||
const vector<Mat>& masks=vector<Mat>() );
|
const vector<Mat>& masks=vector<Mat>() );
|
||||||
void knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,
|
CV_WRAP void knnMatch( const Mat& queryDescriptors, CV_OUT vector<vector<DMatch> >& matches, int k,
|
||||||
const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
|
const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
|
||||||
void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
|
||||||
const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
|
const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
|
||||||
@ -2562,10 +2562,10 @@ void BruteForceMatcher<L2<float> >::radiusMatchImpl( const Mat& queryDescriptors
|
|||||||
/*
|
/*
|
||||||
* Flann based matcher
|
* Flann based matcher
|
||||||
*/
|
*/
|
||||||
class CV_EXPORTS FlannBasedMatcher : public DescriptorMatcher
|
class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
|
CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
|
||||||
const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );
|
const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );
|
||||||
|
|
||||||
virtual void add( const vector<Mat>& descriptors );
|
virtual void add( const vector<Mat>& descriptors );
|
||||||
|
@ -74,6 +74,11 @@ typedef Ptr<FeatureDetector> Ptr_FeatureDetector;
|
|||||||
typedef Ptr<DescriptorExtractor> Ptr_DescriptorExtractor;
|
typedef Ptr<DescriptorExtractor> Ptr_DescriptorExtractor;
|
||||||
typedef Ptr<DescriptorMatcher> Ptr_DescriptorMatcher;
|
typedef Ptr<DescriptorMatcher> Ptr_DescriptorMatcher;
|
||||||
|
|
||||||
|
typedef cvflann::flann_distance_t cvflann_flann_distance_t;
|
||||||
|
typedef cvflann::flann_algorithm_t cvflann_flann_algorithm_t;
|
||||||
|
typedef Ptr<flann::IndexParams> Ptr_flann_IndexParams;
|
||||||
|
typedef Ptr<flann::SearchParams> Ptr_flann_SearchParams;
|
||||||
|
|
||||||
static PyObject* failmsgp(const char *fmt, ...)
|
static PyObject* failmsgp(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char str[1000];
|
char str[1000];
|
||||||
@ -820,6 +825,14 @@ static bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
static bool pyopencv_to(PyObject *o, Ptr<T>& p, const char *name="<unknown>")
|
||||||
|
{
|
||||||
|
p = new T();
|
||||||
|
return pyopencv_to(o, *p, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool pyopencv_to(PyObject *o, cvflann::flann_distance_t& dist, const char *name="<unknown>")
|
static bool pyopencv_to(PyObject *o, cvflann::flann_distance_t& dist, const char *name="<unknown>")
|
||||||
{
|
{
|
||||||
int d = (int)dist;
|
int d = (int)dist;
|
||||||
|
@ -191,7 +191,7 @@ class CppHeaderParser(object):
|
|||||||
if add_star:
|
if add_star:
|
||||||
arg_type += "*"
|
arg_type += "*"
|
||||||
|
|
||||||
arg_type = self.batch_replace(arg_type, [("std::", ""), ("cv::", "")])
|
arg_type = self.batch_replace(arg_type, [("std::", ""), ("cv::", ""), ("::", "_")])
|
||||||
|
|
||||||
return arg_type, arg_name, modlist, argno
|
return arg_type, arg_name, modlist, argno
|
||||||
|
|
||||||
|
@ -4,35 +4,53 @@ Feature homography
|
|||||||
|
|
||||||
Example of using features2d framework for interactive video homography matching.
|
Example of using features2d framework for interactive video homography matching.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
feature_homography.py [<video source>]
|
||||||
|
|
||||||
Keys
|
Keys
|
||||||
----
|
----
|
||||||
SPACE - set reference frame
|
SPACE - set reference frame
|
||||||
ESC - exit
|
ESC - exit
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
import video
|
import video
|
||||||
from common import draw_str
|
from common import draw_str, clock
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
detector = cv2.FastFeatureDetector(16, True)
|
||||||
|
detector = cv2.GridAdaptedFeatureDetector(detector)
|
||||||
|
extractor = cv2.DescriptorExtractor_create('ORB')
|
||||||
|
|
||||||
|
FLANN_INDEX_KDTREE = 1
|
||||||
|
FLANN_INDEX_LSH = 6
|
||||||
|
flann_params= dict(algorithm = FLANN_INDEX_LSH,
|
||||||
|
table_number = 6, # 12
|
||||||
|
key_size = 12, # 20
|
||||||
|
multi_probe_level = 1) #2
|
||||||
|
matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
||||||
|
|
||||||
|
green, red = (0, 255, 0), (0, 0, 255)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
print __doc__
|
print __doc__
|
||||||
|
|
||||||
detector = cv2.FeatureDetector_create('ORB')
|
try: src = sys.argv[1]
|
||||||
extractor = cv2.DescriptorExtractor_create('ORB')
|
except: src = 0
|
||||||
matcher = cv2.DescriptorMatcher_create('BruteForce-Hamming') # 'BruteForce-Hamming' # FlannBased
|
cap = video.create_capture(src)
|
||||||
|
|
||||||
ref_desc = None
|
|
||||||
ref_kp = None
|
ref_kp = None
|
||||||
|
|
||||||
green, red = (0, 255, 0), (0, 0, 255)
|
|
||||||
|
|
||||||
cap = video.create_capture(0)
|
|
||||||
while True:
|
while True:
|
||||||
ret, img = cap.read()
|
ret, img = cap.read()
|
||||||
vis = img.copy()
|
vis = img.copy()
|
||||||
kp = detector.detect(img)
|
kp = detector.detect(img)
|
||||||
|
kp, desc = extractor.compute(img, kp)
|
||||||
|
|
||||||
for p in kp:
|
for p in kp:
|
||||||
x, y = np.int32(p.pt)
|
x, y = np.int32(p.pt)
|
||||||
@ -40,14 +58,17 @@ if __name__ == '__main__':
|
|||||||
cv2.circle(vis, (x, y), r, (0, 255, 0))
|
cv2.circle(vis, (x, y), r, (0, 255, 0))
|
||||||
draw_str(vis, (20, 20), 'feature_n: %d' % len(kp))
|
draw_str(vis, (20, 20), 'feature_n: %d' % len(kp))
|
||||||
|
|
||||||
desc = extractor.compute(img, kp)
|
if ref_kp is not None:
|
||||||
if ref_desc is not None:
|
raw_matches = matcher.knnMatch(desc, 2)
|
||||||
raw_matches = matcher.knnMatch(desc, ref_desc, 2)
|
matches = []
|
||||||
eps = 1e-5
|
for m in raw_matches:
|
||||||
matches = [(m1.trainIdx, m1.queryIdx) for m1, m2 in raw_matches if (m1.distance+eps) / (m2.distance+eps) < 0.7]
|
if len(m) == 2:
|
||||||
|
m1, m2 = m
|
||||||
|
if m1.distance < m2.distance * 0.7:
|
||||||
|
matches.append((m1.trainIdx, m1.queryIdx))
|
||||||
match_n = len(matches)
|
match_n = len(matches)
|
||||||
|
|
||||||
inliner_n = 0
|
inlier_n = 0
|
||||||
if match_n > 10:
|
if match_n > 10:
|
||||||
p0 = np.float32( [ref_kp[i].pt for i, j in matches] )
|
p0 = np.float32( [ref_kp[i].pt for i, j in matches] )
|
||||||
p1 = np.float32( [kp[j].pt for i, j in matches] )
|
p1 = np.float32( [kp[j].pt for i, j in matches] )
|
||||||
@ -66,7 +87,8 @@ if __name__ == '__main__':
|
|||||||
cv2.imshow('img', vis)
|
cv2.imshow('img', vis)
|
||||||
ch = cv2.waitKey(1)
|
ch = cv2.waitKey(1)
|
||||||
if ch == ord(' '):
|
if ch == ord(' '):
|
||||||
ref_desc = desc
|
matcher.clear()
|
||||||
|
matcher.add([desc])
|
||||||
ref_kp = kp
|
ref_kp = kp
|
||||||
ref_img = img.copy()
|
ref_img = img.copy()
|
||||||
if ch == 27:
|
if ch == 27:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user