From 1b013e43f8ed828595c1c895383eb7fbada60c3d Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Wed, 15 Jul 2015 00:51:41 +0100 Subject: [PATCH] Add DisparityStereoSGBM module --- app/ModulesHandleOptDialog.cpp | 30 +++++++-- lib/ProgramOptions.h | 10 ++- modules/CMakeLists.txt | 1 + modules/DisparityStereoBM.cpp | 9 +-- modules/DisparityStereoBM.h | 1 + modules/DisparityStereoSGBM.cpp | 107 ++++++++++++++++++++++++++++++ modules/DisparityStereoSGBM.h | 76 +++++++++++++++++++++ modules/PlaYUVerModuleFactory.cpp | 1 + 8 files changed, 223 insertions(+), 12 deletions(-) create mode 100644 modules/DisparityStereoSGBM.cpp create mode 100644 modules/DisparityStereoSGBM.h diff --git a/app/ModulesHandleOptDialog.cpp b/app/ModulesHandleOptDialog.cpp index 1814f8d..5b3f18f 100644 --- a/app/ModulesHandleOptDialog.cpp +++ b/app/ModulesHandleOptDialog.cpp @@ -38,20 +38,34 @@ class OpionConfiguration: public QWidget public: OpionConfiguration( OptionBase* option ) { + m_pcChecked = NULL; m_cName = option->opt_string; m_pcDescription = new QLabel; m_pcDescription->setText( QString::fromStdString( option->opt_desc ) ); - m_pcValue = new QLineEdit; - m_pcValue->setMinimumWidth( 40 ); - m_pcValue->setMaximumWidth( 65 ); QHBoxLayout* layout = new QHBoxLayout; layout->addWidget( m_pcDescription, Qt::AlignLeft ); - layout->addWidget( m_pcValue, Qt::AlignRight ); + if( !option->isBinary() ) + { + m_pcValue = new QLineEdit; + m_pcValue->setMinimumWidth( 40 ); + m_pcValue->setMaximumWidth( 65 ); + layout->addWidget( m_pcValue, Qt::AlignRight ); + } + else + { + m_pcChecked = new QCheckBox; + m_pcChecked->setChecked( false ); + layout->addWidget( m_pcChecked, Qt::AlignRight ); + } layout->setContentsMargins( 1, 1, 1, 1 ); setLayout( layout ); } const QString getValue() const { + if( m_pcChecked ) + { + return m_pcChecked->isChecked() ? "true" : ""; + } return m_pcValue->text(); } const std::string& getName() @@ -60,6 +74,7 @@ public: } private: QLineEdit* m_pcValue; + QCheckBox* m_pcChecked; QLabel* m_pcDescription; std::string m_cName; }; @@ -116,8 +131,11 @@ Int ModulesHandleOptDialog::runConfiguration() { optionString.append( "--" ); optionString.append( m_apcOptionList.at( i )->getName() ); - optionString.append( "=" ); - optionString.append( valueString.toStdString() ); + if( !( valueString == "true" ) ) + { + optionString.append( "=" ); + optionString.append( valueString.toStdString() ); + } argsArray.push_back( optionString ); } optionString.clear(); diff --git a/lib/ProgramOptions.h b/lib/ProgramOptions.h index b27ae47..b3e1721 100644 --- a/lib/ProgramOptions.h +++ b/lib/ProgramOptions.h @@ -63,7 +63,6 @@ struct ParseFailure: public std::exception ~ParseFailure() throw() { } - ; std::string arg; std::string val; @@ -85,7 +84,6 @@ struct OptionBase opt_desc( desc ) { } - ; virtual ~OptionBase() { @@ -99,10 +97,15 @@ struct OptionBase { return arg_count; } + Bool isBinary() + { + return is_binary; + } int arg_count; std::string opt_string; std::string opt_desc; + Bool is_binary; }; /** Type specific option storage */ @@ -113,6 +116,7 @@ struct FlagOpt: public OptionBase opt_storage( false ), opt_default_val( default_val ) { + is_binary = true; } void parse( const std::string& arg ) @@ -137,6 +141,7 @@ struct StandardOpt: public OptionBase OptionBase( name, desc ), opt_storage( storage ) { + is_binary = false; } void parse( const std::string& arg ); @@ -194,6 +199,7 @@ struct OptionFunc: public OptionBase parent( parent_ ), func( func_ ) { + is_binary = false; } void parse( const std::string& arg ) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 942d507..6fab0ed 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -43,6 +43,7 @@ ADD_MODULE( FrameCrop "FrameCrop" ) ADD_MODULE( LumaAverage "LumaAverage" ) IF( USE_OPENCV ) ADD_MODULE( DisparityStereoBM "DisparityStereoBM" ) + ADD_MODULE( DisparityStereoSGBM "DisparityStereoSGBM" ) #ADD_MODULE( DisparityStereoVar "DisparityStereoVar" ) ENDIF() diff --git a/modules/DisparityStereoBM.cpp b/modules/DisparityStereoBM.cpp index 5aeb702..fadd833 100644 --- a/modules/DisparityStereoBM.cpp +++ b/modules/DisparityStereoBM.cpp @@ -34,8 +34,8 @@ DisparityStereoBM::DisparityStereoBM() /* Module Definition */ m_iModuleAPI = MODULE_API_2; m_iModuleType = FRAME_PROCESSING_MODULE; - m_pchModuleCategory = "Disparity"; - m_pchModuleName = "BlockMatching"; + m_pchModuleCategory = "Stereo"; + m_pchModuleName = "Disparity-BM"; m_pchModuleTooltip = "Measure the disparity between two images using the Stereo BM method (OpenCV)"; m_uiNumberOfFrames = MODULE_REQUIRES_TWO_FRAMES; m_uiModuleRequirements = MODULE_REQUIRES_SKIP_WHILE_PLAY | MODULE_REQUIRES_NEW_WINDOW | MODULE_REQUIRES_OPTIONS; @@ -56,6 +56,7 @@ Bool DisparityStereoBM::create( std::vector apcFrameList ) { m_uiBlockSize++; } + m_uiNumberOfDisparities = ( ( apcFrameList[0]->getWidth() / 8 ) + 15 ) & -16; #if( CV_MAJOR_VERSION == 2) m_cStereoBM.state->preFilterCap = 31; m_cStereoBM.state->SADWindowSize = 9; @@ -66,13 +67,13 @@ Bool DisparityStereoBM::create( std::vector apcFrameList ) m_cStereoBM.state->speckleRange = 32; m_cStereoBM.state->disp12MaxDiff = 1; m_cStereoBM.state->SADWindowSize = m_uiBlockSize; - m_cStereoBM.state->numberOfDisparities = ( ( ( apcFrameList[0]->getWidth() / 8 ) + 15 ) & -16 ); + m_cStereoBM.state->numberOfDisparities = m_uiNumberOfDisparities; #else m_cStereoBM = cv::StereoBM::create( 16, m_uiBlockSize ); m_cStereoBM->setPreFilterCap( 31 ); m_cStereoBM->setBlockSize( m_uiBlockSize ); m_cStereoBM->setMinDisparity( 0 ); - m_cStereoBM->setNumDisparities( ( ( ( apcFrameList[0]->getWidth() / 8 ) + 15 ) & -16 ) ); + m_cStereoBM->setNumDisparities( m_uiNumberOfDisparities ); m_cStereoBM->setTextureThreshold( 10 ); m_cStereoBM->setUniquenessRatio( 15 ); m_cStereoBM->setSpeckleWindowSize( 100 ); diff --git a/modules/DisparityStereoBM.h b/modules/DisparityStereoBM.h index 41b0f55..f7ddaf6 100644 --- a/modules/DisparityStereoBM.h +++ b/modules/DisparityStereoBM.h @@ -40,6 +40,7 @@ REGISTER_CLASS_FACTORY( DisparityStereoBM ) private: PlaYUVerFrame* m_pcDisparityFrame; + Int m_uiNumberOfDisparities; UInt m_uiBlockSize; #if( CV_MAJOR_VERSION == 2) cv::StereoBM m_cStereoBM; diff --git a/modules/DisparityStereoSGBM.cpp b/modules/DisparityStereoSGBM.cpp new file mode 100644 index 0000000..610bdc0 --- /dev/null +++ b/modules/DisparityStereoSGBM.cpp @@ -0,0 +1,107 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014-2015 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * \file DisparityStereoSGBM.cpp + * \brief Measure the disparity between two images using the SGBM method (OpenCV) + */ + +#include + +#include "DisparityStereoSGBM.h" + +namespace plaYUVer +{ + +DisparityStereoSGBM::DisparityStereoSGBM() +{ + /* Module Definition */ + m_iModuleAPI = MODULE_API_2; + m_iModuleType = FRAME_PROCESSING_MODULE; + m_pchModuleCategory = "Stereo"; + m_pchModuleName = "Disparity-SGBM"; + m_pchModuleTooltip = "Measure the disparity between two images using the Stereo SGBM method (OpenCV)"; + m_uiNumberOfFrames = MODULE_REQUIRES_TWO_FRAMES; + m_uiModuleRequirements = MODULE_REQUIRES_SKIP_WHILE_PLAY | MODULE_REQUIRES_NEW_WINDOW | MODULE_REQUIRES_OPTIONS; + + m_cModuleOptions.addOptions()/**/ + ( "block_size", m_uiBlockSize, "Block Size (positive odd number) [3]" ); + //( "HHAlgorithm", m_bUseHH, "Use HH algorithm [false]" ); + + m_pcDisparityFrame = NULL; + m_uiBlockSize = 9; + +} + +Bool DisparityStereoSGBM::create( std::vector apcFrameList ) +{ + _BASIC_MODULE_API_2_CHECK_ + m_pcDisparityFrame = new PlaYUVerFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), PlaYUVerFrame::GRAY ); + if( ( m_uiBlockSize % 2 ) == 0 ) + { + m_uiBlockSize++; + } + m_uiNumberOfDisparities = ( ( apcFrameList[0]->getWidth() / 8 ) + 15 ) & -16; +#if( CV_MAJOR_VERSION == 3) + m_cStereoMatch = cv::StereoSGBM::create( 0, 16, m_uiBlockSize ); + m_cStereoMatch->setPreFilterCap(63); + int cn = apcFrameList[0]->getNumberChannels(); + m_cStereoMatch->setP1( 8 * cn * m_uiBlockSize * m_uiBlockSize ); + m_cStereoMatch->setP2( 32 * cn * m_uiBlockSize * m_uiBlockSize ); + m_cStereoMatch->setMinDisparity( 0 ); + m_cStereoMatch->setNumDisparities( m_uiNumberOfDisparities ); + m_cStereoMatch->setUniquenessRatio( 10 ); + m_cStereoMatch->setSpeckleWindowSize( 100 ); + m_cStereoMatch->setSpeckleRange( 32 ); + m_cStereoMatch->setDisp12MaxDiff( 1 ); + //m_cStereoMatch->setMode( alg == STEREO_HH ? StereoSGBM::MODE_HH : StereoSGBM::MODE_SGBM ); + m_cStereoMatch->setMode( m_bUseHH ? cv::StereoSGBM::MODE_HH :cv::StereoSGBM::MODE_SGBM ); +#endif + return true; +} +PlaYUVerFrame* DisparityStereoSGBM::process( std::vector apcFrameList ) +{ + PlaYUVerFrame* InputLeft = apcFrameList[0]; + PlaYUVerFrame* InputRight = apcFrameList[1]; + + cv::Mat* leftImage = InputLeft->getCvMat( true ); + cv::Mat* rightImage = InputRight->getCvMat( true ); + cv::Mat disparityImage, disparityImage8; + +#if( CV_MAJOR_VERSION == 3) + m_cStereoMatch->compute( *leftImage, *rightImage, disparityImage ); +#else + m_cStereoMatch( *leftImage, *rightImage, disparityImage ); +#endif + + disparityImage.convertTo( disparityImage8, CV_8U ); + + m_pcDisparityFrame->fromCvMat( &disparityImage8 ); + return m_pcDisparityFrame; +} + +Void DisparityStereoSGBM::destroy() +{ + if( m_pcDisparityFrame ) + delete m_pcDisparityFrame; + m_pcDisparityFrame = NULL; +} + +} // NAMESPACE + diff --git a/modules/DisparityStereoSGBM.h b/modules/DisparityStereoSGBM.h new file mode 100644 index 0000000..deaf691 --- /dev/null +++ b/modules/DisparityStereoSGBM.h @@ -0,0 +1,76 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014-2015 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * \file DisparityStereoSGBM.h + * \brief Measure the disparity between two images using the SGBM method (OpenCV) + * The class implements the modified H. Hirschmuller algorithm + * that differs from the original one as follows: + * - By default, the algorithm is single-pass, which means that you consider only 5 directions + * instead of 8. Set mode=StereoSGBM::MODE_HH in createStereoSGBM to run the full variant of the + * algorithm but beware that it may consume a lot of memory. + * - The algorithm matches blocks, not individual pixels. Though, setting blockSize=1 reduces the + * blocks to single pixels. + * - Mutual information cost function is not implemented. Instead, a simpler Birchfield-Tomasi + * sub-pixel metric from @cite BT98 is used. Though, the color images are supported as well. + * - Some pre- and post- processing steps from K. Konolige algorithm StereoBM are included, for + * example: pre-filtering (StereoBM::PREFILTER_XSOBEL type) and post-filtering (uniqueness + * check, quadratic interpolation and speckle filtering). + */ + +#ifndef __DISPARITYSTEREOSGBM_H__ +#define __DISPARITYSTEREOSGBM_H__ + +#include +#include "PlaYUVerModuleIf.h" +#include + +#include + +namespace plaYUVer +{ + +class DisparityStereoSGBM: public PlaYUVerModuleIf +{ +REGISTER_CLASS_FACTORY( DisparityStereoSGBM ) + +private: + PlaYUVerFrame* m_pcDisparityFrame; + Bool m_bUseHH; + Int m_uiNumberOfDisparities; + UInt m_uiBlockSize; +#if( CV_MAJOR_VERSION == 3) + cv::Ptr m_cStereoMatch; +#endif + +public: + DisparityStereoSGBM(); + virtual ~DisparityStereoSGBM() + { + } + + Bool create( std::vector apcFrameList ); + PlaYUVerFrame* process( std::vector apcFrameList ); + Void destroy(); +}; + +} // NAMESPACE + +#endif // __DISPARITYSTEREOSGBM_H__ + diff --git a/modules/PlaYUVerModuleFactory.cpp b/modules/PlaYUVerModuleFactory.cpp index 2181424..2148714 100644 --- a/modules/PlaYUVerModuleFactory.cpp +++ b/modules/PlaYUVerModuleFactory.cpp @@ -46,6 +46,7 @@ PlaYUVerModuleFactory::PlaYUVerModuleFactory() #ifdef USE_OPENCV //Register( "DisparityStereoVar", &DisparityStereoVar::Create ); Register( "DisparityStereoBM", &DisparityStereoBM::Create ); + Register( "DisparityStereoSGBM", &DisparityStereoSGBM::Create ); #endif }