diff --git a/CMakeLists.txt b/CMakeLists.txt index 9780ab47..c68eae7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,9 +166,9 @@ IF(NOT CATKIN_BUILD) ADD_SUBDIRECTORY( src ) ADD_SUBDIRECTORY( app ) ADD_SUBDIRECTORY( tools ) - IF(NONFREE) + IF(NONFREE OR OpenCV_VERSION VERSION_GREATER "4.2.0") # SIFT is free from 4.3.0 ADD_SUBDIRECTORY( example ) - ENDIF(NONFREE) + ENDIF(NONFREE OR OpenCV_VERSION VERSION_GREATER "4.2.0") ####################### # Uninstall target, for "make uninstall" @@ -293,10 +293,18 @@ IF(NOT CATKIN_BUILD) ELSE() MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} xfeatures2d module (BRIEF/FREAK/KAZE) = NO (not found)") ENDIF() - IF(NONFREE) - MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SIFT/SURF) = YES") - ELSE() - MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SIFT/SURF) = NO") + IF(OpenCV_VERSION VERSION_GREATER "4.2.0") + IF(NONFREE) + MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SURF) = YES") + ELSE() + MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SURF) = NO") + ENDIF() + ELSEIF() + IF(NONFREE) + MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SIFT/SURF) = YES") + ELSE() + MESSAGE(STATUS " With OpenCV ${OpenCV_VERSION_MAJOR} nonfree module (SIFT/SURF) = NO") + ENDIF() ENDIF() ENDIF() diff --git a/example/main.cpp b/example/main.cpp index 0d4e9f6d..7e47b1e1 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -108,8 +108,10 @@ int main(int argc, char * argv[]) // detector = cv::Ptr(new cv::StarFeatureDetector()); // detector = cv::Ptr(new cv::SURF(600.0)); // detector = cv::Ptr(new cv::BRISK()); -#else +#elif CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) detector = cv::xfeatures2d::SIFT::create(); +#else // >= 4.3.0 + detector = cv::SIFT::create(); #endif detector->detect(objectImg, objectKeypoints); printf("Object: %d keypoints detected in %d ms\n", (int)objectKeypoints.size(), time.restart()); @@ -128,8 +130,10 @@ int main(int argc, char * argv[]) // extractor = cv::Ptr(new cv::SURF(600.0)); // extractor = cv::Ptr(new cv::BRISK()); // extractor = cv::Ptr(new cv::FREAK()); -#else +#elif CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) extractor = cv::xfeatures2d::SIFT::create(); +#else // >= 4.3.0 + extractor = cv::SIFT::create(); #endif extractor->compute(objectImg, objectKeypoints, objectDescriptors); printf("Object: %d descriptors extracted in %d ms\n", objectDescriptors.rows, time.restart()); diff --git a/src/ParametersToolBox.cpp b/src/ParametersToolBox.cpp index 3f2fb5ad..248620ce 100644 --- a/src/ParametersToolBox.cpp +++ b/src/ParametersToolBox.cpp @@ -346,7 +346,9 @@ void ParametersToolBox::addParameter(QVBoxLayout * layout, if(key.compare(Settings::kFeature2D_1Detector()) == 0) { #if FINDOBJECT_NONFREE == 0 +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) // <4.3.0 widget->setItemData(5, 0, Qt::UserRole - 1); // disable SIFT +#endif widget->setItemData(7, 0, Qt::UserRole - 1); // disable SURF #endif #if CV_MAJOR_VERSION < 3 @@ -366,7 +368,9 @@ void ParametersToolBox::addParameter(QVBoxLayout * layout, if(key.compare(Settings::kFeature2D_2Descriptor()) == 0) { #if FINDOBJECT_NONFREE == 0 +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) // <4.3.0 widget->setItemData(2, 0, Qt::UserRole - 1); // disable SIFT +#endif widget->setItemData(3, 0, Qt::UserRole - 1); // disable SURF #endif #if CV_MAJOR_VERSION < 3 diff --git a/src/Settings.cpp b/src/Settings.cpp index 95696d3b..8af30c0c 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -137,6 +137,7 @@ ParametersMap Settings::loadSettings(const QString & fileName) int index = str.split(':').first().toInt(); if(key.compare(Settings::kFeature2D_1Detector()) == 0) { +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) #if FINDOBJECT_NONFREE == 0 if(index == 5 || index == 7) { @@ -147,6 +148,16 @@ ParametersMap Settings::loadSettings(const QString & fileName) Settings::defaultFeature2D_1Detector().split(':').last().split(";").at(index).toStdString().c_str()); } #endif +#elif FINDOBJECT_NONFREE == 0 + if(index == 7) + { + index = Settings::defaultFeature2D_1Detector().split(':').first().toInt(); + UWARN("Trying to set \"%s\" to SURF but Find-Object isn't built " + "with the nonfree module from OpenCV. Keeping default combo value: %s.", + Settings::kFeature2D_1Detector().toStdString().c_str(), + Settings::defaultFeature2D_1Detector().split(':').last().split(";").at(index).toStdString().c_str()); + } +#endif #if FINDOBJECT_TORCH == 0 if(index == 12) { @@ -160,6 +171,7 @@ ParametersMap Settings::loadSettings(const QString & fileName) } else if(key.compare(Settings::kFeature2D_2Descriptor()) == 0) { +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) #if FINDOBJECT_NONFREE == 0 if(index == 2 || index == 3) { @@ -170,6 +182,16 @@ ParametersMap Settings::loadSettings(const QString & fileName) Settings::defaultFeature2D_2Descriptor().split(':').last().split(";").at(index).toStdString().c_str()); } #endif +#elif FINDOBJECT_NONFREE == 0 + if(index == 3) + { + index = Settings::defaultFeature2D_2Descriptor().split(':').first().toInt(); + UWARN("Trying to set \"%s\" to SURF but Find-Object isn't built " + "with the nonfree module from OpenCV. Keeping default combo value: %s.", + Settings::kFeature2D_2Descriptor().toStdString().c_str(), + Settings::defaultFeature2D_2Descriptor().split(':').last().split(";").at(index).toStdString().c_str()); + } +#endif #if FINDOBJECT_TORCH == 0 if(index == 11) { @@ -688,8 +710,9 @@ Feature2D * Settings::createKeypointDetector() if(index>=0 && index= 4.3.0 + else if(strategies.at(index).compare("SIFT") == 0) + { + feature2D = new Feature2D(cv::SIFT::create( + getFeature2D_SIFT_nfeatures(), + getFeature2D_SIFT_nOctaveLayers(), + getFeature2D_SIFT_contrastThreshold(), + getFeature2D_SIFT_edgeThreshold(), + getFeature2D_SIFT_sigma())); + UDEBUG("type=%s", strategies.at(index).toStdString().c_str()); + } +#if FINDOBJECT_NONFREE == 1 + else if(strategies.at(index).compare("SURF") == 0) + { + if(getFeature2D_SURF_gpu() && CVCUDA::getCudaEnabledDeviceCount()) + { + feature2D = new GPUSURF( + getFeature2D_SURF_hessianThreshold(), + getFeature2D_SURF_nOctaves(), + getFeature2D_SURF_nOctaveLayers(), + getFeature2D_SURF_extended(), + getFeature2D_SURF_keypointsRatio(), + getFeature2D_SURF_upright()); + UDEBUG("type=%s (GPU)", strategies.at(index).toStdString().c_str()); + } + else + { + feature2D = new Feature2D(cv::xfeatures2d::SURF::create( + getFeature2D_SURF_hessianThreshold(), + getFeature2D_SURF_nOctaves(), + getFeature2D_SURF_nOctaveLayers(), + getFeature2D_SURF_extended(), + getFeature2D_SURF_upright())); + UDEBUG("type=%s", strategies.at(index).toStdString().c_str()); + } + } +#endif +#endif #if FINDOBJECT_TORCH == 1 else if(strategies.at(index).compare("SuperPointTorch") == 0) { @@ -1053,8 +1125,9 @@ Feature2D * Settings::createDescriptorExtractor() if(index>=0 && index= 4.3.0 + if(strategies.at(index).compare("SURF") == 0) + { + index = Settings::defaultFeature2D_2Descriptor().split(':').first().toInt(); + UERROR("Find-Object is not built with OpenCV nonfree module so " + "SURF cannot be used! Using default \"%s\" instead.", + strategies.at(index).toStdString().c_str()); + + } +#endif #if FINDOBJECT_TORCH == 0 //check for nonfree stuff @@ -1269,6 +1352,7 @@ Feature2D * Settings::createDescriptorExtractor() UDEBUG("type=%s", strategies.at(index).toStdString().c_str()); } #endif +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION < 3) // <4.3.0 #if FINDOBJECT_NONFREE == 1 else if(strategies.at(index).compare("SIFT") == 0) { @@ -1323,6 +1407,45 @@ Feature2D * Settings::createDescriptorExtractor() } } #endif +#else // >= 4.3.0 + else if(strategies.at(index).compare("SIFT") == 0) + { + feature2D = new Feature2D(cv::SIFT::create( + getFeature2D_SIFT_nfeatures(), + getFeature2D_SIFT_nOctaveLayers(), + getFeature2D_SIFT_contrastThreshold(), + getFeature2D_SIFT_edgeThreshold(), + getFeature2D_SIFT_sigma())); + UDEBUG("type=%s", strategies.at(index).toStdString().c_str()); + } +#if FINDOBJECT_NONFREE == 1 + else if(strategies.at(index).compare("SURF") == 0) + { + if(getFeature2D_SURF_gpu() && CVCUDA::getCudaEnabledDeviceCount()) + { + feature2D = new GPUSURF( + getFeature2D_SURF_hessianThreshold(), + getFeature2D_SURF_nOctaves(), + getFeature2D_SURF_nOctaveLayers(), + getFeature2D_SURF_extended(), + getFeature2D_SURF_keypointsRatio(), + getFeature2D_SURF_upright()); + UDEBUG("type=%s (GPU)", strategies.at(index).toStdString().c_str()); + } + else + { + feature2D = new Feature2D(cv::xfeatures2d::SURF::create( + getFeature2D_SURF_hessianThreshold(), + getFeature2D_SURF_nOctaves(), + getFeature2D_SURF_nOctaveLayers(), + getFeature2D_SURF_extended(), + getFeature2D_SURF_upright())); + UDEBUG("type=%s", strategies.at(index).toStdString().c_str()); + } + } +#endif +#endif + #if FINDOBJECT_TORCH == 1 else if(strategies.at(index).compare("SuperPointTorch") == 0) { diff --git a/tools/similarity/main.cpp b/tools/similarity/main.cpp index ba63c640..ddc10e8f 100644 --- a/tools/similarity/main.cpp +++ b/tools/similarity/main.cpp @@ -121,7 +121,11 @@ int main(int argc, char * argv[]) //////////////////////////// // EXTRACT KEYPOINTS //////////////////////////// +#if CV_MAJOR_VERSION < 4 || (CV_MAJOR_VERSION == 4 and CV_MINOR_VERSION < 3) cv::Ptr sift = cv::xfeatures2d::SIFT::create(); +#else + cv::Ptr sift = cv::SIFT::create(); +#endif sift->detect(objectImg, objectKeypoints); sift->detect(sceneImg, sceneKeypoints);