From 56f1df4a56d35e0df14ca5217e5f858a41f47918 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 16 Nov 2019 01:44:31 +0000 Subject: [PATCH 1/4] 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__ From 81972c9ded98725ee71e7319ffa86e9d28830c77 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 16 Nov 2019 02:09:56 +0000 Subject: [PATCH 2/4] Add headers to the CMakeLists --- lib/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index a9afed2..d73bccb 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -29,12 +29,12 @@ set(Calyp_Lib_SRCS CalypOptions.cpp # Modules CalypModuleIf.h + CalypOpenCVModuleIf.h ) LIST(APPEND CMAKE_CFG_LINKER_LIBSS ${PROJECT_LIBRARY} ) LIST(APPEND CMAKE_CFG_INCLUDE_DIRS ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR} ) - IF( USE_FFMPEG ) LIST( APPEND Calyp_Lib_SRCS StreamHandlerLibav.h ) LIST( APPEND Calyp_Lib_SRCS StreamHandlerLibav.cpp ) From ce82d0a17dc85c490767be07ff4ace58c9c82fb5 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 16 Nov 2019 02:27:47 +0000 Subject: [PATCH 3/4] Change the opencv includes --- lib/CalypOpenCVModuleIf.cpp | 1 - lib/CalypOpenCVModuleIf.h | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/CalypOpenCVModuleIf.cpp b/lib/CalypOpenCVModuleIf.cpp index d52fa45..fdd6055 100644 --- a/lib/CalypOpenCVModuleIf.cpp +++ b/lib/CalypOpenCVModuleIf.cpp @@ -25,7 +25,6 @@ #include "CalypOpenCVModuleIf.h" // OpenCV -#include #include using cv::Mat; diff --git a/lib/CalypOpenCVModuleIf.h b/lib/CalypOpenCVModuleIf.h index 142d5cc..ea4ef34 100644 --- a/lib/CalypOpenCVModuleIf.h +++ b/lib/CalypOpenCVModuleIf.h @@ -28,6 +28,11 @@ #include "CalypModuleIf.h" +namespace cv +{ +class Mat; +} + /** * \class CalypOpenCVModuleIf * \ingroup CalypLib Calyp_Modules From 3ed8b0916f982830f1b9ec3b3c6d1528a1dbd33e Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 23 Nov 2019 01:25:46 +0000 Subject: [PATCH 4/4] Add new module FrameResampling and change opencv interface --- lib/CalypOpenCVModuleIf.cpp | 9 ++- lib/CalypOpenCVModuleIf.h | 2 +- modules/CMakeLists.txt | 2 + modules/FrameResampling.cpp | 104 ++++++++++++++++++++++++++++++++++ modules/FrameResampling.h | 54 ++++++++++++++++++ modules/SaliencyDetection.cpp | 18 +++--- modules/SaliencyDetection.h | 6 +- 7 files changed, 180 insertions(+), 15 deletions(-) create mode 100644 modules/FrameResampling.cpp create mode 100644 modules/FrameResampling.h diff --git a/lib/CalypOpenCVModuleIf.cpp b/lib/CalypOpenCVModuleIf.cpp index fdd6055..207f898 100644 --- a/lib/CalypOpenCVModuleIf.cpp +++ b/lib/CalypOpenCVModuleIf.cpp @@ -39,12 +39,17 @@ bool CalypOpenCVModuleIf::create( std::vector apcFrameList ) acMatList[i] = new Mat; apcFrameList[i]->toMat( *acMatList[i], m_bConvertToGray ); } - if( create_using_opencv( acMatList ) ) + Mat* outputMat = create_using_opencv( acMatList ); + if( outputMat ) { unsigned pelFormat = m_bConvertToGray ? CalypFrame::findPixelFormat( "GRAY" ) : apcFrameList[0]->getPelFormat(); - m_pcOutputFrame = new CalypFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), pelFormat, apcFrameList[0]->getBitsPel() ); + m_pcOutputFrame = new CalypFrame( outputMat->cols, outputMat->rows, pelFormat, apcFrameList[0]->getBitsPel() ); bRet = true; } + else + { + return false; + } for( unsigned i = 0; i < apcFrameList.size(); i++ ) { delete acMatList[i]; diff --git a/lib/CalypOpenCVModuleIf.h b/lib/CalypOpenCVModuleIf.h index ea4ef34..16b7662 100644 --- a/lib/CalypOpenCVModuleIf.h +++ b/lib/CalypOpenCVModuleIf.h @@ -57,7 +57,7 @@ public: void destroy(); // API using OpenCV - virtual bool create_using_opencv( std::vector apcFrameList ) { return true; }; + virtual cv::Mat* create_using_opencv( std::vector apcFrameList ) { return NULL; }; virtual cv::Mat* process_using_opencv( std::vector apcFrameList ) = 0; virtual void destroy_using_opencv(){}; }; diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index ba97a34..69fed69 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -46,6 +46,8 @@ ADD_MODULE_USE_OPENCV( SaliencyDetectionSpectral "SaliencyDetection" VERSION 3.3 ADD_MODULE_USE_OPENCV( SaliencyDetectionFineGrained "SaliencyDetection" VERSION 3.3.0 MODULES saliency ) ADD_MODULE_USE_OPENCV( SaliencyDetectionBinWangApr2014 "SaliencyDetection" VERSION 3.3.0 MODULES saliency ) +ADD_MODULE_USE_OPENCV( FrameResampling "FrameResampling" VERSION 3.3.0 MODULES core ) + ADD_MODULE_USE_OPENCV( ThreeSixtySpatialtoTemporal "ThreeSixtySpatialtoTemporal" VERSION 3.1.0 MODULES core ) ADD_MODULE_USE_OPENCV( ThreeSixtyFaceExtraction "ThreeSixtyFaceExtraction" VERSION 3.1.0 MODULES core ) ADD_MODULE_USE_OPENCV( ThreeSixtyFaceConcatenation "ThreeSixtyFaceConcatenation" VERSION 3.1.0 MODULES core ) diff --git a/modules/FrameResampling.cpp b/modules/FrameResampling.cpp new file mode 100644 index 0000000..d8e2963 --- /dev/null +++ b/modules/FrameResampling.cpp @@ -0,0 +1,104 @@ +/* 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 FrameResampling.cpp + * \brief Modules to resampling frames + */ + +#include "FrameResampling.h" + +#include + +// OpenCV +#include +#include + +using cv::Mat; +using cv::Size; + +FrameResampling::FrameResampling() +{ + /* Module Definition */ + m_iModuleAPI = CLP_MODULE_API_2; + m_iModuleType = CLP_FRAME_PROCESSING_MODULE; + m_pchModuleCategory = "Utilities"; + m_pchModuleName = "FrameResampling"; + m_pchModuleLongName = "Frame Resampling"; + m_pchModuleTooltip = "Resampling frame to an abritary resolution"; + m_uiNumberOfFrames = 1; + m_uiModuleRequirements = CLP_MODULE_REQUIRES_OPTIONS; + + m_cModuleOptions.addOptions() /**/ + ( "width", m_iWidth, "Width of the crop region [-1]" ) /**/ + ( "height", m_iHeight, "Height of the crop region [-1]" ) /**/ + ( "Interpolation", m_iInterpolation, "Interpolation method (1-4) [4]" ) ; + + m_iInterpolation = 4; + m_iWidth = -1; + m_iHeight = -1; +} + +bool FrameResampling::create( std::vector apcFrameList ) +{ + switch( m_iInterpolation ) + { + case 2: + m_iInterpolation = cv::INTER_LINEAR; + break; + case 3: + m_iInterpolation = cv::INTER_CUBIC; + break; + case 4: + m_iInterpolation = cv::INTER_LANCZOS4; + break; + default: + m_iInterpolation = cv::INTER_NEAREST; + } + if( m_iWidth == -1 ) + { + m_iWidth = apcFrameList[0]->getWidth(); + } + if( m_iHeight == -1 ) + { + m_iHeight = apcFrameList[0]->getHeight() * double( m_iWidth ) / double( apcFrameList[0]->getWidth() ); + } + m_pcOutputFrame = new CalypFrame( m_iWidth, m_iHeight, apcFrameList[0]->getPelFormat(), apcFrameList[0]->getBitsPel() ); + return m_pcOutputFrame; +} + +CalypFrame* FrameResampling::process( std::vector apcFrameList ) +{ + m_pcOutputFrame->reset(); + for( unsigned ch = 0; ch < apcFrameList[0]->getNumberChannels(); ch++ ) + { + Mat inputFrame, outputFrame; + cv::Size outSize( m_pcOutputFrame->getWidth( ch ), m_pcOutputFrame->getHeight( ch ) ); + apcFrameList[0]->toMat( inputFrame, true, false, ch ); + cv::resize( inputFrame, outputFrame, outSize, 0, 0, m_iInterpolation ); + m_pcOutputFrame->fromMat( outputFrame, ch ); + } + return m_pcOutputFrame; +} + +void FrameResampling::destroy() +{ + if( m_pcOutputFrame ) + delete m_pcOutputFrame; +} diff --git a/modules/FrameResampling.h b/modules/FrameResampling.h new file mode 100644 index 0000000..bde5ded --- /dev/null +++ b/modules/FrameResampling.h @@ -0,0 +1,54 @@ +/* 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 FrameResampling.h + * \brief Modules to resampling frames + */ + +#ifndef __FRAMERESAMPLING_H__ +#define __FRAMERESAMPLING_H__ + +// CalypLib +#include "lib/CalypModuleIf.h" + +/** + * \ingroup Calyp_Modules + * @defgroup Calyp_Modules_Saliency Saliency + * @{ + * Frame processing modules for saliency detection + * @} + */ + +class FrameResampling : public CalypModuleIf +{ + REGISTER_CLASS_FACTORY( FrameResampling ) +private: + int m_iWidth; + int m_iHeight; +int m_iInterpolation; +public: + FrameResampling(); + virtual ~FrameResampling() {} + bool create( std::vector apcFrameList ); + CalypFrame* process( std::vector apcFrameList ); + void destroy(); +}; + +#endif // __FRAMERESAMPLING_H__ diff --git a/modules/SaliencyDetection.cpp b/modules/SaliencyDetection.cpp index d895901..90755b7 100644 --- a/modules/SaliencyDetection.cpp +++ b/modules/SaliencyDetection.cpp @@ -63,11 +63,11 @@ SaliencyDetectionSpectral::SaliencyDetectionSpectral() m_bBinaryMap = false; } -bool SaliencyDetectionSpectral::create_using_opencv( std::vector apcMatList ) +cv::Mat* SaliencyDetectionSpectral::create_using_opencv( std::vector apcMatList ) { - m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = StaticSaliencySpectralResidual::create(); - return true; + m_pcvSaliency = new Mat( apcMatList[0]->rows, apcMatList[0]->cols, CV_8UC1 ); + return m_pcvSaliency; } Mat* SaliencyDetectionSpectral::process_using_opencv( std::vector apcMatList ) @@ -93,11 +93,11 @@ SaliencyDetectionFineGrained::SaliencyDetectionFineGrained() m_pchModuleTooltip = "Measure saliency using fine grained method"; } -bool SaliencyDetectionFineGrained::create_using_opencv( std::vector apcMatList ) +Mat* SaliencyDetectionFineGrained::create_using_opencv( std::vector apcMatList ) { - m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = cv::saliency::StaticSaliencyFineGrained::create(); - return true; + m_pcvSaliency = new Mat( apcMatList[0]->rows, apcMatList[0]->cols, CV_8UC1 ); + return m_pcvSaliency; } Mat* SaliencyDetectionFineGrained::process_using_opencv( std::vector apcMatList ) @@ -114,13 +114,13 @@ SaliencyDetectionBinWangApr2014::SaliencyDetectionBinWangApr2014() m_pchModuleTooltip = "Measure saliency using a fast self-tuning background subtraction algorithm"; } -bool SaliencyDetectionBinWangApr2014::create_using_opencv( std::vector apcMatList ) +Mat* SaliencyDetectionBinWangApr2014::create_using_opencv( std::vector apcMatList ) { - m_pcvSaliency = new Mat; m_ptrSaliencyAlgorithm = MotionSaliencyBinWangApr2014::create(); m_ptrSaliencyAlgorithm.dynamicCast()->setImagesize( apcMatList[0]->cols, apcMatList[0]->rows ); m_ptrSaliencyAlgorithm.dynamicCast()->init(); - return true; + m_pcvSaliency = new Mat( apcMatList[0]->rows, apcMatList[0]->cols, CV_8UC1 ); + return m_pcvSaliency; } Mat* SaliencyDetectionBinWangApr2014::process_using_opencv( std::vector apcMatList ) diff --git a/modules/SaliencyDetection.h b/modules/SaliencyDetection.h index 62075e0..5974e48 100644 --- a/modules/SaliencyDetection.h +++ b/modules/SaliencyDetection.h @@ -75,7 +75,7 @@ private: public: SaliencyDetectionSpectral(); virtual ~SaliencyDetectionSpectral() {} - bool create_using_opencv( std::vector apcMatList ); + Mat* create_using_opencv( std::vector apcMatList ); Mat* process_using_opencv( std::vector apcMatList ); }; @@ -95,7 +95,7 @@ class SaliencyDetectionFineGrained : public SaliencyDetectionModule public: SaliencyDetectionFineGrained(); virtual ~SaliencyDetectionFineGrained() {} - bool create_using_opencv( std::vector apcMatList ); + Mat* create_using_opencv( std::vector apcMatList ); Mat* process_using_opencv( std::vector apcMatList ); }; @@ -112,7 +112,7 @@ class SaliencyDetectionBinWangApr2014 : public SaliencyDetectionModule public: SaliencyDetectionBinWangApr2014(); virtual ~SaliencyDetectionBinWangApr2014() {} - bool create_using_opencv( std::vector apcMatList ); + Mat* create_using_opencv( std::vector apcMatList ); Mat* process_using_opencv( std::vector apcMatList ); };