diff --git a/cv-node/CMakeLists.txt b/cv-node/CMakeLists.txt index ef5221d0fd5c7e33ec08943cdc83e619ea9b1679..cafa37799cef27a37f5b9358116ec8c8b7e9d390 100644 --- a/cv-node/CMakeLists.txt +++ b/cv-node/CMakeLists.txt @@ -22,6 +22,7 @@ set(CVNODESRC src/algorithms/rtcensus_sgm.cpp src/algorithms/opencv_sgbm.cpp src/algorithms/opencv_bm.cpp + src/algorithms/elas.cpp ) if (LIBSGM_FOUND) @@ -43,12 +44,13 @@ endif (CUDA_FOUND) add_executable(cv-node ${CVNODESRC}) add_dependencies(cv-node ftlnet) +add_dependencies(cv-node libelas) if (CUDA_FOUND) set_property(TARGET cv-node PROPERTY CUDA_SEPARABLE_COMPILATION ON) endif() #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(cv-node Threads::Threads ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet) +target_link_libraries(cv-node Threads::Threads libelas ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet) diff --git a/cv-node/include/ftl/algorithms/elas.hpp b/cv-node/include/ftl/algorithms/elas.hpp new file mode 100644 index 0000000000000000000000000000000000000000..02f72e3e20b3cf1b94de6fdbc8a55283ba90f3da --- /dev/null +++ b/cv-node/include/ftl/algorithms/elas.hpp @@ -0,0 +1,39 @@ +/* + * Copyright 2019 Nicolas Pope + */ + +#ifndef _FTL_ALGORITHMS_ELAS_HPP_ +#define _FTL_ALGORITHMS_ELAS_HPP_ + +#include <opencv2/core.hpp> +#include <opencv2/opencv.hpp> +#include <elas.h> +#include <ftl/disparity.hpp> + +namespace ftl { +namespace algorithms { + +/** + * LibELAS - Efficient Large-scale Stereo Matching + * @see http://www.cvlibs.net/software/libelas/ + */ +class ELAS : public ftl::Disparity { + public: + explicit ELAS(nlohmann::json &config); + + void compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp); + + /* Factory creator */ + static inline Disparity *create(nlohmann::json &config) { + return new ELAS(config); + } + + private: + Elas::parameters param_; + Elas *elas_; +}; +}; +}; + +#endif // _FTL_ALGORITHMS_ELAS_HPP_ + diff --git a/cv-node/src/algorithms/elas.cpp b/cv-node/src/algorithms/elas.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2cb3b6b69254418823802d29de3dcc1bf28ed6b --- /dev/null +++ b/cv-node/src/algorithms/elas.cpp @@ -0,0 +1,41 @@ +/* Copyright 2019 Nicolas Pope */ + +#include <ftl/algorithms/elas.hpp> +#include <glog/logging.h> + +using ftl::algorithms::ELAS; +using cv::Mat; + +static ftl::Disparity::Register elass("elas", ELAS::create); + +ELAS::ELAS(nlohmann::json &config) : Disparity(config) { + param_.postprocess_only_left = true; + elas_ = new Elas(param_); +} + +void ELAS::compute(const cv::Mat &l, const cv::Mat &r, cv::Mat &disp) { + //Mat left_disp; + //Mat right_disp; + + Mat lbw, rbw; + cv::cvtColor(l, lbw, cv::COLOR_BGR2GRAY); + cv::cvtColor(r, rbw, cv::COLOR_BGR2GRAY); + + disp = Mat(cv::Size(l.cols, l.rows), CV_32F); + Mat dispr(cv::Size(l.cols, l.rows), CV_32F); + + const int32_t dims[3] = {l.cols,l.rows,l.step/sizeof(float)}; + + if (disp.step/sizeof(float) != lbw.step) LOG(WARNING) << "Incorrect disparity step"; + + auto start = std::chrono::high_resolution_clock::now(); + elas_->process(lbw.data, rbw.data, (float*)disp.data, (float*)dispr.data, dims); + std::chrono::duration<double> elapsed = + std::chrono::high_resolution_clock::now() - start; + LOG(INFO) << "Elas in " << elapsed.count() << "s"; + + //disp.convertTo(disp, CV_32F, 1.0f); +} + + +