From 50cc22cb46c1a9006750278849af70505ac6b9b6 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Fri, 2 Feb 2018 03:54:44 +0000 Subject: [PATCH] Add saliency detection modules --- modules/CMakeLists.txt | 12 +-- modules/SaliencyDetection.cpp | 137 ++++++++++++++++++++++++++++++++++ modules/SaliencyDetection.h | 75 +++++++++++++++++++ 3 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 modules/SaliencyDetection.cpp create mode 100644 modules/SaliencyDetection.h diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index df6dcca..58912c3 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -30,17 +30,19 @@ ADD_MODULE( LumaAverage "LumaAverage" ) ADD_MODULE( WeightedPSNR "WeightedPSNR" ) ADD_MODULE_USE_OPENCV( MeasureOpticalFlowDualTVL1 "MeasureOpticalFlowDualTVL1" VERSION 2.4.13.0 MODULES video ) - +ADD_MODULE_USE_OPENCV( DisparityStereoBM "DisparityStereoBM" VERSION 2.4.13.0 MODULES calib3d ) +ADD_MODULE_USE_OPENCV( DisparityStereoSGBM "DisparityStereoSGBM" VERSION 2.4.13.0 MODULES calib3d ) ADD_MODULE_USE_OPENCV( OpticalFlowDualTVL1 "OpticalFlow" VERSION 3.1.0 MODULES optflow ) ADD_MODULE_USE_OPENCV( OpticalFlowSparseToDense "OpticalFlow" VERSION 3.1.0 MODULES optflow ) ADD_MODULE_USE_OPENCV( OpticalFlowFarneback "OpticalFlow" VERSION 3.1.0 MODULES video optflow ) ADD_MODULE_USE_OPENCV( OpticalDeepFlow "OpticalFlow" VERSION 3.1.0 MODULES video optflow ) - -ADD_MODULE_USE_OPENCV( DisparityStereoBM "DisparityStereoBM" VERSION 2.4.13.0 MODULES calib3d ) -ADD_MODULE_USE_OPENCV( DisparityStereoSGBM "DisparityStereoSGBM" VERSION 2.4.13.0 MODULES calib3d ) - +ADD_MODULE_USE_OPENCV( SaliencyDetectionSpectral "SaliencyDetection" VERSION 3.1.0 MODULES saliency ) +ADD_MODULE_USE_OPENCV( SaliencyDetectionFineGrained "SaliencyDetection" VERSION 3.1.0 MODULES saliency ) +# +# Create header and auxiliar files +# CREATE_MODULE_MACROS() INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} ) ADD_LIBRARY( PlaYUVerModules ${plaYUver_Mod_SRCS} ) diff --git a/modules/SaliencyDetection.cpp b/modules/SaliencyDetection.cpp new file mode 100644 index 0000000..88ffaf3 --- /dev/null +++ b/modules/SaliencyDetection.cpp @@ -0,0 +1,137 @@ +/* This file is a part of PlaYUVer project + * Copyright (C) 2014-2018 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 SaliencyDetection.cpp + * \brief Modules to measure optical flow + */ + +#include "SaliencyDetection.h" + +#include + +//#include + +using cv::Mat; +using cv::Ptr; +using cv::saliency::Saliency; + +SaliencyDetectionModule::SaliencyDetectionModule() +{ + /* Module Definition */ + m_iModuleAPI = MODULE_API_2; + m_iModuleType = FRAME_PROCESSING_MODULE; + m_pchModuleCategory = "Saliency"; + m_uiNumberOfFrames = MODULE_REQUIRES_ONE_FRAME; + m_uiModuleRequirements = MODULE_REQUIRES_NEW_WINDOW; + + m_pcSaliencyFrame = NULL; +} + +Bool SaliencyDetectionModule::commonCreate( std::vector apcFrameList ) +{ + _BASIC_MODULE_API_2_CHECK_ + + m_pcSaliencyFrame = new PlaYUVerFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), PlaYUVerFrame::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; +} + +SaliencyDetectionSpectral::SaliencyDetectionSpectral() +{ + /* Module Definition */ + m_pchModuleName = "SaliencySpectralResidual"; + m_pchModuleLongName = "Spectral residual"; + m_pchModuleTooltip = "Measure saliency using spectral residual method"; + + m_uiModuleRequirements = m_uiModuleRequirements | MODULE_REQUIRES_OPTIONS; + + m_cModuleOptions.addOptions() /**/ + ( "Binary map", m_bBinaryMap, "Measure a binary saliency map [false]" ); + + m_bBinaryMap = false; +} + +Bool SaliencyDetectionSpectral::create( std::vector apcFrameList ) +{ + Bool bRet = commonCreate( apcFrameList ); + if( !bRet ) + return bRet; + + m_ptrSaliencyAlgorithm = cv::saliency::StaticSaliencySpectralResidual::create(); + + return true; +} + +PlaYUVerFrame* SaliencyDetectionSpectral::process( std::vector apcFrameList ) +{ + if( commonProcess( apcFrameList ) ) + { + Mat matBinaryMap; + cv::saliency::StaticSaliencySpectralResidual spec; + spec.computeBinaryMap( m_matSaliency, matBinaryMap ); + + if( m_bBinaryMap ) + m_pcSaliencyFrame->fromMat( matBinaryMap ); + else + m_pcSaliencyFrame->fromMat( m_matSaliency ); + } + return m_pcSaliencyFrame; +} + +SaliencyDetectionFineGrained::SaliencyDetectionFineGrained() +{ + /* Module Definition */ + m_pchModuleName = "SaliencyDetectionFineGrained"; + m_pchModuleLongName = "Fine grained"; + m_pchModuleTooltip = "Measure saliency using fine grained method"; +} + +Bool SaliencyDetectionFineGrained::create( std::vector apcFrameList ) +{ + Bool bRet = commonCreate( apcFrameList ); + if( !bRet ) + return bRet; + m_ptrSaliencyAlgorithm = cv::saliency::StaticSaliencyFineGrained::create(); + return true; +} + +PlaYUVerFrame* SaliencyDetectionFineGrained::process( std::vector apcFrameList ) +{ + if( commonProcess( apcFrameList ) ) + m_pcSaliencyFrame->fromMat( m_matSaliency ); + return m_pcSaliencyFrame; +} diff --git a/modules/SaliencyDetection.h b/modules/SaliencyDetection.h new file mode 100644 index 0000000..1624c23 --- /dev/null +++ b/modules/SaliencyDetection.h @@ -0,0 +1,75 @@ +/* This file is a part of PlaYUVer project + * Copyright (C) 2014-2018 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 SaliencyDetection.h + * \brief Modules to detect saliency in image and videos + */ + +#ifndef __SALIENCYDETECTION_H__ +#define __SALIENCYDETECTION_H__ + +// OpenCV +#include +#include + +// PlaYUVerLib +#include "lib/PlaYUVerModuleIf.h" + +class SaliencyDetectionModule : public PlaYUVerModuleIf +{ +protected: + PlaYUVerFrame* m_pcSaliencyFrame; + + cv::Ptr m_ptrSaliencyAlgorithm; + cv::Mat m_matSaliency; + + Bool commonCreate( std::vector apcFrameList ); + Bool commonProcess( std::vector apcFrameList ); + +public: + SaliencyDetectionModule(); + virtual ~SaliencyDetectionModule() {} + Void destroy(); +}; + +class SaliencyDetectionSpectral : public SaliencyDetectionModule +{ + REGISTER_CLASS_FACTORY( SaliencyDetectionSpectral ) +private: + Bool m_bBinaryMap; + +public: + SaliencyDetectionSpectral(); + virtual ~SaliencyDetectionSpectral() {} + Bool create( std::vector apcFrameList ); + PlaYUVerFrame* process( std::vector apcFrameList ); +}; + +class SaliencyDetectionFineGrained : public SaliencyDetectionModule +{ + REGISTER_CLASS_FACTORY( SaliencyDetectionFineGrained ) +public: + SaliencyDetectionFineGrained(); + virtual ~SaliencyDetectionFineGrained() {} + Bool create( std::vector apcFrameList ); + PlaYUVerFrame* process( std::vector apcFrameList ); +}; + +#endif // __SALIENCYDETECTION_H__