diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 20d0b75..4167eb6 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -42,6 +42,7 @@ ADD_MODULE( EightBitsSampling "EightBitsSampling" ) ADD_MODULE( FrameCrop "FrameCrop" ) ADD_MODULE( LumaAverage "LumaAverage" ) IF( USE_OPENCV ) + ADD_MODULE( DisparityStereoBM "DisparityStereoBM" ) ADD_MODULE( DisparityStereoVar "DisparityStereoVar" ) ENDIF() diff --git a/modules/DisparityStereoBM.cpp b/modules/DisparityStereoBM.cpp new file mode 100644 index 0000000..e950391 --- /dev/null +++ b/modules/DisparityStereoBM.cpp @@ -0,0 +1,94 @@ +/* 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 DisparityStereoBM.cpp + * \brief Measure the disparity between two images using the Stereo Var method (OpenCV) + */ + +#include + +#include "DisparityStereoBM.h" + +namespace plaYUVer +{ + +DisparityStereoBM::DisparityStereoBM() +{ + /* Module Definition */ + m_iModuleAPI = MODULE_API_2; + m_iModuleType = FRAME_PROCESSING_MODULE; + m_pchModuleCategory = "Disparity"; + m_pchModuleName = "BlockMatching"; + 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; + + m_cModuleOptions.addOptions()/**/ + ( "block_size", m_cStereoBM.state->SADWindowSize, "Block Size (positive odd number) [9]" ); + + m_pcDisparityFrame = NULL; + + m_cStereoBM.state->preFilterCap = 31; + m_cStereoBM.state->SADWindowSize = 9; + m_cStereoBM.state->minDisparity = 0; + m_cStereoBM.state->textureThreshold = 10; + m_cStereoBM.state->uniquenessRatio = 15; + m_cStereoBM.state->speckleWindowSize = 100; + m_cStereoBM.state->speckleRange = 32; + m_cStereoBM.state->disp12MaxDiff = 1; + +} + +Bool DisparityStereoBM::create( std::vector apcFrameList ) +{ + _BASIC_MODULE_API_2_CHECK_ + m_pcDisparityFrame = new PlaYUVerFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), PlaYUVerFrame::GRAY ); + if( ( m_cStereoBM.state->SADWindowSize % 2 ) == 0 ) + { + m_cStereoBM.state->SADWindowSize++; + } + m_cStereoBM.state->numberOfDisparities = ( ( ( apcFrameList[0]->getWidth() / 8 ) + 15 ) & -16 ); + return true; +} +PlaYUVerFrame* DisparityStereoBM::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; + + m_cStereoBM( *leftImage, *rightImage, disparityImage ); + disparityImage.convertTo( disparityImage8, CV_8U ); + + m_pcDisparityFrame->fromCvMat( &disparityImage8 ); + return m_pcDisparityFrame; +} + +Void DisparityStereoBM::destroy() +{ + if( m_pcDisparityFrame ) + delete m_pcDisparityFrame; + m_pcDisparityFrame = NULL; +} + +} // NAMESPACE + diff --git a/modules/DisparityStereoBM.h b/modules/DisparityStereoBM.h new file mode 100644 index 0000000..5c40fe6 --- /dev/null +++ b/modules/DisparityStereoBM.h @@ -0,0 +1,58 @@ +/* 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 DisparityStereoBM.h + * \brief Measure the disparity between two images using the Stereo Var method (OpenCV) + */ + +#ifndef __DISPARITYSTEREOBM_H__ +#define __DISPARITYSTEREOBM_H__ + +#include +#include "PlaYUVerModuleIf.h" +#include + +#include + +namespace plaYUVer +{ + +class DisparityStereoBM: public PlaYUVerModuleIf +{ +REGISTER_CLASS_FACTORY( DisparityStereoBM ) + +private: + PlaYUVerFrame* m_pcDisparityFrame; + cv::StereoBM m_cStereoBM; +public: + DisparityStereoBM(); + virtual ~DisparityStereoBM() + { + } + + Bool create( std::vector apcFrameList ); + PlaYUVerFrame* process( std::vector apcFrameList ); + Void destroy(); +}; + +} // NAMESPACE + +#endif // __DISPARITYSTEREOBM_H__ + diff --git a/modules/PlaYUVerModuleFactory.cpp b/modules/PlaYUVerModuleFactory.cpp index eb63928..1a7e67c 100644 --- a/modules/PlaYUVerModuleFactory.cpp +++ b/modules/PlaYUVerModuleFactory.cpp @@ -44,6 +44,7 @@ PlaYUVerModuleFactory::PlaYUVerModuleFactory() Register( "LumaAverage", &LumaAverage::Create ); #ifdef USE_OPENCV Register( "DisparityStereoVar", &DisparityStereoVar::Create ); + Register( "DisparityStereoBM", &DisparityStereoBM::Create ); #endif }