feat(bm model): add bm model

This commit is contained in:
TinyOh 2019-01-14 20:08:14 +08:00
parent e56bf190f6
commit c82a29968d
3 changed files with 99 additions and 37 deletions

View File

@ -16,6 +16,7 @@
#include <utility>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "mynteye/logger.h"
@ -23,41 +24,76 @@ MYNTEYE_BEGIN_NAMESPACE
const char DisparityProcessor::NAME[] = "DisparityProcessor";
DisparityProcessor::DisparityProcessor(std::int32_t proc_period)
: Processor(std::move(proc_period)) {
DisparityProcessor::DisparityProcessor(DisparityProcessorType type,
std::int32_t proc_period)
: Processor(std::move(proc_period)), type_(type) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
int sgbmWinSize = 3;
int numberOfDisparities = 64;
if (type_ == DisparityProcessorType::SGBM) {
int sgbmWinSize = 3;
int numberOfDisparities = 64;
#ifdef WITH_OPENCV2
// StereoSGBM
// http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html?#stereosgbm
sgbm_ = cv::Ptr<cv::StereoSGBM>(
new cv::StereoSGBM(
0, // minDisparity
numberOfDisparities, // numDisparities
sgbmWinSize, // SADWindowSize
8 * sgbmWinSize * sgbmWinSize, // P1
32 * sgbmWinSize * sgbmWinSize, // P2
1, // disp12MaxDiff
63, // preFilterCap
10, // uniquenessRatio
100, // speckleWindowSize
32, // speckleRange
false)); // fullDP
// StereoSGBM
// http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html?#stereosgbm
sgbm_matcher = cv::Ptr<cv::StereoSGBM>(
new cv::StereoSGBM(
0, // minDisparity
numberOfDisparities, // numDisparities
sgbmWinSize, // SADWindowSize
8 * sgbmWinSize * sgbmWinSize, // P1
32 * sgbmWinSize * sgbmWinSize, // P2
1, // disp12MaxDiff
63, // preFilterCap
10, // uniquenessRatio
100, // speckleWindowSize
32, // speckleRange
false)); // fullDP
#else
sgbm_ = cv::StereoSGBM::create(0, 16, 3);
sgbm_->setPreFilterCap(63);
sgbm_->setBlockSize(sgbmWinSize);
sgbm_->setP1(8 * sgbmWinSize * sgbmWinSize);
sgbm_->setP2(32 * sgbmWinSize * sgbmWinSize);
sgbm_->setMinDisparity(0);
sgbm_->setNumDisparities(numberOfDisparities);
sgbm_->setUniquenessRatio(10);
sgbm_->setSpeckleWindowSize(100);
sgbm_->setSpeckleRange(32);
sgbm_->setDisp12MaxDiff(1);
sgbm_matcher = cv::StereoSGBM::create(0, 16, 3);
sgbm_matcher->setPreFilterCap(63);
sgbm_matcher->setBlockSize(sgbmWinSize);
sgbm_matcher->setP1(8 * sgbmWinSize * sgbmWinSize);
sgbm_matcher->setP2(32 * sgbmWinSize * sgbmWinSize);
sgbm_matcher->setMinDisparity(0);
sgbm_matcher->setNumDisparities(numberOfDisparities);
sgbm_matcher->setUniquenessRatio(10);
sgbm_matcher->setSpeckleWindowSize(100);
sgbm_matcher->setSpeckleRange(32);
sgbm_matcher->setDisp12MaxDiff(1);
#endif
} else if (type_ == DisparityProcessorType::BM) {
#ifdef WITH_OPENCV2
int bmWinSize = 3;
// StereoBM
// https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereobm-stereobm
bm_matcher = cv::Ptr<cv::StereoBM>(new cv::StereoBM(
int 0,
64,
100,
8 * bmWinSize * bmWinSize,
32 * bmWinSize * bmWinSize,
int -1,
int 31,
15,
100,
4));
#else
bm_matcher = cv::StereoBM::create(0, 3);
bm_matcher->setPreFilterSize(9);
bm_matcher->setPreFilterCap(31);
bm_matcher->setBlockSize(15);
bm_matcher->setMinDisparity(0);
bm_matcher->setNumDisparities(64);
bm_matcher->setUniquenessRatio(15);
bm_matcher->setTextureThreshold(10);
bm_matcher->setSpeckleWindowSize(100);
bm_matcher->setSpeckleRange(4);
bm_matcher->setPreFilterType(cv::StereoBM::PREFILTER_XSOBEL);
#endif
} else {
LOG(ERROR) << "no enum DisparityProcessorType" << static_cast<int>(type);
}
}
DisparityProcessor::~DisparityProcessor() {
@ -87,7 +123,11 @@ bool DisparityProcessor::OnProcess(
// It contains disparity values scaled by 16. So, to get the floating-point
// disparity map,
// you need to divide each disp element by 16.
(*sgbm_)(input->first, input->second, disparity);
if (type_ == SGBM) {
(*sgbm_matcher)(input->first, input->second, disparity);
} else if (type_ == BM) {
(*bm_matcher)(input->first, input->second, disparity);
}
#else
// compute()
// http://docs.opencv.org/master/d2/d6e/classcv_1_1StereoMatcher.html
@ -96,7 +136,15 @@ bool DisparityProcessor::OnProcess(
// disparity map
// (where each disparity value has 4 fractional bits),
// whereas other algorithms output 32-bit floating-point disparity map.
sgbm_->compute(input->first, input->second, disparity);
if (type_ == DisparityProcessorType::SGBM) {
sgbm_matcher->compute(input->first, input->second, disparity);
} else if (type_ == DisparityProcessorType::BM) {
CvSize size = input->first.size();
cv::Mat tmp1, tmp2;
cv::cvtColor(input->first, tmp1, CV_RGB2GRAY);
cv::cvtColor(input->second, tmp2, CV_RGB2GRAY);
bm_matcher->compute(tmp1, tmp2, disparity);
}
#endif
disparity.convertTo(output->value, CV_32F, 1./16, 1);
output->id = input->first_id;

View File

@ -16,22 +16,33 @@
#pragma once
#include <string>
#include <opencv2/ximgproc/disparity_filter.hpp>
#include "mynteye/api/processor.h"
namespace cv {
class StereoSGBM;
class StereoBM;
} // namespace cv
enum class DisparityProcessorType : std::uint8_t {
/** bm */
SGBM = 0,
/** Equidistant: KANNALA_BRANDT */
BM = 1,
/** Unknow */
UNKNOW
};
MYNTEYE_BEGIN_NAMESPACE
class DisparityProcessor : public Processor {
public:
static const char NAME[];
explicit DisparityProcessor(std::int32_t proc_period = 0);
explicit DisparityProcessor(DisparityProcessorType type,
std::int32_t proc_period = 0);
virtual ~DisparityProcessor();
std::string Name() override;
@ -42,7 +53,9 @@ class DisparityProcessor : public Processor {
Object *const in, Object *const out, Processor *const parent) override;
private:
cv::Ptr<cv::StereoSGBM> sgbm_;
cv::Ptr<cv::StereoSGBM> sgbm_matcher;
cv::Ptr<cv::StereoBM> bm_matcher;
DisparityProcessorType type_;
};
MYNTEYE_END_NAMESPACE

View File

@ -613,7 +613,8 @@ void Synthetic::InitProcessors() {
rectify_processor = rectify_processor_ocv;
}
auto &&disparity_processor =
std::make_shared<DisparityProcessor>(DISPARITY_PROC_PERIOD);
std::make_shared<DisparityProcessor>(DisparityProcessorType::BM,
DISPARITY_PROC_PERIOD);
auto &&disparitynormalized_processor =
std::make_shared<DisparityNormalizedProcessor>(
DISPARITY_NORM_PROC_PERIOD);