Files
calyp/modules/InterFrameVariance.cpp
T
jfmcarreira 93cf60e34a Improvements on CalypModuleIf
Add type safe flags
Replace MACRO with templates (WIP)
2021-10-19 23:42:18 +01:00

111 lines
4.0 KiB
C++

/* This file is a part of Calyp project
* Copyright (C) 2014-2021 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 InterFrameVariance.cpp
* \brief Variance across several frames
*/
#include "InterFrameVariance.h"
InterFrameVariance::InterFrameVariance()
{
/* Module Definition */
m_iModuleAPI = CLP_MODULE_API_2; // Use API version 2 (recommended).
// See this example for details on the functions prototype
m_iModuleType = ClpModuleType::FrameProcessing; // Apply module to the frames or to
// the whole sequence.
m_pchModuleCategory = "Measurements"; // Category (sub-menu)
m_pchModuleName = "InterFrameVariance"; // Name
m_pchModuleLongName = "Variance across frames";
m_pchModuleTooltip = "Measure the variance across several frames";
m_uiNumberOfFrames = 2; // Number of Frames required (This module
// allows a variable number of inputs)
m_uiModuleRequirements = ClpModuleFeature::NewWindow | ClpModuleFeature::VariableNumOfFrames;
// Several requirements should be "or" between each others.
m_pcFrameVariance = NULL;
}
bool InterFrameVariance::create( std::vector<CalypFrame*> apcFrameList )
{
_BASIC_MODULE_API_2_CHECK_
for( unsigned int i = 1; i < apcFrameList.size(); i++ )
if( !apcFrameList[i]->haveSameFmt( apcFrameList[0], CalypFrame::MATCH_COLOR_SPACE |
CalypFrame::MATCH_RESOLUTION |
CalypFrame::MATCH_BITS ) )
return false;
m_pcFrameVariance =
new CalypFrame( apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight(), CLP_GRAY, 8 );
m_pVariance = CalypPlane<double>{ apcFrameList[0]->getWidth(), apcFrameList[0]->getHeight() };
return true;
}
CalypFrame* InterFrameVariance::process( std::vector<CalypFrame*> apcFrameList )
{
int numFrames = apcFrameList.size();
ClpPel** pInput = new ClpPel*[numFrames];
ClpPel* pOutputPelYUV = m_pcFrameVariance->getPelBufferYUV()[0][0];
for( int i = 0; i < numFrames; i++ )
{
pInput[i] = apcFrameList[i]->getPelBufferYUV()[0][0];
}
double maxVariance = 0;
for( unsigned int y = 0; y < m_pcFrameVariance->getHeight(); y++ )
for( unsigned int x = 0; x < m_pcFrameVariance->getWidth(); x++ )
{
int sum = 0;
int v = 0;
m_pVariance[y][x] = 0;
for( int i = 0; i < numFrames; i++ )
{
v = *pInput[i]++;
sum += v;
m_pVariance[y][x] += v * v;
}
// Final result of the variance for one block
m_pVariance[y][x] -= double( sum * sum ) / double( numFrames );
m_pVariance[y][x] /= double( numFrames );
if( m_pVariance[y][x] > maxVariance )
maxVariance = m_pVariance[y][x];
}
for( unsigned int y = 0; y < m_pcFrameVariance->getHeight(); y++ )
for( unsigned int x = 0; x < m_pcFrameVariance->getWidth(); x++ )
{
*pOutputPelYUV++ = m_pVariance[y][x] * 255 / maxVariance;
}
delete[] pInput;
return m_pcFrameVariance;
}
void InterFrameVariance::destroy()
{
if( m_pcFrameVariance )
delete m_pcFrameVariance;
m_pcFrameVariance = NULL;
}