2010-05-12 01:44:00 +08: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.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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"
|
2014-01-14 18:45:01 +08:00
|
|
|
#include "opencl_kernels.hpp"
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
////////////////////////////////////////////////// matchTemplate //////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-14 18:45:01 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
enum
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
2014-02-22 04:43:03 +08:00
|
|
|
SUM_1 = 0, SUM_2 = 1
|
|
|
|
};
|
|
|
|
|
2014-02-24 22:38:33 +08:00
|
|
|
static bool sumTemplate(InputArray _src, UMat & result)
|
2014-02-22 04:43:03 +08:00
|
|
|
{
|
2014-02-24 22:38:33 +08:00
|
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2014-02-22 04:43:03 +08:00
|
|
|
int wdepth = std::max(CV_32S, depth), wtype = CV_MAKE_TYPE(wdepth, cn);
|
2014-02-24 22:38:33 +08:00
|
|
|
size_t wgs = ocl::Device::getDefault().maxWorkGroupSize();
|
|
|
|
|
|
|
|
int wgs2_aligned = 1;
|
|
|
|
while (wgs2_aligned < (int)wgs)
|
|
|
|
wgs2_aligned <<= 1;
|
|
|
|
wgs2_aligned >>= 1;
|
2014-02-22 04:43:03 +08:00
|
|
|
|
|
|
|
char cvt[40];
|
|
|
|
ocl::Kernel k("calcSum", ocl::imgproc::match_template_oclsrc,
|
2014-02-24 22:38:33 +08:00
|
|
|
format("-D CALC_SUM -D T=%s -D WT=%s -D cn=%d -D convertToWT=%s -D WGS=%d -D WGS2_ALIGNED=%d -D wdepth=%d",
|
|
|
|
ocl::typeToStr(type), ocl::typeToStr(wtype), cn,
|
|
|
|
ocl::convertTypeStr(depth, wdepth, cn, cvt),
|
|
|
|
(int)wgs, wgs2_aligned, wdepth));
|
2014-02-22 04:43:03 +08:00
|
|
|
if (k.empty())
|
2014-01-27 17:25:21 +08:00
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-24 22:38:33 +08:00
|
|
|
UMat src = _src.getUMat();
|
2014-02-22 04:43:03 +08:00
|
|
|
result.create(1, 1, CV_32FC1);
|
|
|
|
|
2014-02-24 22:38:33 +08:00
|
|
|
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
|
|
|
|
resarg = ocl::KernelArg::PtrWriteOnly(result);
|
|
|
|
|
|
|
|
k.args(srcarg, src.cols, (int)src.total(), resarg);
|
2014-02-22 04:43:03 +08:00
|
|
|
|
2014-02-24 22:38:33 +08:00
|
|
|
size_t globalsize = wgs;
|
|
|
|
return k.run(1, &globalsize, &wgs, false);
|
2014-02-22 04:43:03 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
static bool matchTemplateNaive_CCORR(InputArray _image, InputArray _templ, OutputArray _result)
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2014-02-22 04:43:03 +08:00
|
|
|
int wdepth = std::max(depth, CV_32S), wtype = CV_MAKE_TYPE(wdepth, cn);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
char cvt[40];
|
2014-02-02 03:34:54 +08:00
|
|
|
ocl::Kernel k("matchTemplate_Naive_CCORR", ocl::imgproc::match_template_oclsrc,
|
2014-02-22 04:43:03 +08:00
|
|
|
format("-D CCORR -D T=%s -D WT=%s -D convertToWT=%s -D cn=%d -D wdepth=%d", ocl::typeToStr(type), ocl::typeToStr(wtype),
|
|
|
|
ocl::convertTypeStr(depth, wdepth, cn, cvt), cn, wdepth));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat();
|
2014-02-22 04:43:03 +08:00
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32FC1);
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat result = _result.getUMat();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ),
|
|
|
|
ocl::KernelArg::WriteOnly(result));
|
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
2014-02-22 04:43:03 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
|
|
|
|
{
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
int type = _image.type(), cn = CV_MAT_CN(type);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
ocl::Kernel k("matchTemplate_CCORR_NORMED", ocl::imgproc::match_template_oclsrc,
|
2014-02-22 04:43:03 +08:00
|
|
|
format("-D CCORR_NORMED -D T=%s -D cn=%d", ocl::typeToStr(type), cn));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat();
|
2014-02-22 04:43:03 +08:00
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32FC1);
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat result = _result.getUMat();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
UMat image_sums, image_sqsums;
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F);
|
2014-01-16 18:26:48 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
UMat templ_sqsum;
|
2014-02-24 22:38:33 +08:00
|
|
|
if (!sumTemplate(templ, templ_sqsum))
|
2014-02-22 04:43:03 +08:00
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(result),
|
|
|
|
templ.rows, templ.cols, ocl::KernelArg::PtrReadOnly(templ_sqsum));
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result)
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2014-02-22 04:43:03 +08:00
|
|
|
int wdepth = std::max(depth, CV_32S), wtype = CV_MAKE_TYPE(wdepth, cn);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
char cvt[40];
|
2014-02-02 03:34:54 +08:00
|
|
|
ocl::Kernel k("matchTemplate_Naive_SQDIFF", ocl::imgproc::match_template_oclsrc,
|
2014-02-22 04:43:03 +08:00
|
|
|
format("-D SQDIFF -D T=%s -D WT=%s -D convertToWT=%s -D cn=%d -D wdepth=%d", ocl::typeToStr(type),
|
|
|
|
ocl::typeToStr(wtype), ocl::convertTypeStr(depth, wdepth, cn, cvt), cn, wdepth));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat();
|
2014-01-27 17:25:21 +08:00
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat result = _result.getUMat();
|
2014-01-16 18:26:48 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ),
|
|
|
|
ocl::KernelArg::WriteOnly(result));
|
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
2014-02-22 04:43:03 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
static bool matchTemplate_SQDIFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
int type = _image.type(), cn = CV_MAT_CN(type);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
ocl::Kernel k("matchTemplate_SQDIFF_NORMED", ocl::imgproc::match_template_oclsrc,
|
2014-02-22 04:43:03 +08:00
|
|
|
format("-D SQDIFF_NORMED -D T=%s -D cn=%d", ocl::typeToStr(type), cn));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat();
|
2014-01-27 17:25:21 +08:00
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat result = _result.getUMat();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
UMat image_sums, image_sqsums;
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F);
|
2014-01-16 18:26:48 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
UMat templ_sqsum;
|
2014-02-24 22:38:33 +08:00
|
|
|
if (!sumTemplate(_templ, templ_sqsum))
|
2014-02-22 04:43:03 +08:00
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(result),
|
|
|
|
templ.rows, templ.cols, ocl::KernelArg::PtrReadOnly(templ_sqsum));
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
///////////////////////////////////// CCOEFF /////////////////////////////////////////////////////////////////
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result)
|
|
|
|
{
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-04 14:54:02 +08:00
|
|
|
UMat image_sums, temp;
|
|
|
|
integral(_image, temp);
|
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
if (temp.depth() == CV_64F)
|
2014-02-04 14:54:02 +08:00
|
|
|
temp.convertTo(image_sums, CV_32F);
|
|
|
|
else
|
|
|
|
image_sums = temp;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
ocl::Kernel k("matchTemplate_Prepared_CCOEFF", ocl::imgproc::match_template_oclsrc,
|
|
|
|
format("-D CCOEFF -D T=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat templ = _templ.getUMat();
|
|
|
|
Size size = _image.size(), tsize = templ.size();
|
2014-01-27 17:25:21 +08:00
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F);
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat result = _result.getUMat();
|
2014-01-16 18:26:48 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
if (cn == 1)
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
float templ_sum = static_cast<float>(sum(_templ)[0]) / tsize.area();
|
2014-02-22 04:43:03 +08:00
|
|
|
|
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result),
|
|
|
|
templ.rows, templ.cols, templ_sum);
|
2014-01-14 18:45:01 +08:00
|
|
|
}
|
2014-01-27 17:25:21 +08:00
|
|
|
else
|
2014-01-14 18:45:01 +08:00
|
|
|
{
|
2014-01-27 17:25:21 +08:00
|
|
|
Vec4f templ_sum = Vec4f::all(0);
|
2014-02-02 03:34:54 +08:00
|
|
|
templ_sum = sum(templ) / tsize.area();
|
2014-01-27 17:25:21 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
if (cn == 2)
|
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols,
|
|
|
|
templ_sum[0], templ_sum[1]);
|
|
|
|
else
|
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols,
|
|
|
|
templ_sum[0], templ_sum[1], templ_sum[2], templ_sum[3]);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-02-22 04:43:03 +08:00
|
|
|
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
|
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
UMat temp, image_sums, image_sqsums;
|
2014-02-02 03:34:54 +08:00
|
|
|
integral(_image, image_sums, image_sqsums, CV_32F, CV_32F);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
ocl::Kernel k("matchTemplate_CCOEFF_NORMED", ocl::imgproc::match_template_oclsrc,
|
|
|
|
format("-D CCOEFF_NORMED -D type=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
2014-01-27 17:25:21 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
2014-01-16 18:26:48 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
UMat templ = _templ.getUMat();
|
|
|
|
Size size = _image.size(), tsize = templ.size();
|
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F);
|
|
|
|
UMat result = _result.getUMat();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
float scale = 1.f / tsize.area();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
if (cn == 1)
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
|
|
|
float templ_sum = (float)sum(templ)[0];
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
multiply(templ, templ, temp, 1, CV_32F);
|
2014-01-27 17:25:21 +08:00
|
|
|
float templ_sqsum = (float)sum(temp)[0];
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
templ_sqsum -= scale * templ_sum * templ_sum;
|
|
|
|
templ_sum *= scale;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
if (templ_sqsum < DBL_EPSILON)
|
2014-01-14 18:45:01 +08:00
|
|
|
{
|
2014-01-27 17:25:21 +08:00
|
|
|
result = Scalar::all(1);
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
|
|
|
|
ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
Vec4f templ_sum = Vec4f::all(0), templ_sqsum = Vec4f::all(0);
|
2014-01-27 17:25:21 +08:00
|
|
|
templ_sum = sum(templ);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
multiply(templ, templ, temp, 1, CV_32F);
|
2014-01-27 17:25:21 +08:00
|
|
|
templ_sqsum = sum(temp);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
float templ_sqsum_sum = 0;
|
2014-02-02 03:34:54 +08:00
|
|
|
for (int i = 0; i < cn; i ++)
|
|
|
|
templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i];
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
templ_sum *= scale;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
if (templ_sqsum_sum < DBL_EPSILON)
|
|
|
|
{
|
|
|
|
result = Scalar::all(1);
|
|
|
|
return true;
|
2014-01-14 18:45:01 +08:00
|
|
|
}
|
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
if (cn == 2)
|
2014-02-22 04:43:03 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
|
|
|
|
ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale,
|
|
|
|
templ_sum[0], templ_sum[1], templ_sqsum_sum);
|
|
|
|
else
|
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
|
|
|
|
ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale,
|
|
|
|
templ_sum[0], templ_sum[1], templ_sum[2], templ_sum[3], templ_sqsum_sum);
|
2014-01-14 18:45:01 +08:00
|
|
|
}
|
2014-02-22 04:43:03 +08:00
|
|
|
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2014-01-27 17:25:21 +08:00
|
|
|
}
|
|
|
|
|
2014-01-14 18:45:01 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method)
|
|
|
|
{
|
2014-02-02 03:34:54 +08:00
|
|
|
int cn = _img.channels();
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
if (cn == 3 || cn > 4)
|
|
|
|
return false;
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
typedef bool (*Caller)(InputArray _img, InputArray _templ, OutputArray _result);
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
static const Caller callers[] =
|
2014-01-27 17:25:21 +08:00
|
|
|
{
|
2014-02-22 04:43:03 +08:00
|
|
|
matchTemplateNaive_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplateNaive_CCORR,
|
2014-01-27 17:25:21 +08:00
|
|
|
matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED
|
|
|
|
};
|
2014-02-02 03:34:54 +08:00
|
|
|
const Caller caller = callers[method];
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
return caller(_img, _templ, _result);
|
2014-01-14 18:45:01 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#endif
|
2010-11-13 04:57:01 +08:00
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-08 20:48:13 +08:00
|
|
|
|
|
|
|
typedef IppStatus (CV_STDCALL * ippimatchTemplate)(const void*, int, IppiSize, const void*, int, IppiSize, Ipp32f* , int , IppEnum , Ipp8u*);
|
|
|
|
|
2014-04-09 21:36:00 +08:00
|
|
|
static bool ipp_crossCorr(const Mat& src, const Mat& tpl, Mat& dst)
|
2014-04-08 20:48:13 +08:00
|
|
|
{
|
|
|
|
IppStatus status;
|
|
|
|
|
|
|
|
IppiSize srcRoiSize = {src.cols,src.rows};
|
|
|
|
IppiSize tplRoiSize = {tpl.cols,tpl.rows};
|
|
|
|
|
|
|
|
Ipp8u *pBuffer;
|
|
|
|
int bufSize=0;
|
|
|
|
|
|
|
|
int depth = src.depth();
|
|
|
|
|
2014-04-09 21:36:00 +08:00
|
|
|
ippimatchTemplate ippFunc =
|
2014-04-08 20:48:13 +08:00
|
|
|
depth==CV_8U ? (ippimatchTemplate)ippiCrossCorrNorm_8u32f_C1R:
|
|
|
|
depth==CV_32F? (ippimatchTemplate)ippiCrossCorrNorm_32f_C1R: 0;
|
|
|
|
|
|
|
|
if (ippFunc==0)
|
|
|
|
return false;
|
|
|
|
|
2014-04-09 21:36:00 +08:00
|
|
|
IppEnum funCfg = (IppEnum)(ippAlgAuto | ippiNormNone | ippiROIValid);
|
|
|
|
|
|
|
|
status = ippiCrossCorrNormGetBufferSize(srcRoiSize, tplRoiSize, funCfg, &bufSize);
|
|
|
|
if ( status < 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pBuffer = ippsMalloc_8u( bufSize );
|
|
|
|
|
|
|
|
status = ippFunc(src.data, (int)src.step, srcRoiSize, tpl.data, (int)tpl.step, tplRoiSize, (Ipp32f*)dst.data, (int)dst.step, funCfg, pBuffer);
|
|
|
|
|
|
|
|
ippsFree( pBuffer );
|
|
|
|
return status >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ipp_sqrDistance(const Mat& src, const Mat& tpl, Mat& dst)
|
|
|
|
{
|
|
|
|
IppStatus status;
|
|
|
|
|
|
|
|
IppiSize srcRoiSize = {src.cols,src.rows};
|
|
|
|
IppiSize tplRoiSize = {tpl.cols,tpl.rows};
|
|
|
|
|
|
|
|
Ipp8u *pBuffer;
|
|
|
|
int bufSize=0;
|
|
|
|
|
|
|
|
int depth = src.depth();
|
|
|
|
|
|
|
|
ippimatchTemplate ippFunc =
|
|
|
|
depth==CV_8U ? (ippimatchTemplate)ippiSqrDistanceNorm_8u32f_C1R:
|
|
|
|
depth==CV_32F? (ippimatchTemplate)ippiSqrDistanceNorm_32f_C1R: 0;
|
|
|
|
|
|
|
|
if (ippFunc==0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IppEnum funCfg = (IppEnum)(ippAlgAuto | ippiNormNone | ippiROIValid);
|
2014-04-08 20:48:13 +08:00
|
|
|
|
2014-04-09 21:36:00 +08:00
|
|
|
status = ippiSqrDistanceNormGetBufferSize(srcRoiSize, tplRoiSize, funCfg, &bufSize);
|
2014-04-08 20:48:13 +08:00
|
|
|
if ( status < 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pBuffer = ippsMalloc_8u( bufSize );
|
|
|
|
|
2014-04-09 15:08:32 +08:00
|
|
|
status = ippFunc(src.data, (int)src.step, srcRoiSize, tpl.data, (int)tpl.step, tplRoiSize, (Ipp32f*)dst.data, (int)dst.step, funCfg, pBuffer);
|
2014-04-08 20:48:13 +08:00
|
|
|
|
|
|
|
ippsFree( pBuffer );
|
2014-04-09 15:08:32 +08:00
|
|
|
return status >= 0;
|
2014-04-08 20:48:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-01-25 20:43:47 +08:00
|
|
|
void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
|
2010-11-13 04:57:01 +08:00
|
|
|
Size corrsize, int ctype,
|
|
|
|
Point anchor, double delta, int borderType )
|
|
|
|
{
|
|
|
|
const double blockScale = 4.5;
|
|
|
|
const int minBlockSize = 256;
|
|
|
|
std::vector<uchar> buf;
|
|
|
|
|
2012-01-25 20:43:47 +08:00
|
|
|
Mat templ = _templ;
|
2010-11-13 04:57:01 +08:00
|
|
|
int depth = img.depth(), cn = img.channels();
|
|
|
|
int tdepth = templ.depth(), tcn = templ.channels();
|
|
|
|
int cdepth = CV_MAT_DEPTH(ctype), ccn = CV_MAT_CN(ctype);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
CV_Assert( img.dims <= 2 && templ.dims <= 2 && corr.dims <= 2 );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-01-25 20:43:47 +08:00
|
|
|
if( depth != tdepth && tdepth != std::max(CV_32F, depth) )
|
|
|
|
{
|
|
|
|
_templ.convertTo(templ, std::max(CV_32F, depth));
|
|
|
|
tdepth = templ.depth();
|
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-01-25 20:43:47 +08:00
|
|
|
CV_Assert( depth == tdepth || tdepth == CV_32F);
|
2010-11-13 04:57:01 +08:00
|
|
|
CV_Assert( corrsize.height <= img.rows + templ.rows - 1 &&
|
|
|
|
corrsize.width <= img.cols + templ.cols - 1 );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
CV_Assert( ccn == 1 || delta == 0 );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
corr.create(corrsize, ctype);
|
|
|
|
|
2012-04-12 20:34:55 +08:00
|
|
|
int maxDepth = depth > CV_8S ? CV_64F : std::max(std::max(CV_32F, tdepth), cdepth);
|
2010-11-13 04:57:01 +08:00
|
|
|
Size blocksize, dftsize;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
blocksize.width = cvRound(templ.cols*blockScale);
|
|
|
|
blocksize.width = std::max( blocksize.width, minBlockSize - templ.cols + 1 );
|
|
|
|
blocksize.width = std::min( blocksize.width, corr.cols );
|
|
|
|
blocksize.height = cvRound(templ.rows*blockScale);
|
|
|
|
blocksize.height = std::max( blocksize.height, minBlockSize - templ.rows + 1 );
|
|
|
|
blocksize.height = std::min( blocksize.height, corr.rows );
|
|
|
|
|
|
|
|
dftsize.width = std::max(getOptimalDFTSize(blocksize.width + templ.cols - 1), 2);
|
|
|
|
dftsize.height = getOptimalDFTSize(blocksize.height + templ.rows - 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
if( dftsize.width <= 0 || dftsize.height <= 0 )
|
|
|
|
CV_Error( CV_StsOutOfRange, "the input arrays are too big" );
|
|
|
|
|
|
|
|
// recompute block size
|
2010-11-13 04:57:01 +08:00
|
|
|
blocksize.width = dftsize.width - templ.cols + 1;
|
|
|
|
blocksize.width = MIN( blocksize.width, corr.cols );
|
|
|
|
blocksize.height = dftsize.height - templ.rows + 1;
|
|
|
|
blocksize.height = MIN( blocksize.height, corr.rows );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat dftTempl( dftsize.height*tcn, dftsize.width, maxDepth );
|
|
|
|
Mat dftImg( dftsize, maxDepth );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
int i, k, bufSize = 0;
|
|
|
|
if( tcn > 1 && tdepth != maxDepth )
|
|
|
|
bufSize = templ.cols*templ.rows*CV_ELEM_SIZE(tdepth);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( cn > 1 && depth != maxDepth )
|
|
|
|
bufSize = std::max( bufSize, (blocksize.width + templ.cols - 1)*
|
|
|
|
(blocksize.height + templ.rows - 1)*CV_ELEM_SIZE(depth));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( (ccn > 1 || cn > 1) && cdepth != maxDepth )
|
|
|
|
bufSize = std::max( bufSize, blocksize.width*blocksize.height*CV_ELEM_SIZE(cdepth));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
buf.resize(bufSize);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// compute DFT of each template plane
|
2010-11-13 04:57:01 +08:00
|
|
|
for( k = 0; k < tcn; k++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int yofs = k*dftsize.height;
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat src = templ;
|
|
|
|
Mat dst(dftTempl, Rect(0, yofs, dftsize.width, dftsize.height));
|
|
|
|
Mat dst1(dftTempl, Rect(0, yofs, templ.cols, templ.rows));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( tcn > 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
src = tdepth == maxDepth ? dst1 : Mat(templ.size(), tdepth, &buf[0]);
|
|
|
|
int pairs[] = {k, 0};
|
|
|
|
mixChannels(&templ, 1, &src, 1, pairs, 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( dst1.data != src.data )
|
|
|
|
src.convertTo(dst1, dst1.depth());
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( dst.cols > templ.cols )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat part(dst, Range(0, templ.rows), Range(templ.cols, dst.cols));
|
|
|
|
part = Scalar::all(0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-11-13 04:57:01 +08:00
|
|
|
dft(dst, dst, 0, templ.rows);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
int tileCountX = (corr.cols + blocksize.width - 1)/blocksize.width;
|
|
|
|
int tileCountY = (corr.rows + blocksize.height - 1)/blocksize.height;
|
|
|
|
int tileCount = tileCountX * tileCountY;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
Size wholeSize = img.size();
|
|
|
|
Point roiofs(0,0);
|
|
|
|
Mat img0 = img;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( !(borderType & BORDER_ISOLATED) )
|
|
|
|
{
|
|
|
|
img.locateROI(wholeSize, roiofs);
|
|
|
|
img0.adjustROI(roiofs.y, wholeSize.height-img.rows-roiofs.y,
|
|
|
|
roiofs.x, wholeSize.width-img.cols-roiofs.x);
|
|
|
|
}
|
2011-12-26 16:05:33 +08:00
|
|
|
borderType |= BORDER_ISOLATED;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// calculate correlation by blocks
|
2010-11-13 04:57:01 +08:00
|
|
|
for( i = 0; i < tileCount; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
int x = (i%tileCountX)*blocksize.width;
|
|
|
|
int y = (i/tileCountX)*blocksize.height;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
Size bsz(std::min(blocksize.width, corr.cols - x),
|
|
|
|
std::min(blocksize.height, corr.rows - y));
|
|
|
|
Size dsz(bsz.width + templ.cols - 1, bsz.height + templ.rows - 1);
|
|
|
|
int x0 = x - anchor.x + roiofs.x, y0 = y - anchor.y + roiofs.y;
|
|
|
|
int x1 = std::max(0, x0), y1 = std::max(0, y0);
|
|
|
|
int x2 = std::min(img0.cols, x0 + dsz.width);
|
|
|
|
int y2 = std::min(img0.rows, y0 + dsz.height);
|
|
|
|
Mat src0(img0, Range(y1, y2), Range(x1, x2));
|
|
|
|
Mat dst(dftImg, Rect(0, 0, dsz.width, dsz.height));
|
|
|
|
Mat dst1(dftImg, Rect(x1-x0, y1-y0, x2-x1, y2-y1));
|
|
|
|
Mat cdst(corr, Rect(x, y, bsz.width, bsz.height));
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
for( k = 0; k < cn; k++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat src = src0;
|
2011-04-26 20:33:25 +08:00
|
|
|
dftImg = Scalar::all(0);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( cn > 1 )
|
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
src = depth == maxDepth ? dst1 : Mat(y2-y1, x2-x1, depth, &buf[0]);
|
|
|
|
int pairs[] = {k, 0};
|
|
|
|
mixChannels(&src0, 1, &src, 1, pairs, 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( dst1.data != src.data )
|
|
|
|
src.convertTo(dst1, dst1.depth());
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( x2 - x1 < dsz.width || y2 - y1 < dsz.height )
|
|
|
|
copyMakeBorder(dst1, dst, y1-y0, dst.rows-dst1.rows-(y1-y0),
|
|
|
|
x1-x0, dst.cols-dst1.cols-(x1-x0), borderType);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
dft( dftImg, dftImg, 0, dsz.height );
|
|
|
|
Mat dftTempl1(dftTempl, Rect(0, tcn > 1 ? k*dftsize.height : 0,
|
|
|
|
dftsize.width, dftsize.height));
|
|
|
|
mulSpectrums(dftImg, dftTempl1, dftImg, 0, true);
|
|
|
|
dft( dftImg, dftImg, DFT_INVERSE + DFT_SCALE, bsz.height );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
src = dftImg(Rect(0, 0, bsz.width, bsz.height));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( ccn > 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
if( cdepth != maxDepth )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat plane(bsz, cdepth, &buf[0]);
|
|
|
|
src.convertTo(plane, cdepth, 1, delta);
|
|
|
|
src = plane;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-11-13 04:57:01 +08:00
|
|
|
int pairs[] = {0, k};
|
2012-10-17 07:18:30 +08:00
|
|
|
mixChannels(&src, 1, &cdst, 1, pairs, 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
if( k == 0 )
|
|
|
|
src.convertTo(cdst, cdepth, 1, delta);
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
if( maxDepth != cdepth )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat plane(bsz, cdepth, &buf[0]);
|
|
|
|
src.convertTo(plane, cdepth);
|
|
|
|
src = plane;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-11-13 04:57:01 +08:00
|
|
|
add(src, cdst, cdst);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-01-14 18:45:01 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-04-17 03:35:39 +08:00
|
|
|
int type = _img.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
2010-11-13 04:57:01 +08:00
|
|
|
CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
|
2014-04-17 03:35:39 +08:00
|
|
|
CV_Assert( (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 );
|
2014-01-17 22:37:57 +08:00
|
|
|
|
2014-02-02 03:34:54 +08:00
|
|
|
bool needswap = _img.size().height < _templ.size().height || _img.size().width < _templ.size().width;
|
|
|
|
if (needswap)
|
2014-01-17 20:26:11 +08:00
|
|
|
{
|
2014-01-17 19:01:22 +08:00
|
|
|
CV_Assert(_img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width);
|
2014-01-17 20:26:11 +08:00
|
|
|
}
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
CV_OCL_RUN(_img.dims() <= 2 && _result.isUMat(),
|
2014-02-02 03:34:54 +08:00
|
|
|
(!needswap ? ocl_matchTemplate(_img, _templ, _result, method) : ocl_matchTemplate(_templ, _img, _result, method)))
|
2014-01-14 18:45:01 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
|
|
|
|
method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
|
|
|
|
bool isNormed = method == CV_TM_CCORR_NORMED ||
|
2010-05-12 01:44:00 +08:00
|
|
|
method == CV_TM_SQDIFF_NORMED ||
|
|
|
|
method == CV_TM_CCOEFF_NORMED;
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat img = _img.getMat(), templ = _templ.getMat();
|
2014-02-02 03:34:54 +08:00
|
|
|
if (needswap)
|
2010-11-13 04:57:01 +08:00
|
|
|
std::swap(img, templ);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1);
|
|
|
|
_result.create(corrSize, CV_32F);
|
|
|
|
Mat result = _result.getMat();
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2014-04-09 21:36:00 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
|
|
|
if (tegra::matchTemplate(img, templ, result, method))
|
2014-04-08 20:48:13 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-17 03:35:39 +08:00
|
|
|
if (method == CV_TM_SQDIFF && cn == 1)
|
2014-04-16 23:23:44 +08:00
|
|
|
{
|
|
|
|
if (ipp_sqrDistance(img, templ, result))
|
|
|
|
return;
|
|
|
|
setIppErrorStatus();
|
|
|
|
}
|
2013-02-04 16:57:03 +08:00
|
|
|
#endif
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-17 03:35:39 +08:00
|
|
|
if (cn == 1)
|
|
|
|
{
|
|
|
|
if (!ipp_crossCorr(img, templ, result))
|
|
|
|
{
|
|
|
|
setIppErrorStatus();
|
|
|
|
crossCorr( img, templ, result, result.size(), result.type(), Point(0,0), 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-04-15 15:35:30 +08:00
|
|
|
#endif
|
2011-04-17 21:14:45 +08:00
|
|
|
crossCorr( img, templ, result, result.size(), result.type(), Point(0,0), 0, 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( method == CV_TM_CCORR )
|
|
|
|
return;
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
double invArea = 1./((double)templ.rows * templ.cols);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
Mat sum, sqsum;
|
|
|
|
Scalar templMean, templSdv;
|
|
|
|
double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
|
|
|
|
double templNorm = 0, templSum2 = 0;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( method == CV_TM_CCOEFF )
|
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
integral(img, sum, CV_64F);
|
|
|
|
templMean = mean(templ);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
integral(img, sum, sqsum, CV_64F);
|
|
|
|
meanStdDev( templ, templMean, templSdv );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-31 17:26:21 +08:00
|
|
|
templNorm = templSdv[0]*templSdv[0] + templSdv[1]*templSdv[1] + templSdv[2]*templSdv[2] + templSdv[3]*templSdv[3];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( templNorm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
result = Scalar::all(1);
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-03-31 17:26:21 +08:00
|
|
|
templSum2 = templNorm + templMean[0]*templMean[0] + templMean[1]*templMean[1] + templMean[2]*templMean[2] + templMean[3]*templMean[3];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( numType != 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
templMean = Scalar::all(0);
|
|
|
|
templNorm = templSum2;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
templSum2 /= invArea;
|
2013-02-25 00:14:01 +08:00
|
|
|
templNorm = std::sqrt(templNorm);
|
|
|
|
templNorm /= std::sqrt(invArea); // care of accuracy here
|
2010-11-13 04:57:01 +08:00
|
|
|
|
|
|
|
q0 = (double*)sqsum.data;
|
|
|
|
q1 = q0 + templ.cols*cn;
|
|
|
|
q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
|
|
|
|
q3 = q2 + templ.cols*cn;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
double* p0 = (double*)sum.data;
|
|
|
|
double* p1 = p0 + templ.cols*cn;
|
|
|
|
double* p2 = (double*)(sum.data + templ.rows*sum.step);
|
|
|
|
double* p3 = p2 + templ.cols*cn;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-26 00:55:46 +08:00
|
|
|
int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
|
|
|
|
int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
int i, j, k;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
for( i = 0; i < result.rows; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
float* rrow = (float*)(result.data + i*result.step);
|
|
|
|
int idx = i * sumstep;
|
|
|
|
int idx2 = i * sqstep;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
double num = rrow[j], t;
|
2010-11-13 04:57:01 +08:00
|
|
|
double wndMean2 = 0, wndSum2 = 0;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( numType == 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
{
|
|
|
|
t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
|
2013-03-31 17:26:21 +08:00
|
|
|
wndMean2 += t*t;
|
2010-11-13 04:57:01 +08:00
|
|
|
num -= t*templMean[k];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
wndMean2 *= invArea;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( isNormed || numType == 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
{
|
|
|
|
t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
|
2010-11-13 04:57:01 +08:00
|
|
|
wndSum2 += t;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( numType == 2 )
|
2013-01-09 21:53:19 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
num = wndSum2 - 2*num + templSum2;
|
2013-01-09 21:53:19 +08:00
|
|
|
num = MAX(num, 0.);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
if( isNormed )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
t = std::sqrt(MAX(wndSum2 - wndMean2,0))*templNorm;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( fabs(num) < t )
|
|
|
|
num /= t;
|
|
|
|
else if( fabs(num) < t*1.125 )
|
|
|
|
num = num > 0 ? 1 : -1;
|
|
|
|
else
|
|
|
|
num = method != CV_TM_SQDIFF_NORMED ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rrow[j] = (float)num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:57:01 +08:00
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvMatchTemplate( const CvArr* _img, const CvArr* _templ, CvArr* _result, int method )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-13 04:57:01 +08:00
|
|
|
cv::Mat img = cv::cvarrToMat(_img), templ = cv::cvarrToMat(_templ),
|
|
|
|
result = cv::cvarrToMat(_result);
|
|
|
|
CV_Assert( result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1,
|
|
|
|
std::abs(img.rows - templ.rows) + 1) &&
|
|
|
|
result.type() == CV_32F );
|
|
|
|
matchTemplate(img, templ, result, method);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|