2010-05-11 19:44:00 +02:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T, typename ST, typename QT>
|
2011-04-18 17:14:32 +02:00
|
|
|
void integral_( const T* src, size_t _srcstep, ST* sum, size_t _sumstep,
|
|
|
|
QT* sqsum, size_t _sqsumstep, ST* tilted, size_t _tiltedstep,
|
2011-04-17 15:14:45 +02:00
|
|
|
Size size, int cn )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int x, y, k;
|
|
|
|
|
2011-04-18 17:14:32 +02:00
|
|
|
int srcstep = (int)(_srcstep/sizeof(T));
|
|
|
|
int sumstep = (int)(_sumstep/sizeof(ST));
|
|
|
|
int tiltedstep = (int)(_tiltedstep/sizeof(ST));
|
|
|
|
int sqsumstep = (int)(_sqsumstep/sizeof(QT));
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
size.width *= cn;
|
|
|
|
|
|
|
|
memset( sum, 0, (size.width+cn)*sizeof(sum[0]));
|
|
|
|
sum += sumstep + cn;
|
|
|
|
|
|
|
|
if( sqsum )
|
|
|
|
{
|
|
|
|
memset( sqsum, 0, (size.width+cn)*sizeof(sqsum[0]));
|
|
|
|
sqsum += sqsumstep + cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tilted )
|
|
|
|
{
|
|
|
|
memset( tilted, 0, (size.width+cn)*sizeof(tilted[0]));
|
|
|
|
tilted += tiltedstep + cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( sqsum == 0 && tilted == 0 )
|
|
|
|
{
|
|
|
|
for( y = 0; y < size.height; y++, src += srcstep - cn, sum += sumstep - cn )
|
|
|
|
{
|
|
|
|
for( k = 0; k < cn; k++, src++, sum++ )
|
|
|
|
{
|
|
|
|
ST s = sum[-cn] = 0;
|
|
|
|
for( x = 0; x < size.width; x += cn )
|
|
|
|
{
|
|
|
|
s += src[x];
|
|
|
|
sum[x] = sum[x - sumstep] + s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( tilted == 0 )
|
|
|
|
{
|
|
|
|
for( y = 0; y < size.height; y++, src += srcstep - cn,
|
|
|
|
sum += sumstep - cn, sqsum += sqsumstep - cn )
|
|
|
|
{
|
|
|
|
for( k = 0; k < cn; k++, src++, sum++, sqsum++ )
|
|
|
|
{
|
|
|
|
ST s = sum[-cn] = 0;
|
|
|
|
QT sq = sqsum[-cn] = 0;
|
|
|
|
for( x = 0; x < size.width; x += cn )
|
|
|
|
{
|
|
|
|
T it = src[x];
|
|
|
|
s += it;
|
2011-04-17 15:14:45 +02:00
|
|
|
sq += (QT)it*it;
|
2010-05-11 19:44:00 +02:00
|
|
|
ST t = sum[x - sumstep] + s;
|
|
|
|
QT tq = sqsum[x - sqsumstep] + sq;
|
|
|
|
sum[x] = t;
|
|
|
|
sqsum[x] = tq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AutoBuffer<ST> _buf(size.width+cn);
|
|
|
|
ST* buf = _buf;
|
|
|
|
ST s;
|
|
|
|
QT sq;
|
2011-04-17 15:14:45 +02:00
|
|
|
for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
sum[-cn] = tilted[-cn] = 0;
|
|
|
|
|
|
|
|
for( x = 0, s = 0, sq = 0; x < size.width; x += cn )
|
|
|
|
{
|
|
|
|
T it = src[x];
|
|
|
|
buf[x] = tilted[x] = it;
|
|
|
|
s += it;
|
2011-04-17 15:14:45 +02:00
|
|
|
sq += (QT)it*it;
|
2010-05-11 19:44:00 +02:00
|
|
|
sum[x] = s;
|
2011-04-17 15:14:45 +02:00
|
|
|
if( sqsum )
|
|
|
|
sqsum[x] = sq;
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( size.width == cn )
|
|
|
|
buf[cn] = 0;
|
2011-04-17 15:14:45 +02:00
|
|
|
|
|
|
|
if( sqsum )
|
|
|
|
{
|
|
|
|
sqsum[-cn] = 0;
|
|
|
|
sqsum++;
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for( y = 1; y < size.height; y++ )
|
|
|
|
{
|
|
|
|
src += srcstep - cn;
|
|
|
|
sum += sumstep - cn;
|
|
|
|
tilted += tiltedstep - cn;
|
|
|
|
buf += -cn;
|
2011-04-17 15:14:45 +02:00
|
|
|
|
|
|
|
if( sqsum )
|
|
|
|
sqsum += sqsumstep - cn;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-04-17 15:14:45 +02:00
|
|
|
for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
T it = src[0];
|
|
|
|
ST t0 = s = it;
|
2011-04-17 15:14:45 +02:00
|
|
|
QT tq0 = sq = (QT)it*it;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
sum[-cn] = 0;
|
2011-04-17 15:14:45 +02:00
|
|
|
if( sqsum )
|
|
|
|
sqsum[-cn] = 0;
|
2010-05-11 19:44:00 +02:00
|
|
|
tilted[-cn] = tilted[-tiltedstep];
|
|
|
|
|
|
|
|
sum[0] = sum[-sumstep] + t0;
|
2011-04-17 15:14:45 +02:00
|
|
|
if( sqsum )
|
|
|
|
sqsum[0] = sqsum[-sqsumstep] + tq0;
|
2010-05-11 19:44:00 +02:00
|
|
|
tilted[0] = tilted[-tiltedstep] + t0 + buf[cn];
|
|
|
|
|
|
|
|
for( x = cn; x < size.width - cn; x += cn )
|
|
|
|
{
|
|
|
|
ST t1 = buf[x];
|
|
|
|
buf[x - cn] = t1 + t0;
|
|
|
|
t0 = it = src[x];
|
2011-04-17 15:14:45 +02:00
|
|
|
tq0 = (QT)it*it;
|
2010-05-11 19:44:00 +02:00
|
|
|
s += t0;
|
|
|
|
sq += tq0;
|
|
|
|
sum[x] = sum[x - sumstep] + s;
|
2011-04-17 15:14:45 +02:00
|
|
|
if( sqsum )
|
|
|
|
sqsum[x] = sqsum[x - sqsumstep] + sq;
|
2010-05-11 19:44:00 +02:00
|
|
|
t1 += buf[x + cn] + t0 + tilted[x - tiltedstep - cn];
|
|
|
|
tilted[x] = t1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( size.width > cn )
|
|
|
|
{
|
|
|
|
ST t1 = buf[x];
|
|
|
|
buf[x - cn] = t1 + t0;
|
|
|
|
t0 = it = src[x];
|
2011-04-17 15:14:45 +02:00
|
|
|
tq0 = (QT)it*it;
|
2010-05-11 19:44:00 +02:00
|
|
|
s += t0;
|
|
|
|
sq += tq0;
|
|
|
|
sum[x] = sum[x - sumstep] + s;
|
2011-04-17 15:14:45 +02:00
|
|
|
if( sqsum )
|
|
|
|
sqsum[x] = sqsum[x - sqsumstep] + sq;
|
2010-05-11 19:44:00 +02:00
|
|
|
tilted[x] = t0 + t1 + tilted[x - tiltedstep - cn];
|
|
|
|
buf[x] = t0;
|
|
|
|
}
|
2011-04-17 15:14:45 +02:00
|
|
|
|
|
|
|
if( sqsum )
|
|
|
|
sqsum++;
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-17 15:14:45 +02:00
|
|
|
|
|
|
|
#define DEF_INTEGRAL_FUNC(suffix, T, ST, QT) \
|
|
|
|
void integral_##suffix( T* src, size_t srcstep, ST* sum, size_t sumstep, QT* sqsum, size_t sqsumstep, \
|
|
|
|
ST* tilted, size_t tiltedstep, Size size, int cn ) \
|
|
|
|
{ integral_(src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tiltedstep, size, cn); }
|
|
|
|
|
|
|
|
DEF_INTEGRAL_FUNC(8u32s, uchar, int, double)
|
|
|
|
DEF_INTEGRAL_FUNC(8u32f, uchar, float, double)
|
|
|
|
DEF_INTEGRAL_FUNC(8u64f, uchar, double, double)
|
2011-04-29 13:55:33 +02:00
|
|
|
DEF_INTEGRAL_FUNC(32f, float, float, double)
|
2011-04-17 15:14:45 +02:00
|
|
|
DEF_INTEGRAL_FUNC(32f64f, float, double, double)
|
|
|
|
DEF_INTEGRAL_FUNC(64f, double, double, double)
|
|
|
|
|
|
|
|
typedef void (*IntegralFunc)(const uchar* src, size_t srcstep, uchar* sum, size_t sumstep,
|
|
|
|
uchar* sqsum, size_t sqsumstep, uchar* tilted, size_t tstep,
|
|
|
|
Size size, int cn );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-06-06 16:51:27 +02:00
|
|
|
void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, OutputArray _tilted, int sdepth )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-04-17 15:14:45 +02:00
|
|
|
Mat src = _src.getMat(), sum, sqsum, tilted;
|
2010-05-11 19:44:00 +02:00
|
|
|
int depth = src.depth(), cn = src.channels();
|
|
|
|
Size isize(src.cols + 1, src.rows+1);
|
|
|
|
|
|
|
|
if( sdepth <= 0 )
|
|
|
|
sdepth = depth == CV_8U ? CV_32S : CV_64F;
|
|
|
|
sdepth = CV_MAT_DEPTH(sdepth);
|
2011-04-17 15:14:45 +02:00
|
|
|
_sum.create( isize, CV_MAKETYPE(sdepth, cn) );
|
|
|
|
sum = _sum.getMat();
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-04-17 15:14:45 +02:00
|
|
|
if( _tilted.needed() )
|
|
|
|
{
|
|
|
|
_tilted.create( isize, CV_MAKETYPE(sdepth, cn) );
|
|
|
|
tilted = _tilted.getMat();
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-04-17 15:14:45 +02:00
|
|
|
if( _sqsum.needed() )
|
|
|
|
{
|
|
|
|
_sqsum.create( isize, CV_MAKETYPE(CV_64F, cn) );
|
|
|
|
sqsum = _sqsum.getMat();
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
IntegralFunc func = 0;
|
|
|
|
|
|
|
|
if( depth == CV_8U && sdepth == CV_32S )
|
2011-12-27 12:21:45 +01:00
|
|
|
func = (IntegralFunc)GET_OPTIMIZED(integral_8u32s);
|
2010-05-11 19:44:00 +02:00
|
|
|
else if( depth == CV_8U && sdepth == CV_32F )
|
2011-04-17 15:14:45 +02:00
|
|
|
func = (IntegralFunc)integral_8u32f;
|
2010-05-11 19:44:00 +02:00
|
|
|
else if( depth == CV_8U && sdepth == CV_64F )
|
2011-04-17 15:14:45 +02:00
|
|
|
func = (IntegralFunc)integral_8u64f;
|
2011-03-17 18:05:31 +01:00
|
|
|
else if( depth == CV_32F && sdepth == CV_32F )
|
2011-04-17 15:14:45 +02:00
|
|
|
func = (IntegralFunc)integral_32f;
|
2010-05-11 19:44:00 +02:00
|
|
|
else if( depth == CV_32F && sdepth == CV_64F )
|
2011-04-17 15:14:45 +02:00
|
|
|
func = (IntegralFunc)integral_32f64f;
|
2010-05-11 19:44:00 +02:00
|
|
|
else if( depth == CV_64F && sdepth == CV_64F )
|
2011-04-17 15:14:45 +02:00
|
|
|
func = (IntegralFunc)integral_64f;
|
2010-05-11 19:44:00 +02:00
|
|
|
else
|
|
|
|
CV_Error( CV_StsUnsupportedFormat, "" );
|
|
|
|
|
2011-04-17 15:14:45 +02:00
|
|
|
func( src.data, src.step, sum.data, sum.step, sqsum.data, sqsum.step,
|
|
|
|
tilted.data, tilted.step, src.size(), cn );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
2011-04-17 15:14:45 +02:00
|
|
|
|
2011-06-06 16:51:27 +02:00
|
|
|
void cv::integral( InputArray src, OutputArray sum, int sdepth )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-06-08 08:55:04 +02:00
|
|
|
integral( src, sum, noArray(), noArray(), sdepth );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 16:51:27 +02:00
|
|
|
void cv::integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-06-08 08:55:04 +02:00
|
|
|
integral( src, sum, sqsum, noArray(), sdepth );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvIntegral( const CvArr* image, CvArr* sumImage,
|
|
|
|
CvArr* sumSqImage, CvArr* tiltedSumImage )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(image), sum = cv::cvarrToMat(sumImage), sum0 = sum;
|
|
|
|
cv::Mat sqsum0, sqsum, tilted0, tilted;
|
|
|
|
cv::Mat *psqsum = 0, *ptilted = 0;
|
|
|
|
|
|
|
|
if( sumSqImage )
|
|
|
|
{
|
|
|
|
sqsum0 = sqsum = cv::cvarrToMat(sumSqImage);
|
|
|
|
psqsum = &sqsum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tiltedSumImage )
|
|
|
|
{
|
|
|
|
tilted0 = tilted = cv::cvarrToMat(tiltedSumImage);
|
|
|
|
ptilted = &tilted;
|
|
|
|
}
|
2011-06-06 16:51:27 +02:00
|
|
|
cv::integral( src, sum, psqsum ? cv::_OutputArray(*psqsum) : cv::_OutputArray(),
|
|
|
|
ptilted ? cv::_OutputArray(*ptilted) : cv::_OutputArray(), sum.depth() );
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
CV_Assert( sum.data == sum0.data && sqsum.data == sqsum0.data && tilted.data == tilted0.data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|