2012-09-12 11:49:37 +08:00
|
|
|
#include <iostream>
|
2012-09-13 17:34:06 +08:00
|
|
|
#include <stdio.h>
|
2012-09-12 11:49:37 +08:00
|
|
|
#include "opencv2/core/core.hpp"
|
2013-06-10 22:18:01 +08:00
|
|
|
#include "opencv2/core/utility.hpp"
|
2012-09-12 11:49:37 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/ocl/ocl.hpp"
|
2013-03-18 16:45:52 +08:00
|
|
|
#include "opencv2/nonfree/ocl.hpp"
|
2012-09-12 11:49:37 +08:00
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
2013-05-31 16:06:56 +08:00
|
|
|
#include "opencv2/nonfree/nonfree.hpp"
|
2012-09-12 11:49:37 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::ocl;
|
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
const int LOOP_NUM = 10;
|
|
|
|
const int GOOD_PTS_MAX = 50;
|
|
|
|
const float GOOD_PORTION = 0.15f;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2012-09-12 11:49:37 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
int64 work_begin = 0;
|
|
|
|
int64 work_end = 0;
|
2012-09-12 11:49:37 +08:00
|
|
|
|
2013-06-10 22:18:01 +08:00
|
|
|
void workBegin()
|
2012-09-12 11:49:37 +08:00
|
|
|
{
|
2013-05-31 16:06:56 +08:00
|
|
|
work_begin = getTickCount();
|
|
|
|
}
|
|
|
|
void workEnd()
|
2012-09-12 11:49:37 +08:00
|
|
|
{
|
2013-05-31 16:06:56 +08:00
|
|
|
work_end = getTickCount() - work_begin;
|
|
|
|
}
|
2013-06-19 16:36:23 +08:00
|
|
|
double getTime()
|
|
|
|
{
|
2013-06-10 22:18:01 +08:00
|
|
|
return work_end /((double)getTickFrequency() * 1000.);
|
2013-05-31 16:06:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class KPDetector>
|
|
|
|
struct SURFDetector
|
|
|
|
{
|
|
|
|
KPDetector surf;
|
|
|
|
SURFDetector(double hessian = 800.0)
|
|
|
|
:surf(hessian)
|
2012-09-12 11:49:37 +08:00
|
|
|
{
|
|
|
|
}
|
2013-05-31 16:06:56 +08:00
|
|
|
template<class T>
|
2013-06-10 22:18:01 +08:00
|
|
|
void operator()(const T& in, const T& mask, std::vector<cv::KeyPoint>& pts, T& descriptors, bool useProvided = false)
|
2012-09-12 11:49:37 +08:00
|
|
|
{
|
2013-05-31 16:06:56 +08:00
|
|
|
surf(in, mask, pts, descriptors, useProvided);
|
2012-09-12 11:49:37 +08:00
|
|
|
}
|
2013-05-31 16:06:56 +08:00
|
|
|
};
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
template<class KPMatcher>
|
|
|
|
struct SURFMatcher
|
|
|
|
{
|
|
|
|
KPMatcher matcher;
|
|
|
|
template<class T>
|
2013-06-10 22:18:01 +08:00
|
|
|
void match(const T& in1, const T& in2, std::vector<cv::DMatch>& matches)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-05-31 16:06:56 +08:00
|
|
|
matcher.match(in1, in2, matches);
|
2012-09-12 11:49:37 +08:00
|
|
|
}
|
2013-05-31 16:06:56 +08:00
|
|
|
};
|
2012-09-12 11:49:37 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
Mat drawGoodMatches(
|
|
|
|
const Mat& cpu_img1,
|
|
|
|
const Mat& cpu_img2,
|
2013-06-10 22:18:01 +08:00
|
|
|
const std::vector<KeyPoint>& keypoints1,
|
|
|
|
const std::vector<KeyPoint>& keypoints2,
|
|
|
|
std::vector<DMatch>& matches,
|
|
|
|
std::vector<Point2f>& scene_corners_
|
2013-05-31 16:06:56 +08:00
|
|
|
)
|
|
|
|
{
|
2013-06-10 22:18:01 +08:00
|
|
|
//-- Sort matches and preserve top 10% matches
|
2013-05-31 16:06:56 +08:00
|
|
|
std::sort(matches.begin(), matches.end());
|
2012-09-12 11:49:37 +08:00
|
|
|
std::vector< DMatch > good_matches;
|
2013-05-31 16:06:56 +08:00
|
|
|
double minDist = matches.front().distance,
|
2013-06-19 16:36:23 +08:00
|
|
|
maxDist = matches.back().distance;
|
2012-09-12 11:49:37 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
const int ptsPairs = std::min(GOOD_PTS_MAX, (int)(matches.size() * GOOD_PORTION));
|
|
|
|
for( int i = 0; i < ptsPairs; i++ )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-05-31 16:06:56 +08:00
|
|
|
good_matches.push_back( matches[i] );
|
2012-09-12 11:49:37 +08:00
|
|
|
}
|
2013-05-31 16:06:56 +08:00
|
|
|
std::cout << "\nMax distance: " << maxDist << std::endl;
|
|
|
|
std::cout << "Min distance: " << minDist << std::endl;
|
|
|
|
|
|
|
|
std::cout << "Calculating homography using " << ptsPairs << " point pairs." << std::endl;
|
2012-09-12 11:49:37 +08:00
|
|
|
|
|
|
|
// drawing the results
|
|
|
|
Mat img_matches;
|
|
|
|
drawMatches( cpu_img1, keypoints1, cpu_img2, keypoints2,
|
2013-06-19 16:36:23 +08:00
|
|
|
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
|
2013-06-25 18:08:23 +08:00
|
|
|
std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
|
2012-09-12 11:49:37 +08:00
|
|
|
|
|
|
|
//-- Localize the object
|
|
|
|
std::vector<Point2f> obj;
|
|
|
|
std::vector<Point2f> scene;
|
|
|
|
|
2012-09-13 17:34:06 +08:00
|
|
|
for( size_t i = 0; i < good_matches.size(); i++ )
|
2012-09-12 11:49:37 +08:00
|
|
|
{
|
|
|
|
//-- Get the keypoints from the good matches
|
|
|
|
obj.push_back( keypoints1[ good_matches[i].queryIdx ].pt );
|
|
|
|
scene.push_back( keypoints2[ good_matches[i].trainIdx ].pt );
|
|
|
|
}
|
|
|
|
//-- Get the corners from the image_1 ( the object to be "detected" )
|
|
|
|
std::vector<Point2f> obj_corners(4);
|
2013-06-25 18:08:23 +08:00
|
|
|
obj_corners[0] = Point(0,0);
|
|
|
|
obj_corners[1] = Point( cpu_img1.cols, 0 );
|
|
|
|
obj_corners[2] = Point( cpu_img1.cols, cpu_img1.rows );
|
|
|
|
obj_corners[3] = Point( 0, cpu_img1.rows );
|
2012-09-12 11:49:37 +08:00
|
|
|
std::vector<Point2f> scene_corners(4);
|
|
|
|
|
2013-06-10 22:18:01 +08:00
|
|
|
Mat H = findHomography( obj, scene, RANSAC );
|
2012-09-12 11:49:37 +08:00
|
|
|
perspectiveTransform( obj_corners, scene_corners, H);
|
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
scene_corners_ = scene_corners;
|
2013-06-10 22:18:01 +08:00
|
|
|
|
2012-09-12 11:49:37 +08:00
|
|
|
//-- Draw lines between the corners (the mapped object in the scene - image_2 )
|
2013-06-10 22:18:01 +08:00
|
|
|
line( img_matches,
|
2013-06-19 16:36:23 +08:00
|
|
|
scene_corners[0] + Point2f( (float)cpu_img1.cols, 0), scene_corners[1] + Point2f( (float)cpu_img1.cols, 0),
|
2013-06-25 18:08:23 +08:00
|
|
|
Scalar( 0, 255, 0), 2, LINE_AA );
|
2013-06-10 22:18:01 +08:00
|
|
|
line( img_matches,
|
2013-06-19 16:36:23 +08:00
|
|
|
scene_corners[1] + Point2f( (float)cpu_img1.cols, 0), scene_corners[2] + Point2f( (float)cpu_img1.cols, 0),
|
2013-06-25 18:08:23 +08:00
|
|
|
Scalar( 0, 255, 0), 2, LINE_AA );
|
2013-06-10 22:18:01 +08:00
|
|
|
line( img_matches,
|
2013-06-19 16:36:23 +08:00
|
|
|
scene_corners[2] + Point2f( (float)cpu_img1.cols, 0), scene_corners[3] + Point2f( (float)cpu_img1.cols, 0),
|
2013-06-25 18:08:23 +08:00
|
|
|
Scalar( 0, 255, 0), 2, LINE_AA );
|
2013-06-10 22:18:01 +08:00
|
|
|
line( img_matches,
|
2013-06-19 16:36:23 +08:00
|
|
|
scene_corners[3] + Point2f( (float)cpu_img1.cols, 0), scene_corners[0] + Point2f( (float)cpu_img1.cols, 0),
|
2013-06-25 18:08:23 +08:00
|
|
|
Scalar( 0, 255, 0), 2, LINE_AA );
|
2013-05-31 16:06:56 +08:00
|
|
|
return img_matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// This program demonstrates the usage of SURF_OCL.
|
|
|
|
// use cpu findHomography interface to calculate the transformation matrix
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2013-06-19 16:36:23 +08:00
|
|
|
const char* keys =
|
2013-06-25 18:08:23 +08:00
|
|
|
"{ help h | false | print help message }"
|
|
|
|
"{ left l | | specify left image }"
|
|
|
|
"{ right r | | specify right image }"
|
|
|
|
"{ output o | SURF_output.jpg | specify output save path (only works in CPU or GPU only mode) }"
|
|
|
|
"{ use_cpu c | false | use CPU algorithms }"
|
|
|
|
"{ use_all a | false | use both CPU and GPU algorithms}";
|
2013-06-19 16:36:23 +08:00
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
|
|
|
if (cmd.get<bool>("help"))
|
|
|
|
{
|
|
|
|
std::cout << "Avaible options:" << std::endl;
|
2013-06-25 18:08:23 +08:00
|
|
|
cmd.printMessage();
|
2013-06-19 16:36:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-10 22:18:01 +08:00
|
|
|
std::vector<cv::ocl::Info> info;
|
2013-05-31 16:06:56 +08:00
|
|
|
if(cv::ocl::getDevice(info) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "Error: Did not find a valid OpenCL device!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ocl::setDevice(info[0]);
|
|
|
|
|
|
|
|
Mat cpu_img1, cpu_img2, cpu_img1_grey, cpu_img2_grey;
|
|
|
|
oclMat img1, img2;
|
2013-06-19 16:36:23 +08:00
|
|
|
bool useCPU = cmd.get<bool>("c");
|
2013-05-31 16:06:56 +08:00
|
|
|
bool useGPU = false;
|
2013-06-19 16:36:23 +08:00
|
|
|
bool useALL = cmd.get<bool>("a");
|
|
|
|
|
2013-06-25 18:08:23 +08:00
|
|
|
std::string outpath = cmd.get<std::string>("o");
|
2013-05-31 16:06:56 +08:00
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
cpu_img1 = imread(cmd.get<std::string>("l"));
|
|
|
|
CV_Assert(!cpu_img1.empty());
|
2013-06-25 18:08:23 +08:00
|
|
|
cvtColor(cpu_img1, cpu_img1_grey, COLOR_BGR2GRAY);
|
2013-06-19 16:36:23 +08:00
|
|
|
img1 = cpu_img1_grey;
|
2013-05-31 16:06:56 +08:00
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
cpu_img2 = imread(cmd.get<std::string>("r"));
|
|
|
|
CV_Assert(!cpu_img2.empty());
|
2013-06-25 18:08:23 +08:00
|
|
|
cvtColor(cpu_img2, cpu_img2_grey, COLOR_BGR2GRAY);
|
2013-06-19 16:36:23 +08:00
|
|
|
img2 = cpu_img2_grey;
|
|
|
|
|
|
|
|
if(useALL)
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
2013-06-19 16:36:23 +08:00
|
|
|
useCPU = false;
|
|
|
|
useGPU = false;
|
2013-05-31 16:06:56 +08:00
|
|
|
}
|
2013-06-19 16:36:23 +08:00
|
|
|
else if(useCPU==false && useALL==false)
|
|
|
|
{
|
|
|
|
useGPU = true;
|
2013-05-31 16:06:56 +08:00
|
|
|
}
|
2013-06-19 16:36:23 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
if(!useCPU)
|
|
|
|
{
|
|
|
|
std::cout
|
2013-06-19 16:36:23 +08:00
|
|
|
<< "Device name:"
|
|
|
|
<< info[0].DeviceName[0]
|
|
|
|
<< std::endl;
|
2013-05-31 16:06:56 +08:00
|
|
|
}
|
|
|
|
double surf_time = 0.;
|
|
|
|
|
|
|
|
//declare input/output
|
2013-06-10 22:18:01 +08:00
|
|
|
std::vector<KeyPoint> keypoints1, keypoints2;
|
|
|
|
std::vector<DMatch> matches;
|
2013-05-31 16:06:56 +08:00
|
|
|
|
2013-06-10 22:18:01 +08:00
|
|
|
std::vector<KeyPoint> gpu_keypoints1;
|
|
|
|
std::vector<KeyPoint> gpu_keypoints2;
|
|
|
|
std::vector<DMatch> gpu_matches;
|
2013-05-31 16:06:56 +08:00
|
|
|
|
|
|
|
Mat descriptors1CPU, descriptors2CPU;
|
|
|
|
|
|
|
|
oclMat keypoints1GPU, keypoints2GPU;
|
|
|
|
oclMat descriptors1GPU, descriptors2GPU;
|
|
|
|
|
|
|
|
//instantiate detectors/matchers
|
|
|
|
SURFDetector<SURF> cpp_surf;
|
|
|
|
SURFDetector<SURF_OCL> ocl_surf;
|
2013-06-10 22:18:01 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
SURFMatcher<BFMatcher> cpp_matcher;
|
|
|
|
SURFMatcher<BFMatcher_OCL> ocl_matcher;
|
|
|
|
|
|
|
|
//-- start of timing section
|
2013-06-10 22:18:01 +08:00
|
|
|
if (useCPU)
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i <= LOOP_NUM; i++)
|
|
|
|
{
|
|
|
|
if(i == 1) workBegin();
|
|
|
|
cpp_surf(cpu_img1_grey, Mat(), keypoints1, descriptors1CPU);
|
|
|
|
cpp_surf(cpu_img2_grey, Mat(), keypoints2, descriptors2CPU);
|
|
|
|
cpp_matcher.match(descriptors1CPU, descriptors2CPU, matches);
|
|
|
|
}
|
|
|
|
workEnd();
|
|
|
|
std::cout << "CPP: FOUND " << keypoints1.size() << " keypoints on first image" << std::endl;
|
|
|
|
std::cout << "CPP: FOUND " << keypoints2.size() << " keypoints on second image" << std::endl;
|
|
|
|
|
|
|
|
surf_time = getTime();
|
|
|
|
std::cout << "SURF run time: " << surf_time / LOOP_NUM << " ms" << std::endl<<"\n";
|
|
|
|
}
|
|
|
|
else if(useGPU)
|
|
|
|
{
|
|
|
|
for (int i = 0; i <= LOOP_NUM; i++)
|
|
|
|
{
|
|
|
|
if(i == 1) workBegin();
|
|
|
|
ocl_surf(img1, oclMat(), keypoints1, descriptors1GPU);
|
|
|
|
ocl_surf(img2, oclMat(), keypoints2, descriptors2GPU);
|
|
|
|
ocl_matcher.match(descriptors1GPU, descriptors2GPU, matches);
|
|
|
|
}
|
|
|
|
workEnd();
|
|
|
|
std::cout << "OCL: FOUND " << keypoints1.size() << " keypoints on first image" << std::endl;
|
|
|
|
std::cout << "OCL: FOUND " << keypoints2.size() << " keypoints on second image" << std::endl;
|
|
|
|
|
|
|
|
surf_time = getTime();
|
|
|
|
std::cout << "SURF run time: " << surf_time / LOOP_NUM << " ms" << std::endl<<"\n";
|
2013-06-19 16:36:23 +08:00
|
|
|
}
|
|
|
|
else
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
|
|
|
//cpu runs
|
|
|
|
for (int i = 0; i <= LOOP_NUM; i++)
|
|
|
|
{
|
|
|
|
if(i == 1) workBegin();
|
|
|
|
cpp_surf(cpu_img1_grey, Mat(), keypoints1, descriptors1CPU);
|
|
|
|
cpp_surf(cpu_img2_grey, Mat(), keypoints2, descriptors2CPU);
|
|
|
|
cpp_matcher.match(descriptors1CPU, descriptors2CPU, matches);
|
|
|
|
}
|
|
|
|
workEnd();
|
|
|
|
std::cout << "\nCPP: FOUND " << keypoints1.size() << " keypoints on first image" << std::endl;
|
|
|
|
std::cout << "CPP: FOUND " << keypoints2.size() << " keypoints on second image" << std::endl;
|
|
|
|
|
|
|
|
surf_time = getTime();
|
|
|
|
std::cout << "(CPP)SURF run time: " << surf_time / LOOP_NUM << " ms" << std::endl;
|
|
|
|
|
|
|
|
//gpu runs
|
|
|
|
for (int i = 0; i <= LOOP_NUM; i++)
|
|
|
|
{
|
|
|
|
if(i == 1) workBegin();
|
|
|
|
ocl_surf(img1, oclMat(), gpu_keypoints1, descriptors1GPU);
|
|
|
|
ocl_surf(img2, oclMat(), gpu_keypoints2, descriptors2GPU);
|
|
|
|
ocl_matcher.match(descriptors1GPU, descriptors2GPU, gpu_matches);
|
|
|
|
}
|
|
|
|
workEnd();
|
|
|
|
std::cout << "\nOCL: FOUND " << keypoints1.size() << " keypoints on first image" << std::endl;
|
|
|
|
std::cout << "OCL: FOUND " << keypoints2.size() << " keypoints on second image" << std::endl;
|
|
|
|
|
|
|
|
surf_time = getTime();
|
|
|
|
std::cout << "(OCL)SURF run time: " << surf_time / LOOP_NUM << " ms" << std::endl<<"\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
std::vector<Point2f> cpu_corner;
|
|
|
|
Mat img_matches = drawGoodMatches(cpu_img1, cpu_img2, keypoints1, keypoints2, matches, cpu_corner);
|
|
|
|
|
|
|
|
std::vector<Point2f> gpu_corner;
|
|
|
|
Mat ocl_img_matches;
|
|
|
|
if(useALL || (!useCPU&&!useGPU))
|
|
|
|
{
|
|
|
|
ocl_img_matches = drawGoodMatches(cpu_img1, cpu_img2, gpu_keypoints1, gpu_keypoints2, gpu_matches, gpu_corner);
|
|
|
|
|
|
|
|
//check accuracy
|
|
|
|
std::cout<<"\nCheck accuracy:\n";
|
|
|
|
|
|
|
|
if(cpu_corner.size()!=gpu_corner.size())
|
|
|
|
std::cout<<"Failed\n";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool result = false;
|
2013-05-31 17:29:55 +08:00
|
|
|
for(size_t i = 0; i < cpu_corner.size(); i++)
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
|
|
|
if((std::abs(cpu_corner[i].x - gpu_corner[i].x) > 10)
|
2013-06-19 16:36:23 +08:00
|
|
|
||(std::abs(cpu_corner[i].y - gpu_corner[i].y) > 10))
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
|
|
|
std::cout<<"Failed\n";
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = true;
|
2013-06-10 22:18:01 +08:00
|
|
|
}
|
2013-05-31 16:06:56 +08:00
|
|
|
if(result)
|
|
|
|
std::cout<<"Passed\n";
|
|
|
|
}
|
|
|
|
}
|
2012-09-12 11:49:37 +08:00
|
|
|
|
|
|
|
//-- Show detected matches
|
2013-05-31 16:06:56 +08:00
|
|
|
if (useCPU)
|
|
|
|
{
|
|
|
|
namedWindow("cpu surf matches", 0);
|
|
|
|
imshow("cpu surf matches", img_matches);
|
2013-06-19 16:36:23 +08:00
|
|
|
imwrite(outpath, img_matches);
|
2013-05-31 16:06:56 +08:00
|
|
|
}
|
|
|
|
else if(useGPU)
|
|
|
|
{
|
|
|
|
namedWindow("ocl surf matches", 0);
|
|
|
|
imshow("ocl surf matches", img_matches);
|
2013-06-19 16:36:23 +08:00
|
|
|
imwrite(outpath, img_matches);
|
|
|
|
}
|
|
|
|
else
|
2013-05-31 16:06:56 +08:00
|
|
|
{
|
|
|
|
namedWindow("cpu surf matches", 0);
|
|
|
|
imshow("cpu surf matches", img_matches);
|
2012-09-12 11:49:37 +08:00
|
|
|
|
2013-05-31 16:06:56 +08:00
|
|
|
namedWindow("ocl surf matches", 0);
|
|
|
|
imshow("ocl surf matches", ocl_img_matches);
|
|
|
|
}
|
|
|
|
waitKey(0);
|
2012-09-12 11:49:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|