Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV 3 compatible demo #4

Open
wants to merge 1 commit into
base: opencv3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 82 additions & 81 deletions src/demo.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
/*
BRISK - Binary Robust Invariant Scalable Keypoints
Reference implementation of
[1] Stefan Leutenegger,Margarita Chli and Roland Siegwart, BRISK:
Binary Robust Invariant Scalable Keypoints, in Proceedings of
the IEEE International Conference on Computer Vision (ICCV2011).

Copyright (C) 2011 The Autonomous Systems Lab (ASL), ETH Zurich,
Stefan Leutenegger, Simon Lynen and Margarita Chli.

This file is part of BRISK.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ASL nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
BRISK - Binary Robust Invariant Scalable Keypoints
Reference implementation of
[1] Stefan Leutenegger,Margarita Chli and Roland Siegwart, BRISK:
Binary Robust Invariant Scalable Keypoints, in Proceedings of
the IEEE International Conference on Computer Vision (ICCV2011).
Copyright (C) 2011 The Autonomous Systems Lab (ASL), ETH Zurich,
Stefan Leutenegger, Simon Lynen and Margarita Chli.
This file is part of BRISK.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ASL nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include "../include/brisk/brisk.h"
#include <fstream>
#include <iostream>
Expand All @@ -45,32 +46,32 @@ const float r=2.5; // found 8-9-11, r=3.6, exponent 1.5

void help(char** argv){
std::cout << "This command line tool lets you evaluate different keypoint "
<< "detectors, descriptors and matchers." << std::endl
<< "usage:" << std::endl
<< argv[0] << " <dataset> <2nd> <detector> <descriptor> [descFile1 descFile1]" << std::endl
<< " " << "dataset: Folder containing the images. The images must be of .ppm "<< std::endl
<< " " << " format. They must be named img#.ppm, and there must be "<< std::endl
<< " " << " corresponding homographies named H1to#." << std::endl
<< " " << " You can also use the prefix rot-, then 2nd will be the" << std::endl
<< " " << " rotation in degrees." << std::endl
<< " " << "2nd: Number of the 2nd image (the 1st one is always img1.ppm)"<< std::endl
<< " " << " or the rotation in degrees, if rot- used." << std::endl
<< " " << "detector: Feature detector, e.g. AGAST, or BRISK. You can add the "<< std::endl
<< " " << " threshold, e.g. BRISK80 or SURF2000"<< std::endl
<< " " << "descriptor: Feature descriptor, e.g. SURF, BRIEF, BRISK or U-BRISK."<< std::endl
<< " " << "[descFile]: Optional: files with descriptors to act as detected points."<< std::endl;
<< "detectors, descriptors and matchers." << std::endl
<< "usage:" << std::endl
<< argv[0] << " <dataset> <2nd> <detector> <descriptor> [descFile1 descFile1]" << std::endl
<< " " << "dataset: Folder containing the images. The images must be of .ppm "<< std::endl
<< " " << " format. They must be named img#.ppm, and there must be "<< std::endl
<< " " << " corresponding homographies named H1to#." << std::endl
<< " " << " You can also use the prefix rot-, then 2nd will be the" << std::endl
<< " " << " rotation in degrees." << std::endl
<< " " << "2nd: Number of the 2nd image (the 1st one is always img1.ppm)"<< std::endl
<< " " << " or the rotation in degrees, if rot- used." << std::endl
<< " " << "detector: Feature detector, e.g. AGAST, or BRISK. You can add the "<< std::endl
<< " " << " threshold, e.g. BRISK80 or SURF2000"<< std::endl
<< " " << "descriptor: Feature descriptor, e.g. SURF, BRIEF, BRISK or U-BRISK."<< std::endl
<< " " << "[descFile]: Optional: files with descriptors to act as detected points."<< std::endl;
}

int main(int argc, char ** argv) {

//std::cout<<sizeof(cv::Point2i)<<" "<<sizeof(CvPoint)<<std::endl;

// process command line args
if(argc != 5 && argc != 7 && argc != 1){
help(argv);
return 1;
}

// names of the two image files
std::string fname1;
std::string fname2;
Expand All @@ -92,7 +93,7 @@ int main(int argc, char ** argv) {
fextensions.push_back(".ras");
fextensions.push_back(".tiff");
fextensions.push_back(".tif");

// if no arguments are passed:
if(argc==1){
int i=0;
Expand Down Expand Up @@ -156,19 +157,19 @@ int main(int argc, char ** argv) {
}
}
}

// convert to grayscale
cv::Mat imgGray1;
cv::cvtColor(imgRGB1, imgGray1, CV_BGR2GRAY);
cv::Mat imgGray2;
if(!do_rot){
cv::cvtColor(imgRGB2, imgGray2, CV_BGR2GRAY);
}

// run FAST in first image
std::vector<cv::KeyPoint> keypoints, keypoints2;
int threshold;

// create the detector:
cv::Ptr<cv::FeatureDetector> detector;
if(argc==1){
Expand All @@ -179,7 +180,7 @@ int main(int argc, char ** argv) {
threshold = atoi(argv[3]+4);
if(threshold==0)
threshold = 30;
detector = new cv::FastFeatureDetector(threshold,true);
detector = cv::FastFeatureDetector::create(threshold,true);
}
else if(strncmp("AGAST", argv[3], 5 )==0){
threshold = atoi(argv[3]+5);
Expand All @@ -196,25 +197,25 @@ int main(int argc, char ** argv) {
/*else if(strncmp("SURF", argv[3], 4 )==0){
threshold = atoi(argv[3]+4);
if(threshold==0)
threshold = 400;
threshold = 400;
detector = new cv::SurfFeatureDetector(threshold);
}
else if(strncmp("SIFT", argv[3], 4 )==0){
}
else if(strncmp("SIFT", argv[3], 4 )==0){
float thresh = 0.04 / cv::SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS / 2.0;
float edgeThreshold=atof(argv[3]+4);
if(edgeThreshold==0)
thresh = 10.0;
thresh = 10.0;
detector = new cv::SiftFeatureDetector(thresh,edgeThreshold);
}*/
else{
}
else{
detector = cv::FeatureDetector::create( argv[3] );
}
}*/
if (detector.empty()){
std::cout << "Detector " << argv[3] << " not recognized. Check spelling!" << std::endl;
return 3;
}
}

// run the detector:
if(argc == 7){
// try to read descriptor files
Expand All @@ -230,7 +231,7 @@ int main(int argc, char ** argv) {
std::cout<<"Descriptor file not found at " << desc2<<std::endl;
return 3;
}

// fill the keypoints
std::string str1;
std::stringstream strstrm1;
Expand Down Expand Up @@ -260,17 +261,17 @@ int main(int argc, char ** argv) {
float r=sqrt(1.0/a);
keypoints2.push_back(cv::KeyPoint(x, y, 4.0*r));
}

// clean up
descf1.close();
descf2.close();
}
else{

detector->detect(imgGray1,keypoints);
detector->detect(imgGray2,keypoints2);
}

// now the extractor:
bool hamming=true;
cv::Ptr<cv::DescriptorExtractor> descriptorExtractor;
Expand All @@ -292,59 +293,59 @@ int main(int argc, char ** argv) {
descriptorExtractor = new brisk::BriskDescriptorExtractor(true,false);
}
else if(std::string(argv[4])=="BRIEF"){
descriptorExtractor = new cv::BriefDescriptorExtractor(64);
descriptorExtractor = cv::xfeatures2d::BriefDescriptorExtractor::create(64);
}
/*else if(std::string(argv[4])=="CALONDER"){
descriptorExtractor = new cv::CalonderDescriptorExtractor<float>("current.rtc");
hamming=false;
}
else if(std::string(argv[4])=="SURF"){
}
else if(std::string(argv[4])=="SURF"){
descriptorExtractor = new cv::SurfDescriptorExtractor();
hamming=false;
}
else if(std::string(argv[4])=="SIFT"){
}
else if(std::string(argv[4])=="SIFT"){
descriptorExtractor = new cv::SiftDescriptorExtractor();
hamming=false;
}*/
else{
}
else{
descriptorExtractor = cv::DescriptorExtractor::create( argv[4] );
}
}*/
if (descriptorExtractor.empty()){
hamming=false;
std::cout << "Descriptor " << argv[4] << " not recognized. Check spelling!" << std::endl;
return 4;
}
}

// get the descriptors
cv::Mat descriptors, descriptors2;
std::vector<cv::DMatch> indices;
// first image
descriptorExtractor->compute(imgGray2,keypoints2,descriptors2);
// and the second one
descriptorExtractor->compute(imgGray1,keypoints,descriptors);

// matching
std::vector<std::vector<cv::DMatch> > matches;
cv::Ptr<cv::DescriptorMatcher> descriptorMatcher;
if(hamming)
//descriptorMatcher = new cv::BruteForceMatcher<cv::HammingSse>();
descriptorMatcher = new cv::BFMatcher(cv::NORM_HAMMING);
descriptorMatcher = new cv::BFMatcher(cv::NORM_HAMMING);
else
descriptorMatcher = new cv::BFMatcher();
if(hamming)
descriptorMatcher->radiusMatch(descriptors2,descriptors,matches,100.0);
else
descriptorMatcher->radiusMatch(descriptors2,descriptors,matches,0.21);
cv::Mat outimg;

// drawing
drawMatches(imgRGB2, keypoints2, imgRGB1, keypoints,matches,outimg,
cv::Scalar(0,255,0), cv::Scalar(0,0,255),
std::vector<std::vector<char> >(), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
std::vector<std::vector<char> >(), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
cv::namedWindow("Matches");
cv::imshow("Matches", outimg);
cv::waitKey();

return 0;
}