Commit 400eb371 authored by Maria Dimashova's avatar Maria Dimashova

added parameter keypointIndexes to KeyPoint::convert, updated sample

parent c4fded9a
...@@ -221,9 +221,10 @@ public: ...@@ -221,9 +221,10 @@ public:
: pt(x, y), size(_size), angle(_angle), : pt(x, y), size(_size), angle(_angle),
response(_response), octave(_octave), class_id(_class_id) {} response(_response), octave(_octave), class_id(_class_id) {}
//! converts vector of keypoints to vector of points //! converts vector of keypoints to vector of points
static void convert(const std::vector<KeyPoint>& u, std::vector<Point2f>& v); static void convert(const std::vector<KeyPoint>& keypoints, std::vector<Point2f>& points2f,
const std::vector<int>& keypointIndexes=std::vector<int>());
//! converts vector of points to the vector of keypoints, where each keypoint is assigned the same size and the same orientation //! converts vector of points to the vector of keypoints, where each keypoint is assigned the same size and the same orientation
static void convert(const std::vector<Point2f>& u, std::vector<KeyPoint>& v, static void convert(const std::vector<Point2f>& points2f, std::vector<KeyPoint>& keypoints,
float size=1, float response=1, int octave=0, int class_id=-1); float size=1, float response=1, int octave=0, int class_id=-1);
Point2f pt; //!< coordinates of the keypoints Point2f pt; //!< coordinates of the keypoints
float size; //!< diameter of the meaningfull keypoint neighborhood float size; //!< diameter of the meaningfull keypoint neighborhood
......
...@@ -76,23 +76,37 @@ void read(const FileNode& node, vector<KeyPoint>& keypoints) ...@@ -76,23 +76,37 @@ void read(const FileNode& node, vector<KeyPoint>& keypoints)
} }
void KeyPoint::convert(const std::vector<KeyPoint>& u, std::vector<Point2f>& v) void KeyPoint::convert(const std::vector<KeyPoint>& keypoints, std::vector<Point2f>& points2f,
const vector<int>& keypointIndexes)
{ {
size_t i, sz = u.size(); if( keypointIndexes.empty() )
v.resize(sz); {
points2f.resize( keypoints.size() );
for( i = 0; i < sz; i++ ) for( size_t i = 0; i < keypoints.size(); i++ )
v[i] = u[i].pt; points2f[i] = keypoints[i].pt;
}
else
{
points2f.resize( keypointIndexes.size() );
for( size_t i = 0; i < keypointIndexes.size(); i++ )
{
int idx = keypointIndexes[i];
if( idx >= 0 )
points2f[i] = keypoints[idx].pt;
else
{
CV_Error( CV_StsBadArg, "keypointIndexes has element < 0. TODO: process this case" );
//points2f[i] = Point2f(-1, -1);
}
}
}
} }
void KeyPoint::convert( const std::vector<Point2f>& u, std::vector<KeyPoint>& v, void KeyPoint::convert( const std::vector<Point2f>& points2f, std::vector<KeyPoint>& keypoints,
float size, float response, int octave, int class_id ) float size, float response, int octave, int class_id )
{ {
size_t i, sz = u.size(); for( size_t i = 0; i < points2f.size(); i++ )
v.resize(sz); keypoints[i] = KeyPoint(points2f[i], size, -1, response, octave, class_id);
for( i = 0; i < sz; i++ )
v[i] = KeyPoint(u[i], size, -1, response, octave, class_id);
} }
} }
...@@ -5,21 +5,9 @@ ...@@ -5,21 +5,9 @@
#include "opencv2/features2d/features2d.hpp" #include "opencv2/features2d/features2d.hpp"
#include <iostream> #include <iostream>
using namespace cv; using namespace cv;
using namespace std; using namespace std;
inline Point2f applyHomography( const Mat_<double>& H, const Point2f& pt )
{
double z = H(2,0)*pt.x + H(2,1)*pt.y + H(2,2);
if( z )
{
double w = 1./z;
return Point2f( (H(0,0)*pt.x + H(0,1)*pt.y + H(0,2))*w, (H(1,0)*pt.x + H(1,1)*pt.y + H(1,2))*w );
}
return Point2f( numeric_limits<double>::max(), numeric_limits<double>::max() );
}
void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG* rng ) void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG* rng )
{ {
H.create(3, 3, CV_32FC1); H.create(3, 3, CV_32FC1);
...@@ -74,40 +62,36 @@ void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective, ...@@ -74,40 +62,36 @@ void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective,
if( !isWarpPerspective && ransacReprojThreshold >= 0 ) if( !isWarpPerspective && ransacReprojThreshold >= 0 )
{ {
cout << "< Computing homography (RANSAC)..." << endl; cout << "< Computing homography (RANSAC)..." << endl;
vector<Point2f> points1(matches.size()), points2(matches.size()); vector<Point2f> points1; KeyPoint::convert(keypoints1, points1);
for( size_t i = 0; i < matches.size(); i++ ) vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, matches);
{
points1[i] = keypoints1[i].pt;
points2[i] = keypoints2[matches[i]].pt;
}
H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold ); H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold );
cout << ">" << endl; cout << ">" << endl;
} }
Mat drawImg; Mat drawImg;
if( !H12.empty() ) if( !H12.empty() ) // filter outliers
{ {
vector<char> matchesMask( matches.size(), 0 ); vector<char> matchesMask( matches.size(), 0 );
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1);
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, matches);
Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);
vector<int>::const_iterator mit = matches.begin(); vector<int>::const_iterator mit = matches.begin();
for( size_t i1 = 0; mit != matches.end(); ++mit, i1++ ) for( size_t i1 = 0; i1 < points1.size(); i1++ )
{ {
Point2f pt1 = keypoints1[i1].pt, if( norm(points2[i1] - points1t.at<Point2f>(i1,0)) < 4 ) // inlier
pt2 = keypoints2[*mit].pt;
if( norm(pt2 - applyHomography(H12, pt1)) < 4 ) // inlier
matchesMask[i1] = 1; matchesMask[i1] = 1;
} }
// draw inliers // draw inliers
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask ); drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask );
// draw outliers #if 0 // draw outliers
/*for( size_t i1 = 0; i1 < matchesMask.size(); i1++ ) for( size_t i1 = 0; i1 < matchesMask.size(); i1++ )
matchesMask[i1] = !matchesMask[i1]; matchesMask[i1] = !matchesMask[i1];
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask, drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask,
DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );*/ DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS )
#endif
} }
else else
{ drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg );
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 255, 0) );
}
imshow( winName, drawImg ); imshow( winName, drawImg );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment