From 2ca50933d2ee0524ba9ea68b4a6a1930ca81536e Mon Sep 17 00:00:00 2001 From: matlabbe Date: Thu, 25 Apr 2013 00:11:56 +0000 Subject: [PATCH] Added console app git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@191 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- CMakeLists.txt | 7 +- console_app/CMakeLists.txt | 25 ++++++ console_app/main.cpp | 169 +++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 console_app/CMakeLists.txt create mode 100644 console_app/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 40b898ba..8a749d79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) ####### DEPENDENCIES ####### FIND_PACKAGE(OpenCV REQUIRED) # tested on 2.3.1 -FIND_PACKAGE(Qt4 COMPONENTS QtCore QtGui) # tested on Qt4.7 +#FIND_PACKAGE(Qt4 COMPONENTS QtCore QtGui) # tested on Qt4.7 ####### OSX BUNDLE CMAKE_INSTALL_PREFIX ####### OPTION(BUILD_AS_BUNDLE "Set to ON to build as bundle (DragNDrop)" OFF) @@ -60,8 +60,9 @@ IF(APPLE AND BUILD_AS_BUNDLE) ENDIF(APPLE AND BUILD_AS_BUNDLE) ####### SOURCES (Projects) ####### -ADD_SUBDIRECTORY( app ) -ADD_SUBDIRECTORY( example ) +#ADD_SUBDIRECTORY( app ) +#ADD_SUBDIRECTORY( example ) +ADD_SUBDIRECTORY( console_app ) diff --git a/console_app/CMakeLists.txt b/console_app/CMakeLists.txt new file mode 100644 index 00000000..8d05e4bd --- /dev/null +++ b/console_app/CMakeLists.txt @@ -0,0 +1,25 @@ + + +SET(SRC_FILES + main.cpp +) + +SET(INCLUDE_DIRS + ${OpenCV_INCLUDE_DIRS} +) + +SET(LIBRARIES + ${OpenCV_LIBS} +) + +# Make sure the compiler can find include files from our library. +INCLUDE_DIRECTORIES(${INCLUDE_DIRS}) + +# Add binary called "console" that is built from the source file "main.cpp". +# The extension is automatically found. +ADD_EXECUTABLE(console ${SRC_FILES}) +TARGET_LINK_LIBRARIES(console ${LIBRARIES}) + +SET_TARGET_PROPERTIES( console + PROPERTIES OUTPUT_NAME ${PROJECT_PREFIX}-console) + diff --git a/console_app/main.cpp b/console_app/main.cpp new file mode 100644 index 00000000..c8ee8a99 --- /dev/null +++ b/console_app/main.cpp @@ -0,0 +1,169 @@ + +#include +#include + +// OpenCV stuff +#include +#include +#include +#include +#include // for homography + +void showUsage() +{ + printf( + "\n" + "Return similarity between two images.\n" + "Usage :\n" + " ./find_object-console [option] object.png scene.png\n" + "Options: \n" + " -total return total matches (default total)\n" + " -inliers return inliers percentage : inliers / (inliers + outliers)\n" + " -quiet don't show messages\n"); + + exit(-1); +} + +enum {mTotal, mInliers}; + +int main(int argc, char * argv[]) +{ + bool quiet = false; + int method = mTotal; //total matches + if(argc<3) + { + printf("Two images required!\n"); + showUsage(); + } + else if(argc>3) + { + for(int i=1; i objectKeypoints; + std::vector sceneKeypoints; + cv::Mat objectDescriptors; + cv::Mat sceneDescriptors; + + //////////////////////////// + // EXTRACT KEYPOINTS + //////////////////////////// + cv::SIFT sift; + sift.detect(objectImg, objectKeypoints); + sift.detect(sceneImg, sceneKeypoints); + + //////////////////////////// + // EXTRACT DESCRIPTORS + //////////////////////////// + sift.compute(objectImg, objectKeypoints, objectDescriptors); + sift.compute(sceneImg, sceneKeypoints, sceneDescriptors); + + //////////////////////////// + // NEAREST NEIGHBOR MATCHING USING FLANN LIBRARY (included in OpenCV) + //////////////////////////// + cv::Mat results; + cv::Mat dists; + std::vector > matches; + int k=2; // find the 2 nearest neighbors + + // Create Flann KDTree index + cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN); + results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index + dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 + + // search (nearest neighbor) + flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() ); + + //////////////////////////// + // PROCESS NEAREST NEIGHBOR RESULTS + //////////////////////////// + + // Find correspondences by NNDR (Nearest Neighbor Distance Ratio) + float nndrRatio = 0.6; + std::vector mpts_1, mpts_2; // Used for homography + std::vector indexes_1, indexes_2; // Used for homography + std::vector outlier_mask; // Used for homography + // Check if this descriptor matches with those of the objects + + for(int i=0; i(i,0) <= nndrRatio * dists.at(i,1)) + { + mpts_1.push_back(objectKeypoints.at(i).pt); + indexes_1.push_back(i); + + mpts_2.push_back(sceneKeypoints.at(results.at(i,0)).pt); + indexes_2.push_back(results.at(i,0)); + } + } + + if(method == mInliers) + { + // FIND HOMOGRAPHY + unsigned int minInliers = 8; + if(mpts_1.size() >= minInliers) + { + cv::Mat H = findHomography(mpts_1, + mpts_2, + cv::RANSAC, + 1.0, + outlier_mask); + int inliers=0, outliers=0; + for(unsigned int k=0; k