From 432d2c6cadcf37783fb9ede73ef33f325a82d993 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Tue, 12 Mar 2019 02:05:49 +0000 Subject: [PATCH] Add module to convert ERP to NxN --- modules/CMakeLists.txt | 2 +- modules/ThreeSixtyDownsampling.cpp | 123 +++++++++++++++++++++++++++++ modules/ThreeSixtyDownsampling.h | 46 +++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 modules/ThreeSixtyDownsampling.cpp create mode 100644 modules/ThreeSixtyDownsampling.h diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index f3710e5..d3b61a9 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -33,7 +33,6 @@ ADD_MODULE( HEVCIntraPrediction "HEVCIntraPrediction" ) ADD_MODULE( ThreeSixtySpatialtoTemporal "ThreeSixtySpatialtoTemporal" ) ADD_MODULE( ThreeSixtyTemporaltoSpatial "ThreeSixtyTemporaltoSpatial" ) - 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 ) @@ -45,6 +44,7 @@ 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( ThreeSixtyDownsampling "ThreeSixtyDownsampling" VERSION 3.1.0 MODULES ) # # Create header and auxiliar files diff --git a/modules/ThreeSixtyDownsampling.cpp b/modules/ThreeSixtyDownsampling.cpp new file mode 100644 index 0000000..098e76b --- /dev/null +++ b/modules/ThreeSixtyDownsampling.cpp @@ -0,0 +1,123 @@ +/* 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 ThreeSixtyDownsampling.cpp + * \brief Downsampling ERP to NxN + */ + +#include "ThreeSixtyDownsampling.h" + +#include +#include + +using cv::InputArray; +using cv::Mat; +using cv::OutputArray; +using cv::Size; + +ThreeSixtyDownsampling::ThreeSixtyDownsampling() +{ + /* Module Definition */ + m_iModuleAPI = CLP_MODULE_API_2; + m_iModuleType = CLP_FRAME_PROCESSING_MODULE; + m_pchModuleCategory = "360Video"; + m_pchModuleName = "ThreeSixtyDownsampling"; + m_pchModuleLongName = "ERP to NxN"; + m_pchModuleTooltip = "Downsampling ERP to NxN"; + m_uiNumberOfFrames = 1; + m_uiModuleRequirements = CLP_MODULE_REQUIRES_SKIP_WHILE_PLAY | CLP_MODULE_REQUIRES_OPTIONS; + + m_cModuleOptions.addOptions() /**/ + ( "Downsampling", m_bDownsampling, "ERP to NxN [true]" ); + + m_pcOutputFrame = NULL; + m_bDownsampling = true; +} + +bool ThreeSixtyDownsampling::create( std::vector apcFrameList ) +{ + _BASIC_MODULE_API_2_CHECK_ + m_pcOutputFrame = new CalypFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), CLP_GRAY ); + m_isOdd = !( m_pcOutputFrame->getHeight() % 2 ); + return true; +} + +CalypFrame* ThreeSixtyDownsampling::process( std::vector apcFrameList ) +{ + Mat cvIn; + apcFrameList[0]->toMat( cvIn, true ); + m_pcOutputFrame->reset(); + + memcpy( &m_pcOutputFrame->getPelBufferYUV()[0][m_pcOutputFrame->getHeight() / 2][0], + &apcFrameList[0]->getPelBufferYUV()[0][m_pcOutputFrame->getHeight() / 2][0], + m_pcOutputFrame->getWidth() * sizeof( ClpPel ) ); + if( m_isOdd ) + { + memcpy( &m_pcOutputFrame->getPelBufferYUV()[0][m_pcOutputFrame->getHeight() / 2 - 1][0], + &apcFrameList[0]->getPelBufferYUV()[0][m_pcOutputFrame->getHeight() / 2 - 1][0], + m_pcOutputFrame->getWidth() * sizeof( ClpPel ) ); + } + + for( int line = 1; line < m_pcOutputFrame->getHeight() / 2; line++ ) + { + for( int hemisfer = -1; hemisfer < 2; hemisfer += 2 ) + { + Mat cvOutput; + + // calculate destination sizes + unsigned int ypos = m_pcOutputFrame->getHeight() / 2 + line * hemisfer + ( hemisfer == -1 ? ( m_isOdd ? -1 : 0 ) : 0 ); + unsigned int lineWidth = ( m_pcOutputFrame->getHeight() / 2 - line ) * 4 + 2; + unsigned int xpos = ( m_pcOutputFrame->getWidth() - lineWidth ) / 2; + + Mat cvDownsample( 1, lineWidth, CV_8UC1 ); + Mat cvUpsample( 1, m_pcOutputFrame->getWidth(), CV_8UC1 ); + + // resize + if( m_bDownsampling ) + { + cv::resize( cvIn.row( ypos ), cvDownsample, cvDownsample.size() ); + cvOutput = cvDownsample; + } + else + { + Mat cvauxin = cvIn.row( ypos ).colRange( xpos, xpos + lineWidth ); + cvauxin.copyTo( cvDownsample ); + cv::resize( cvDownsample, cvUpsample, cvUpsample.size() ); + cvOutput = cvUpsample; + } + + // copy to output + ClpPel* outpel = &m_pcOutputFrame->getPelBufferYUV()[0][ypos][xpos * m_bDownsampling]; + unsigned char* pchar = cvOutput.data; + for( unsigned int i = 0; i < cvOutput.cols; i++ ) + { + *outpel++ = *pchar++; + } + } + } + return m_pcOutputFrame; +} + +void ThreeSixtyDownsampling::destroy() +{ + if( m_pcOutputFrame ) + delete m_pcOutputFrame; + m_pcOutputFrame = NULL; +} diff --git a/modules/ThreeSixtyDownsampling.h b/modules/ThreeSixtyDownsampling.h new file mode 100644 index 0000000..0319f39 --- /dev/null +++ b/modules/ThreeSixtyDownsampling.h @@ -0,0 +1,46 @@ +/* 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 ThreeSixtyDownsampling.h + * \brief Downsampling ERP to NxN + */ + +#ifndef __THREESIXTYDOWNSAMPLING_H__ +#define __THREESIXTYDOWNSAMPLING_H__ + +// CalypLib +#include "lib/CalypModuleIf.h" + +class ThreeSixtyDownsampling : public CalypModuleIf +{ + REGISTER_CLASS_FACTORY( ThreeSixtyDownsampling ) +private: + bool m_bDownsampling; + bool m_isOdd; + +public: + ThreeSixtyDownsampling(); + virtual ~ThreeSixtyDownsampling() {} + bool create( std::vector apcFrameList ); + CalypFrame* process( std::vector apcFrameList ); + void destroy(); +}; + +#endif // __THREESIXTYDOWNSAMPLING_H__