/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008-2011, William Lucas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions 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.
* * Neither the name of the Willow Garage nor the names of its
* contributors may 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
* COPYRIGHT OWNER 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.
*********************************************************************/
#include "precomp.hpp"
#include
namespace cv
{
static void magSpectrums( InputArray _src, OutputArray _dst)
{
Mat src = _src.getMat();
int depth = src.depth(), cn = src.channels(), type = src.type();
int rows = src.rows, cols = src.cols;
int j, k;
CV_Assert( type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2 );
if(src.depth() == CV_32F)
_dst.create( src.rows, src.cols, CV_32FC1 );
else
_dst.create( src.rows, src.cols, CV_64FC1 );
Mat dst = _dst.getMat();
dst.setTo(0);//Mat elements are not equal to zero by default!
bool is_1d = (rows == 1 || (cols == 1 && src.isContinuous() && dst.isContinuous()));
if( is_1d )
cols = cols + rows - 1, rows = 1;
int ncols = cols*cn;
int j0 = cn == 1;
int j1 = ncols - (cols % 2 == 0 && cn == 1);
if( depth == CV_32F )
{
const float* dataSrc = src.ptr();
float* dataDst = dst.ptr();
size_t stepSrc = src.step/sizeof(dataSrc[0]);
size_t stepDst = dst.step/sizeof(dataDst[0]);
if( !is_1d && cn == 1 )
{
for( k = 0; k < (cols % 2 ? 1 : 2); k++ )
{
if( k == 1 )
dataSrc += cols - 1, dataDst += cols - 1;
dataDst[0] = dataSrc[0]*dataSrc[0];
if( rows % 2 == 0 )
dataDst[(rows-1)*stepDst] = dataSrc[(rows-1)*stepSrc]*dataSrc[(rows-1)*stepSrc];
for( j = 1; j 0 && out.rows % 2 == 1);
xMid = xMid + yMid;
for(size_t i = 0; i < planes.size(); i++)
{
Mat tmp;
Mat half0(planes[i], Rect(0, 0, xMid + is_odd, 1));
Mat half1(planes[i], Rect(xMid + is_odd, 0, xMid, 1));
half0.copyTo(tmp);
half1.copyTo(planes[i](Rect(0, 0, xMid, 1)));
tmp.copyTo(planes[i](Rect(xMid, 0, xMid + is_odd, 1)));
}
}
else
{
int isXodd = out.cols % 2 == 1;
int isYodd = out.rows % 2 == 1;
for(size_t i = 0; i < planes.size(); i++)
{
// perform quadrant swaps...
Mat q0(planes[i], Rect(0, 0, xMid + isXodd, yMid + isYodd));
Mat q1(planes[i], Rect(xMid + isXodd, 0, xMid, yMid + isYodd));
Mat q2(planes[i], Rect(0, yMid + isYodd, xMid + isXodd, yMid));
Mat q3(planes[i], Rect(xMid + isXodd, yMid + isYodd, xMid, yMid));
if(!(isXodd || isYodd))
{
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
else
{
Mat tmp0, tmp1, tmp2 ,tmp3;
q0.copyTo(tmp0);
q1.copyTo(tmp1);
q2.copyTo(tmp2);
q3.copyTo(tmp3);
tmp0.copyTo(planes[i](Rect(xMid, yMid, xMid + isXodd, yMid + isYodd)));
tmp3.copyTo(planes[i](Rect(0, 0, xMid, yMid)));
tmp1.copyTo(planes[i](Rect(0, yMid, xMid, yMid + isYodd)));
tmp2.copyTo(planes[i](Rect(xMid, 0, xMid + isXodd, yMid)));
}
}
}
merge(planes, out);
}
static Point2d weightedCentroid(InputArray _src, cv::Point peakLocation, cv::Size weightBoxSize, double* response)
{
Mat src = _src.getMat();
int type = src.type();
CV_Assert( type == CV_32FC1 || type == CV_64FC1 );
int minr = peakLocation.y - (weightBoxSize.height >> 1);
int maxr = peakLocation.y + (weightBoxSize.height >> 1);
int minc = peakLocation.x - (weightBoxSize.width >> 1);
int maxc = peakLocation.x + (weightBoxSize.width >> 1);
Point2d centroid;
double sumIntensity = 0.0;
// clamp the values to min and max if needed.
if(minr < 0)
{
minr = 0;
}
if(minc < 0)
{
minc = 0;
}
if(maxr > src.rows - 1)
{
maxr = src.rows - 1;
}
if(maxc > src.cols - 1)
{
maxc = src.cols - 1;
}
if(type == CV_32FC1)
{
const float* dataIn = src.ptr();
dataIn += minr*src.cols;
for(int y = minr; y