Skip to content
Snippets Groups Projects
Commit 435bc2e6 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Add the Fixstars/libSGM algorithm

parent 8c9f1767
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ include(CTest) ...@@ -8,6 +8,7 @@ include(CTest)
enable_testing() enable_testing()
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
if (WIN32) if (WIN32)
find_package( glog REQUIRED ) find_package( glog REQUIRED )
...@@ -15,6 +16,8 @@ include_directories(${glog_DIR}) ...@@ -15,6 +16,8 @@ include_directories(${glog_DIR})
endif (WIN32) endif (WIN32)
find_package( OpenCV REQUIRED ) find_package( OpenCV REQUIRED )
find_package( Threads REQUIRED ) find_package( Threads REQUIRED )
find_package( LibSGM )
find_package( CUDA )
#find_package(PkgConfig) #find_package(PkgConfig)
#pkg_check_modules(GTKMM gtkmm-3.0) #pkg_check_modules(GTKMM gtkmm-3.0)
...@@ -63,10 +66,16 @@ set(CVNODESRC ...@@ -63,10 +66,16 @@ set(CVNODESRC
src/disparity.cpp src/disparity.cpp
src/algorithms/rtcensus.cpp src/algorithms/rtcensus.cpp
src/algorithms/opencv_sgbm.cpp src/algorithms/opencv_sgbm.cpp
src/algorithms/opencv_bm.cpp
) )
if (LIBSGM_FOUND)
list(APPEND CVNODESRC "src/algorithms/fixstars_sgm.cpp")
message("Cuda libraries at ${CUDA_LIBRARIES}")
endif (LIBSGM_FOUND)
add_executable(cv-node ${CVNODESRC}) add_executable(cv-node ${CVNODESRC})
target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(cv-node Threads::Threads ${OpenCV_LIBS} glog) target_link_libraries(cv-node Threads::Threads ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog)
###############################################################################
# Find LibSGM
#
# This sets the following variables:
# LIBSGM_FOUND - True if LIBSGM was found.
# LIBSGM_INCLUDE_DIRS - Directories containing the LIBSGM include files.
# LIBSGM_LIBRARY - Libraries needed to use LIBSGM.
# Find lib
set(LIBSGM_FOUND FALSE CACHE BOOL "" FORCE)
find_library(LIBSGM_LIBRARY
NAMES sgm libsgm
PATH_SUFFIXES lib/
)
# Find include
find_path(LIBSGM_INCLUDE_DIRS
NAMES libsgm.h
PATH_SUFFIXES include/
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibSGM DEFAULT_MSG LIBSGM_LIBRARY LIBSGM_INCLUDE_DIRS)
message(STATUS "(LIBSGM_FOUND : ${LIBSGM_FOUND} include: ${LIBSGM_INCLUDE_DIRS}, lib: ${LIBSGM_LIBRARY})")
mark_as_advanced(LIBSGM_FOUND)
if(LIBSGM_FOUND)
set(LIBSGM_FOUND TRUE CACHE BOOL "" FORCE)
set(LIBSGM_LIBRARIES ${LIBSGM_LIBRARY})
message(STATUS "LibSGM found ( include: ${LIBSGM_INCLUDE_DIRS}, lib: ${LIBSGM_LIBRARY})")
endif()
...@@ -34,7 +34,10 @@ ...@@ -34,7 +34,10 @@
"minimum": 0, "minimum": 0,
"maximum": 208, "maximum": 208,
"tau": 0.0, "tau": 0.0,
"gamma": 0.0 "gamma": 0.0,
"window_size": 5,
"sigma": 1.5,
"lambda": 8000.0
} }
} }
#ifndef _FTL_ALGORITHMS_FIXSTARS_SGM_HPP_
#define _FTL_ALGORITHMS_FIXSTARS_SGM_HPP_
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <libsgm.h>
#include <ftl/disparity.hpp>
namespace ftl {
namespace algorithms {
class FixstarsSGM : public ftl::Disparity {
public:
FixstarsSGM(nlohmann::json &config);
void compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp);
static inline Disparity *create(nlohmann::json &config) { return new FixstarsSGM(config); }
private:
sgm::StereoSGM *ssgm_;
};
};
};
#endif // _FTL_ALGORITHMS_FIXSTARS_SGM_HPP_
#ifndef _FTL_ALGORITHMS_OPENCV_BM_HPP_
#define _FTL_ALGORITHMS_OPENCV_BM_HPP_
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/ximgproc.hpp"
#include <opencv2/calib3d.hpp>
#include <ftl/disparity.hpp>
namespace ftl {
namespace algorithms {
class OpenCVBM : public ftl::Disparity {
public:
OpenCVBM(nlohmann::json &config);
void compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp);
static inline Disparity *create(nlohmann::json &config) { return new OpenCVBM(config); }
private:
cv::Ptr<cv::StereoBM> left_matcher_;
cv::Ptr<cv::StereoMatcher> right_matcher_;
cv::Ptr<cv::ximgproc::DisparityWLSFilter> wls_filter_;
};
};
};
#endif // _FTL_ALGORITHMS_OPENCV_SGBM_HPP_
#include <ftl/algorithms/fixstars_sgm.hpp>
using ftl::algorithms::FixstarsSGM;
using namespace cv;
static ftl::Disparity::Register fixstarssgm("libsgm", FixstarsSGM::create);
FixstarsSGM::FixstarsSGM(nlohmann::json &config) : Disparity(config) {
ssgm_ = nullptr;
}
void FixstarsSGM::compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp) {
Mat left_disp;
Mat right_disp;
if (!ssgm_) {
ssgm_ = new sgm::StereoSGM(l.cols, l.rows, max_disp_, 8, 8, sgm::EXECUTE_INOUT_HOST2HOST);
}
if (disp.cols != l.cols || disp.rows != l.rows) {
disp = Mat(cv::Size(l.cols, l.rows), CV_8UC1);
}
ssgm_->execute(l.data, r.data, disp.data);
}
#include <ftl/algorithms/opencv_bm.hpp>
using ftl::algorithms::OpenCVBM;
using namespace cv::ximgproc;
using namespace cv;
static ftl::Disparity::Register opencvbm("bm", OpenCVBM::create);
OpenCVBM::OpenCVBM(nlohmann::json &config) : Disparity(config) {
int wsize = config.value("windows_size", 5);
float sigma = config.value("sigma", 1.5);
float lambda = config.value("lambda", 8000.0);
left_matcher_ = StereoBM::create(max_disp_,wsize);
wls_filter_ = createDisparityWLSFilter(left_matcher_);
right_matcher_ = createRightMatcher(left_matcher_);
wls_filter_->setLambda(lambda);
wls_filter_->setSigmaColor(sigma);
}
void OpenCVBM::compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp) {
Mat left_disp;
Mat right_disp;
left_matcher_-> compute(l, r,left_disp);
right_matcher_->compute(r,l, right_disp);
wls_filter_->filter(left_disp,l,disp,right_disp);
}
...@@ -7,10 +7,9 @@ using namespace cv; ...@@ -7,10 +7,9 @@ using namespace cv;
static ftl::Disparity::Register opencvsgbm("sgbm", OpenCVSGBM::create); static ftl::Disparity::Register opencvsgbm("sgbm", OpenCVSGBM::create);
OpenCVSGBM::OpenCVSGBM(nlohmann::json &config) : Disparity(config) { OpenCVSGBM::OpenCVSGBM(nlohmann::json &config) : Disparity(config) {
int wsize = 5; int wsize = config.value("windows_size", 5);
float sigma = 1.5; float sigma = config.value("sigma", 1.5);
float lambda = 8000.0; float lambda = config.value("lambda", 8000.0);
left_matcher_ = StereoSGBM::create(min_disp_,max_disp_,wsize); left_matcher_ = StereoSGBM::create(min_disp_,max_disp_,wsize);
left_matcher_->setP1(24*wsize*wsize); left_matcher_->setP1(24*wsize*wsize);
......
...@@ -23,12 +23,9 @@ using std::map; ...@@ -23,12 +23,9 @@ using std::map;
using cv::Mat; using cv::Mat;
using json = nlohmann::json; using json = nlohmann::json;
using std::ifstream; using std::ifstream;
using namespace cv; using namespace cv;
//using namespace cv::ximgproc;
static string OPTION_calibration_config = FTL_CONFIG_ROOT "/calibration.xml";
// Store loaded configuration
static json config; static json config;
/** /**
...@@ -42,6 +39,9 @@ static bool findConfiguration(const string &file) { ...@@ -42,6 +39,9 @@ static bool findConfiguration(const string &file) {
return true; return true;
} }
/**
* Generate a map from command line option to value
*/
map<string,string> read_options(char ***argv, int *argc) { map<string,string> read_options(char ***argv, int *argc) {
map<string,string> opts; map<string,string> opts;
...@@ -63,6 +63,10 @@ map<string,string> read_options(char ***argv, int *argc) { ...@@ -63,6 +63,10 @@ map<string,string> read_options(char ***argv, int *argc) {
return opts; return opts;
} }
/**
* Put command line options into json config. If config element does not exist
* or is of a different type then report an error.
*/
static void process_options(const map<string,string> &opts) { static void process_options(const map<string,string> &opts) {
for (auto opt : opts) { for (auto opt : opts) {
if (opt.first == "config") continue; if (opt.first == "config") continue;
...@@ -95,7 +99,6 @@ int main(int argc, char **argv) { ...@@ -95,7 +99,6 @@ int main(int argc, char **argv) {
// TODO Initiate the network // TODO Initiate the network
LocalSource *lsrc; LocalSource *lsrc;
if (argc) { if (argc) {
// Load video file // Load video file
lsrc = new LocalSource(argv[0], config["source"]); lsrc = new LocalSource(argv[0], config["source"]);
...@@ -114,16 +117,9 @@ int main(int argc, char **argv) { ...@@ -114,16 +117,9 @@ int main(int argc, char **argv) {
Calibrate calibrate(lsrc, config["calibration"]); Calibrate calibrate(lsrc, config["calibration"]);
if (config["calibrate"]) calibrate.recalibrate(); if (config["calibrate"]) calibrate.recalibrate();
if (!calibrate.isCalibrated()) LOG(WARNING) << "Cameras are not calibrated!"; if (!calibrate.isCalibrated()) LOG(WARNING) << "Cameras are not calibrated!";
/*Ptr<StereoBM> left_matcher = StereoBM::create(max_disp,wsize);
//left_matcher->setPreFilterCap(63);
wls_filter = createDisparityWLSFilter(left_matcher);
Ptr<StereoMatcher> right_matcher = createRightMatcher(left_matcher);*/
// Choose and configure disparity algorithm // Choose and configure disparity algorithm
auto disparity = Disparity::create(config["disparity"]); auto disparity = Disparity::create(config["disparity"]);
//double fact = 4.051863857;
Mat l, r, filtered_disp; Mat l, r, filtered_disp;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment