From 56f1df4a56d35e0df14ca5217e5f858a41f47918 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 16 Nov 2019 01:44:31 +0000 Subject: [PATCH] Add new interface for modules using opencv --- lib/CMakeLists.txt | 3 ++ lib/CalypOpenCVModuleIf.cpp | 79 +++++++++++++++++++++++++++++ lib/CalypOpenCVModuleIf.h | 60 ++++++++++++++++++++++ modules/SaliencyDetection.cpp | 94 +++++++++++------------------------ modules/SaliencyDetection.h | 29 +++++------ 5 files changed, 183 insertions(+), 82 deletions(-) create mode 100644 lib/CalypOpenCVModuleIf.cpp create mode 100644 lib/CalypOpenCVModuleIf.h diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e353aef..a9afed2 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -47,6 +47,8 @@ IF( USE_OPENCV ) LIST( APPEND Calyp_Lib_SRCS StreamHandlerOpenCV.h ) LIST( APPEND Calyp_Lib_SRCS StreamHandlerOpenCV.cpp ) + LIST( APPEND Calyp_Lib_SRCS CalypOpenCVModuleIf.cpp ) + LIST(APPEND CMAKE_CFG_LINKER_LIBSS ${OpenCV_LIBRARIES} ) LIST(APPEND CMAKE_CFG_INCLUDE_DIRS ${OpenCV_INCLUDE_DIRS} ) ENDIF() @@ -57,6 +59,7 @@ set(Calyp_Lib_HEADERS CalypStream.h CalypOptions.h CalypModuleIf.h + CalypOpenCVModuleIf.h ) CONFIGURE_FILE( CalypConfig.cmake.in CalypConfig.cmake @ONLY) diff --git a/lib/CalypOpenCVModuleIf.cpp b/lib/CalypOpenCVModuleIf.cpp new file mode 100644 index 0000000..d52fa45 --- /dev/null +++ b/lib/CalypOpenCVModuleIf.cpp @@ -0,0 +1,79 @@ +/* This file is a part of Calyp project + * Copyright (C) 2014-2019 by Joao Carreira (jfmcarreira@gmail.com) + * Luis Lucas (luisfrlucas@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 CalypOpenCVModuleIf.cpp + * \brief Calyp modules interface for OpenCV + */ + +#include "CalypOpenCVModuleIf.h" + +// OpenCV +#include +#include + +using cv::Mat; + +bool CalypOpenCVModuleIf::create( std::vector apcFrameList ) +{ + _BASIC_MODULE_API_2_CHECK_ + bool bRet = false; + std::vector acMatList( apcFrameList.size() ); + for( unsigned i = 0; i < apcFrameList.size(); i++ ) + { + acMatList[i] = new Mat; + apcFrameList[i]->toMat( *acMatList[i], m_bConvertToGray ); + } + if( create_using_opencv( acMatList ) ) + { + unsigned pelFormat = m_bConvertToGray ? CalypFrame::findPixelFormat( "GRAY" ) : apcFrameList[0]->getPelFormat(); + m_pcOutputFrame = new CalypFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), pelFormat, apcFrameList[0]->getBitsPel() ); + bRet = true; + } + for( unsigned i = 0; i < apcFrameList.size(); i++ ) + { + delete acMatList[i]; + } + return bRet; +} + +CalypFrame* CalypOpenCVModuleIf::process( std::vector apcFrameList ) +{ + m_pcOutputFrame->reset(); + std::vector acMatList( apcFrameList.size() ); + for( unsigned i = 0; i < apcFrameList.size(); i++ ) + { + acMatList[i] = new Mat; + apcFrameList[i]->toMat( *acMatList[i], m_bConvertToGray ); + } + Mat* cvOutput = process_using_opencv( acMatList ); + m_pcOutputFrame->fromMat( *cvOutput ); + for( unsigned i = 0; i < apcFrameList.size(); i++ ) + { + delete acMatList[i]; + } + return m_pcOutputFrame; +} + +void CalypOpenCVModuleIf::destroy() +{ + if( m_pcOutputFrame ) + delete m_pcOutputFrame; + m_pcOutputFrame = NULL; +} diff --git a/lib/CalypOpenCVModuleIf.h b/lib/CalypOpenCVModuleIf.h new file mode 100644 index 0000000..142d5cc --- /dev/null +++ b/lib/CalypOpenCVModuleIf.h @@ -0,0 +1,60 @@ +/* This file is a part of Calyp project + * Copyright (C) 2014-2019 by Joao Carreira (jfmcarreira@gmail.com) + * Luis Lucas (luisfrlucas@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 CalypOpenCVModuleIf.h + * \ingroup CalypLibGroup + * \brief Calyp modules interface for OpenCV + */ + +#ifndef __CALYPOPENCVMODULEIF_H__ +#define __CALYPOPENCVMODULEIF_H__ + +#include "CalypModuleIf.h" + +/** + * \class CalypOpenCVModuleIf + * \ingroup CalypLib Calyp_Modules + * \brief Abstract class for modules using OpenCV library + */ +class CalypOpenCVModuleIf : public CalypModuleIf +{ +protected: + const char* m_pchPythonFunctionName; + bool m_bConvertToGray; + +public: + CalypOpenCVModuleIf() + { + m_bConvertToGray = false; + }; + virtual ~CalypOpenCVModuleIf() {} + + // Common API + bool create( std::vector apcFrameList ); + CalypFrame* process( std::vector apcFrameList ); + void destroy(); + + // API using OpenCV + virtual bool create_using_opencv( std::vector apcFrameList ) { return true; }; + virtual cv::Mat* process_using_opencv( std::vector apcFrameList ) = 0; + virtual void destroy_using_opencv(){}; +}; + +#endif // __CALYPOPENCVMODULEIF_H__ diff --git a/modules/SaliencyDetection.cpp b/modules/SaliencyDetection.cpp index 189c051..d895901 100644 --- a/modules/SaliencyDetection.cpp +++ b/modules/SaliencyDetection.cpp @@ -40,34 +40,12 @@ SaliencyDetectionModule::SaliencyDetectionModule() m_pchModuleCategory = "Saliency"; m_uiNumberOfFrames = 1; m_uiModuleRequirements = CLP_MODULE_REQUIRES_NEW_WINDOW; - - m_pcSaliencyFrame = NULL; + m_bConvertToGray = true; } -bool SaliencyDetectionModule::commonCreate( std::vector apcFrameList ) +void SaliencyDetectionModule::destroy_using_opencv() { - _BASIC_MODULE_API_2_CHECK_ - - m_pcSaliencyFrame = new CalypFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), CLP_GRAY ); - - return true; -} - -bool SaliencyDetectionModule::commonProcess( std::vector apcFrameList ) -{ - Mat cvFrame; - if( !apcFrameList[0]->toMat( cvFrame, true ) ) - { - return m_pcSaliencyFrame; - } - cvFrame.copyTo( m_matSaliency ); - return m_ptrSaliencyAlgorithm->computeSaliency( cvFrame, m_matSaliency ); -} - -void SaliencyDetectionModule::destroy() -{ - if( m_pcSaliencyFrame ) - delete m_pcSaliencyFrame; + delete m_pcvSaliency; } SaliencyDetectionSpectral::SaliencyDetectionSpectral() @@ -85,36 +63,26 @@ SaliencyDetectionSpectral::SaliencyDetectionSpectral() m_bBinaryMap = false; } -bool SaliencyDetectionSpectral::create( std::vector apcFrameList ) +bool SaliencyDetectionSpectral::create_using_opencv( std::vector apcMatList ) { - bool bRet = commonCreate( apcFrameList ); - if( !bRet ) - return bRet; - + m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = StaticSaliencySpectralResidual::create(); - return true; } -CalypFrame* SaliencyDetectionSpectral::process( std::vector apcFrameList ) +Mat* SaliencyDetectionSpectral::process_using_opencv( std::vector apcMatList ) { - if( commonProcess( apcFrameList ) ) + Mat cvSaliency; + m_ptrSaliencyAlgorithm->computeSaliency( *apcMatList[0], cvSaliency ); + Mat cvBinaryMap; + StaticSaliencySpectralResidual spec; + spec.computeBinaryMap( cvSaliency, *m_pcvSaliency ); + if( !m_bBinaryMap ) { - Mat matBinaryMap; - StaticSaliencySpectralResidual spec; - spec.computeBinaryMap( m_matSaliency, matBinaryMap ); - if( m_bBinaryMap ) - { - m_pcSaliencyFrame->fromMat( matBinaryMap ); - } - else - { - m_matSaliency *= 255; - m_matSaliency.convertTo( m_matSaliency, CV_8UC1 ); - m_pcSaliencyFrame->fromMat( m_matSaliency ); - } + cvSaliency *= 255; + cvSaliency.convertTo( *m_pcvSaliency, CV_8UC1 ); } - return m_pcSaliencyFrame; + return m_pcvSaliency; } SaliencyDetectionFineGrained::SaliencyDetectionFineGrained() @@ -125,20 +93,17 @@ SaliencyDetectionFineGrained::SaliencyDetectionFineGrained() m_pchModuleTooltip = "Measure saliency using fine grained method"; } -bool SaliencyDetectionFineGrained::create( std::vector apcFrameList ) +bool SaliencyDetectionFineGrained::create_using_opencv( std::vector apcMatList ) { - bool bRet = commonCreate( apcFrameList ); - if( !bRet ) - return bRet; + m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = cv::saliency::StaticSaliencyFineGrained::create(); return true; } -CalypFrame* SaliencyDetectionFineGrained::process( std::vector apcFrameList ) +Mat* SaliencyDetectionFineGrained::process_using_opencv( std::vector apcMatList ) { - if( commonProcess( apcFrameList ) ) - m_pcSaliencyFrame->fromMat( m_matSaliency ); - return m_pcSaliencyFrame; + m_ptrSaliencyAlgorithm->computeSaliency( *apcMatList[0], *m_pcvSaliency ); + return m_pcvSaliency; } SaliencyDetectionBinWangApr2014::SaliencyDetectionBinWangApr2014() @@ -149,25 +114,22 @@ SaliencyDetectionBinWangApr2014::SaliencyDetectionBinWangApr2014() m_pchModuleTooltip = "Measure saliency using a fast self-tuning background subtraction algorithm"; } -bool SaliencyDetectionBinWangApr2014::create( std::vector apcFrameList ) +bool SaliencyDetectionBinWangApr2014::create_using_opencv( std::vector apcMatList ) { - bool bRet = commonCreate( apcFrameList ); - if( !bRet ) - return bRet; + m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = MotionSaliencyBinWangApr2014::create(); - m_ptrSaliencyAlgorithm.dynamicCast()->setImagesize( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight() ); + m_ptrSaliencyAlgorithm.dynamicCast()->setImagesize( apcMatList[0]->cols, apcMatList[0]->rows ); m_ptrSaliencyAlgorithm.dynamicCast()->init(); return true; } -CalypFrame* SaliencyDetectionBinWangApr2014::process( std::vector apcFrameList ) +Mat* SaliencyDetectionBinWangApr2014::process_using_opencv( std::vector apcMatList ) { - if( commonProcess( apcFrameList ) ) + if( m_ptrSaliencyAlgorithm->computeSaliency( *apcMatList[0], *m_pcvSaliency ) ) { - m_matSaliency *= 255; - m_matSaliency.convertTo( m_matSaliency, CV_8UC1 ); + *m_pcvSaliency *= 255; + m_pcvSaliency->convertTo( *m_pcvSaliency, CV_8UC1 ); //cv::cvtColor(m_matSaliency, m_matSaliency, cv::COLOR_BGR2GRAY); - m_pcSaliencyFrame->fromMat( m_matSaliency ); } - return m_pcSaliencyFrame; + return m_pcvSaliency; } diff --git a/modules/SaliencyDetection.h b/modules/SaliencyDetection.h index 19b0fbb..62075e0 100644 --- a/modules/SaliencyDetection.h +++ b/modules/SaliencyDetection.h @@ -25,12 +25,14 @@ #ifndef __SALIENCYDETECTION_H__ #define __SALIENCYDETECTION_H__ +// CalypLib +#include "lib/CalypOpenCVModuleIf.h" + // OpenCV #include #include -// CalypLib -#include "lib/CalypModuleIf.h" +using cv::Mat; /** * \ingroup Calyp_Modules @@ -40,21 +42,16 @@ * @} */ -class SaliencyDetectionModule : public CalypModuleIf +class SaliencyDetectionModule : public CalypOpenCVModuleIf { protected: - CalypFrame* m_pcSaliencyFrame; - + Mat* m_pcvSaliency; cv::Ptr m_ptrSaliencyAlgorithm; - cv::Mat m_matSaliency; - - bool commonCreate( std::vector apcFrameList ); - bool commonProcess( std::vector apcFrameList ); public: SaliencyDetectionModule(); virtual ~SaliencyDetectionModule() {} - void destroy(); + void destroy_using_opencv(); }; /** @@ -78,8 +75,8 @@ private: public: SaliencyDetectionSpectral(); virtual ~SaliencyDetectionSpectral() {} - bool create( std::vector apcFrameList ); - CalypFrame* process( std::vector apcFrameList ); + bool create_using_opencv( std::vector apcMatList ); + Mat* process_using_opencv( std::vector apcMatList ); }; /** @@ -98,8 +95,8 @@ class SaliencyDetectionFineGrained : public SaliencyDetectionModule public: SaliencyDetectionFineGrained(); virtual ~SaliencyDetectionFineGrained() {} - bool create( std::vector apcFrameList ); - CalypFrame* process( std::vector apcFrameList ); + bool create_using_opencv( std::vector apcMatList ); + Mat* process_using_opencv( std::vector apcMatList ); }; /** @@ -115,8 +112,8 @@ class SaliencyDetectionBinWangApr2014 : public SaliencyDetectionModule public: SaliencyDetectionBinWangApr2014(); virtual ~SaliencyDetectionBinWangApr2014() {} - bool create( std::vector apcFrameList ); - CalypFrame* process( std::vector apcFrameList ); + bool create_using_opencv( std::vector apcMatList ); + Mat* process_using_opencv( std::vector apcMatList ); }; #endif // __SALIENCYDETECTION_H__