2010-12-20 17:51:25 +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.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// 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 GpuMaterials 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 bpied warranties, including, but not limited to, the bpied
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
2012-10-02 02:37:20 +08:00
|
|
|
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
void cv::gpu::meanStdDev(const GpuMat&, Scalar&, Scalar&) { throw_nogpu(); }
|
2012-02-22 18:00:53 +08:00
|
|
|
void cv::gpu::meanStdDev(const GpuMat&, Scalar&, Scalar&, GpuMat&) { throw_nogpu(); }
|
2010-12-20 17:51:25 +08:00
|
|
|
double cv::gpu::norm(const GpuMat&, int) { throw_nogpu(); return 0.0; }
|
2011-02-01 18:46:19 +08:00
|
|
|
double cv::gpu::norm(const GpuMat&, int, GpuMat&) { throw_nogpu(); return 0.0; }
|
2010-12-20 17:51:25 +08:00
|
|
|
double cv::gpu::norm(const GpuMat&, const GpuMat&, int) { throw_nogpu(); return 0.0; }
|
|
|
|
Scalar cv::gpu::sum(const GpuMat&) { throw_nogpu(); return Scalar(); }
|
|
|
|
Scalar cv::gpu::sum(const GpuMat&, GpuMat&) { throw_nogpu(); return Scalar(); }
|
2011-01-31 22:37:03 +08:00
|
|
|
Scalar cv::gpu::absSum(const GpuMat&) { throw_nogpu(); return Scalar(); }
|
|
|
|
Scalar cv::gpu::absSum(const GpuMat&, GpuMat&) { throw_nogpu(); return Scalar(); }
|
2010-12-20 17:51:25 +08:00
|
|
|
Scalar cv::gpu::sqrSum(const GpuMat&) { throw_nogpu(); return Scalar(); }
|
|
|
|
Scalar cv::gpu::sqrSum(const GpuMat&, GpuMat&) { throw_nogpu(); return Scalar(); }
|
|
|
|
void cv::gpu::minMax(const GpuMat&, double*, double*, const GpuMat&) { throw_nogpu(); }
|
|
|
|
void cv::gpu::minMax(const GpuMat&, double*, double*, const GpuMat&, GpuMat&) { throw_nogpu(); }
|
|
|
|
void cv::gpu::minMaxLoc(const GpuMat&, double*, double*, Point*, Point*, const GpuMat&) { throw_nogpu(); }
|
|
|
|
void cv::gpu::minMaxLoc(const GpuMat&, double*, double*, Point*, Point*, const GpuMat&, GpuMat&, GpuMat&) { throw_nogpu(); }
|
|
|
|
int cv::gpu::countNonZero(const GpuMat&) { throw_nogpu(); return 0; }
|
|
|
|
int cv::gpu::countNonZero(const GpuMat&, GpuMat&) { throw_nogpu(); return 0; }
|
2011-09-22 15:08:26 +08:00
|
|
|
void cv::gpu::reduce(const GpuMat&, GpuMat&, int, int, int, Stream&) { throw_nogpu(); }
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class DeviceBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit DeviceBuffer(int count_ = 1) : count(count_)
|
|
|
|
{
|
|
|
|
cudaSafeCall( cudaMalloc(&pdev, count * sizeof(double)) );
|
|
|
|
}
|
|
|
|
~DeviceBuffer()
|
|
|
|
{
|
|
|
|
cudaSafeCall( cudaFree(pdev) );
|
|
|
|
}
|
|
|
|
|
|
|
|
operator double*() {return pdev;}
|
|
|
|
|
|
|
|
void download(double* hptr)
|
|
|
|
{
|
|
|
|
double hbuf;
|
|
|
|
cudaSafeCall( cudaMemcpy(&hbuf, pdev, sizeof(double), cudaMemcpyDeviceToHost) );
|
|
|
|
*hptr = hbuf;
|
|
|
|
}
|
|
|
|
void download(double** hptrs)
|
|
|
|
{
|
|
|
|
AutoBuffer<double, 2 * sizeof(double)> hbuf(count);
|
|
|
|
cudaSafeCall( cudaMemcpy((void*)hbuf, pdev, count * sizeof(double), cudaMemcpyDeviceToHost) );
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
*hptrs[i] = hbuf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double* pdev;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// meanStdDev
|
|
|
|
|
|
|
|
void cv::gpu::meanStdDev(const GpuMat& src, Scalar& mean, Scalar& stddev)
|
2012-02-22 18:00:53 +08:00
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
meanStdDev(src, mean, stddev, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::gpu::meanStdDev(const GpuMat& src, Scalar& mean, Scalar& stddev, GpuMat& buf)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
|
|
|
CV_Assert(src.type() == CV_8UC1);
|
|
|
|
|
2012-03-27 18:34:30 +08:00
|
|
|
if (!TargetArchs::builtWith(FEATURE_SET_COMPUTE_13) || !DeviceInfo().supports(FEATURE_SET_COMPUTE_13))
|
|
|
|
CV_Error(CV_StsNotImplemented, "Not sufficient compute capebility");
|
|
|
|
|
2010-12-20 17:51:25 +08:00
|
|
|
NppiSize sz;
|
|
|
|
sz.width = src.cols;
|
|
|
|
sz.height = src.rows;
|
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
DeviceBuffer dbuf(2);
|
2011-05-10 20:39:12 +08:00
|
|
|
|
2012-01-30 21:15:20 +08:00
|
|
|
int bufSize;
|
2012-05-25 17:44:00 +08:00
|
|
|
#if (CUDA_VERSION <= 4020)
|
2012-01-30 21:15:20 +08:00
|
|
|
nppSafeCall( nppiMeanStdDev8uC1RGetBufferHostSize(sz, &bufSize) );
|
2012-05-25 17:44:00 +08:00
|
|
|
#else
|
|
|
|
nppSafeCall( nppiMeanStdDevGetBufferHostSize_8u_C1R(sz, &bufSize) );
|
|
|
|
#endif
|
2012-01-30 21:15:20 +08:00
|
|
|
|
2012-02-22 18:00:53 +08:00
|
|
|
ensureSizeIsEnough(1, bufSize, CV_8UC1, buf);
|
|
|
|
|
2012-01-30 21:15:20 +08:00
|
|
|
nppSafeCall( nppiMean_StdDev_8u_C1R(src.ptr<Npp8u>(), static_cast<int>(src.step), sz, buf.ptr<Npp8u>(), dbuf, (double*)dbuf + 1) );
|
2011-05-10 20:39:12 +08:00
|
|
|
|
2011-05-31 16:31:10 +08:00
|
|
|
cudaSafeCall( cudaDeviceSynchronize() );
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
double* ptrs[2] = {mean.val, stddev.val};
|
|
|
|
dbuf.download(ptrs);
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// norm
|
|
|
|
|
2011-02-01 18:23:10 +08:00
|
|
|
double cv::gpu::norm(const GpuMat& src, int normType)
|
2011-02-01 18:46:19 +08:00
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
return norm(src, normType, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
double cv::gpu::norm(const GpuMat& src, int normType, GpuMat& buf)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2);
|
|
|
|
|
2011-02-01 18:23:10 +08:00
|
|
|
GpuMat src_single_channel = src.reshape(1);
|
|
|
|
|
|
|
|
if (normType == NORM_L1)
|
2011-02-01 18:46:19 +08:00
|
|
|
return absSum(src_single_channel, buf)[0];
|
2011-02-01 18:23:10 +08:00
|
|
|
|
|
|
|
if (normType == NORM_L2)
|
2012-02-22 18:00:53 +08:00
|
|
|
return std::sqrt(sqrSum(src_single_channel, buf)[0]);
|
2011-02-01 18:23:10 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
// NORM_INF
|
|
|
|
double min_val, max_val;
|
|
|
|
minMax(src_single_channel, &min_val, &max_val, GpuMat(), buf);
|
|
|
|
return std::max(std::abs(min_val), std::abs(max_val));
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double cv::gpu::norm(const GpuMat& src1, const GpuMat& src2, int normType)
|
|
|
|
{
|
|
|
|
CV_Assert(src1.type() == CV_8UC1);
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src1.size() == src2.size() && src1.type() == src2.type());
|
2010-12-20 17:51:25 +08:00
|
|
|
CV_Assert(normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2);
|
|
|
|
|
|
|
|
typedef NppStatus (*npp_norm_diff_func_t)(const Npp8u* pSrc1, int nSrcStep1, const Npp8u* pSrc2, int nSrcStep2,
|
|
|
|
NppiSize oSizeROI, Npp64f* pRetVal);
|
|
|
|
|
|
|
|
static const npp_norm_diff_func_t npp_norm_diff_func[] = {nppiNormDiff_Inf_8u_C1R, nppiNormDiff_L1_8u_C1R, nppiNormDiff_L2_8u_C1R};
|
|
|
|
|
|
|
|
NppiSize sz;
|
|
|
|
sz.width = src1.cols;
|
|
|
|
sz.height = src1.rows;
|
|
|
|
|
|
|
|
int funcIdx = normType >> 1;
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
double retVal;
|
2011-05-10 20:39:12 +08:00
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
DeviceBuffer dbuf;
|
2011-05-10 20:39:12 +08:00
|
|
|
|
2011-08-08 19:28:14 +08:00
|
|
|
nppSafeCall( npp_norm_diff_func[funcIdx](src1.ptr<Npp8u>(), static_cast<int>(src1.step), src2.ptr<Npp8u>(), static_cast<int>(src2.step), sz, dbuf) );
|
2011-05-10 20:39:12 +08:00
|
|
|
|
2011-05-31 16:31:10 +08:00
|
|
|
cudaSafeCall( cudaDeviceSynchronize() );
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-05-11 15:30:46 +08:00
|
|
|
dbuf.download(&retVal);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Sum
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace matrix_reductions
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2011-11-14 17:02:06 +08:00
|
|
|
namespace sum
|
|
|
|
{
|
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void sumCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void sumMultipassCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void absSumCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void absSumMultipassCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void sqrSumCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void sqrSumMultipassCaller(const PtrStepSzb src, PtrStepb buf, double* sum, int cn);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2011-11-14 17:02:06 +08:00
|
|
|
void getBufSizeRequired(int cols, int rows, int cn, int& bufcols, int& bufrows);
|
|
|
|
}
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
2011-11-14 17:02:06 +08:00
|
|
|
}}}
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::sum(const GpuMat& src)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
return sum(src, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::sum(const GpuMat& src, GpuMat& buf)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
using namespace cv::gpu::device::matrix_reductions::sum;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*Caller)(const PtrStepSzb, PtrStepb, double*, int);
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[] =
|
|
|
|
{
|
|
|
|
sumMultipassCaller<unsigned char>, sumMultipassCaller<char>,
|
|
|
|
sumMultipassCaller<unsigned short>, sumMultipassCaller<short>,
|
|
|
|
sumMultipassCaller<int>, sumMultipassCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[] = {
|
|
|
|
sumCaller<unsigned char>, sumCaller<char>,
|
|
|
|
sumCaller<unsigned short>, sumCaller<short>,
|
|
|
|
sumCaller<int>, sumCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_32F);
|
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Size buf_size;
|
2012-03-26 22:33:43 +08:00
|
|
|
getBufSizeRequired(src.cols, src.rows, src.channels(), buf_size.width, buf_size.height);
|
2011-01-19 18:54:58 +08:00
|
|
|
ensureSizeIsEnough(buf_size, CV_8U, buf);
|
|
|
|
|
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = singlepass_callers;
|
|
|
|
|
|
|
|
Caller caller = callers[src.depth()];
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
double result[4];
|
|
|
|
caller(src, buf, result, src.channels());
|
|
|
|
return Scalar(result[0], result[1], result[2], result[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::absSum(const GpuMat& src)
|
2011-01-31 22:37:03 +08:00
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
return absSum(src, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::absSum(const GpuMat& src, GpuMat& buf)
|
2011-01-31 22:37:03 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
using namespace cv::gpu::device::matrix_reductions::sum;
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*Caller)(const PtrStepSzb, PtrStepb, double*, int);
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[] =
|
|
|
|
{
|
|
|
|
absSumMultipassCaller<unsigned char>, absSumMultipassCaller<char>,
|
|
|
|
absSumMultipassCaller<unsigned short>, absSumMultipassCaller<short>,
|
|
|
|
absSumMultipassCaller<int>, absSumMultipassCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[] =
|
|
|
|
{
|
|
|
|
absSumCaller<unsigned char>, absSumCaller<char>,
|
|
|
|
absSumCaller<unsigned short>, absSumCaller<short>,
|
|
|
|
absSumCaller<int>, absSumCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-31 22:37:03 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_32F);
|
|
|
|
|
2011-01-31 22:37:03 +08:00
|
|
|
Size buf_size;
|
2012-03-26 22:33:43 +08:00
|
|
|
getBufSizeRequired(src.cols, src.rows, src.channels(), buf_size.width, buf_size.height);
|
2011-01-31 22:37:03 +08:00
|
|
|
ensureSizeIsEnough(buf_size, CV_8U, buf);
|
|
|
|
|
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-31 22:37:03 +08:00
|
|
|
callers = singlepass_callers;
|
|
|
|
|
|
|
|
Caller caller = callers[src.depth()];
|
|
|
|
|
|
|
|
double result[4];
|
|
|
|
caller(src, buf, result, src.channels());
|
|
|
|
return Scalar(result[0], result[1], result[2], result[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::sqrSum(const GpuMat& src)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
return sqrSum(src, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
Scalar cv::gpu::sqrSum(const GpuMat& src, GpuMat& buf)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
using namespace cv::gpu::device::matrix_reductions::sum;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*Caller)(const PtrStepSzb, PtrStepb, double*, int);
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[] =
|
|
|
|
{
|
|
|
|
sqrSumMultipassCaller<unsigned char>, sqrSumMultipassCaller<char>,
|
|
|
|
sqrSumMultipassCaller<unsigned short>, sqrSumMultipassCaller<short>,
|
|
|
|
sqrSumMultipassCaller<int>, sqrSumMultipassCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[7] =
|
|
|
|
{
|
|
|
|
sqrSumCaller<unsigned char>, sqrSumCaller<char>,
|
|
|
|
sqrSumCaller<unsigned short>, sqrSumCaller<short>,
|
|
|
|
sqrSumCaller<int>, sqrSumCaller<float>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_32F);
|
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = singlepass_callers;
|
|
|
|
|
|
|
|
Size buf_size;
|
2012-03-26 22:33:43 +08:00
|
|
|
getBufSizeRequired(src.cols, src.rows, src.channels(), buf_size.width, buf_size.height);
|
2011-01-19 18:54:58 +08:00
|
|
|
ensureSizeIsEnough(buf_size, CV_8U, buf);
|
|
|
|
|
|
|
|
Caller caller = callers[src.depth()];
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
double result[4];
|
|
|
|
caller(src, buf, result, src.channels());
|
|
|
|
return Scalar(result[0], result[1], result[2], result[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Find min or max
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace matrix_reductions
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace minmax
|
2011-11-14 17:02:06 +08:00
|
|
|
{
|
|
|
|
void getBufSizeRequired(int cols, int rows, int elem_size, int& bufcols, int& bufrows);
|
2012-03-26 22:33:43 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxCaller(const PtrStepSzb src, double* minval, double* maxval, PtrStepb buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxMaskCaller(const PtrStepSzb src, const PtrStepb mask, double* minval, double* maxval, PtrStepb buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxMultipassCaller(const PtrStepSzb src, double* minval, double* maxval, PtrStepb buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxMaskMultipassCaller(const PtrStepSzb src, const PtrStepb mask, double* minval, double* maxval, PtrStepb buf);
|
2011-11-14 17:02:06 +08:00
|
|
|
}
|
2011-11-09 21:13:52 +08:00
|
|
|
}
|
2011-11-14 17:02:06 +08:00
|
|
|
}}}
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
void cv::gpu::minMax(const GpuMat& src, double* minVal, double* maxVal, const GpuMat& mask)
|
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
minMax(src, minVal, maxVal, mask, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cv::gpu::minMax(const GpuMat& src, double* minVal, double* maxVal, const GpuMat& mask, GpuMat& buf)
|
|
|
|
{
|
2011-11-14 17:02:06 +08:00
|
|
|
using namespace ::cv::gpu::device::matrix_reductions::minmax;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*Caller)(const PtrStepSzb, double*, double*, PtrStepb);
|
|
|
|
typedef void (*MaskedCaller)(const PtrStepSzb, const PtrStepb, double*, double*, PtrStepb);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[] =
|
|
|
|
{
|
|
|
|
minMaxMultipassCaller<unsigned char>, minMaxMultipassCaller<char>,
|
|
|
|
minMaxMultipassCaller<unsigned short>, minMaxMultipassCaller<short>,
|
|
|
|
minMaxMultipassCaller<int>, minMaxMultipassCaller<float>, 0
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[] =
|
|
|
|
{
|
|
|
|
minMaxCaller<unsigned char>, minMaxCaller<char>,
|
|
|
|
minMaxCaller<unsigned short>, minMaxCaller<short>,
|
|
|
|
minMaxCaller<int>, minMaxCaller<float>, minMaxCaller<double>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static MaskedCaller masked_multipass_callers[] =
|
|
|
|
{
|
|
|
|
minMaxMaskMultipassCaller<unsigned char>, minMaxMaskMultipassCaller<char>,
|
2011-11-09 21:13:52 +08:00
|
|
|
minMaxMaskMultipassCaller<unsigned short>, minMaxMaskMultipassCaller<short>,
|
|
|
|
minMaxMaskMultipassCaller<int>, minMaxMaskMultipassCaller<float>, 0
|
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static MaskedCaller masked_singlepass_callers[] =
|
|
|
|
{
|
|
|
|
minMaxMaskCaller<unsigned char>, minMaxMaskCaller<char>,
|
|
|
|
minMaxMaskCaller<unsigned short>, minMaxMaskCaller<short>,
|
|
|
|
minMaxMaskCaller<int>, minMaxMaskCaller<float>, minMaxMaskCaller<double>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_64F);
|
2010-12-20 17:51:25 +08:00
|
|
|
CV_Assert(src.channels() == 1);
|
|
|
|
CV_Assert(mask.empty() || (mask.type() == CV_8U && src.size() == mask.size()));
|
2011-01-20 23:08:48 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
if (src.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
if (!TargetArchs::builtWith(NATIVE_DOUBLE) || !DeviceInfo().supports(NATIVE_DOUBLE))
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "The device doesn't support double");
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:51:25 +08:00
|
|
|
double minVal_; if (!minVal) minVal = &minVal_;
|
|
|
|
double maxVal_; if (!maxVal) maxVal = &maxVal_;
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Size buf_size;
|
2011-08-08 19:28:14 +08:00
|
|
|
getBufSizeRequired(src.cols, src.rows, static_cast<int>(src.elemSize()), buf_size.width, buf_size.height);
|
2011-01-19 18:54:58 +08:00
|
|
|
ensureSizeIsEnough(buf_size, CV_8U, buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
if (mask.empty())
|
|
|
|
{
|
2011-01-19 18:54:58 +08:00
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = singlepass_callers;
|
|
|
|
|
|
|
|
Caller caller = callers[src.type()];
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(caller != 0);
|
2010-12-20 17:51:25 +08:00
|
|
|
caller(src, minVal, maxVal, buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-19 18:54:58 +08:00
|
|
|
MaskedCaller* callers = masked_multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = masked_singlepass_callers;
|
|
|
|
|
|
|
|
MaskedCaller caller = callers[src.type()];
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(caller != 0);
|
2010-12-20 17:51:25 +08:00
|
|
|
caller(src, mask, minVal, maxVal, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Locate min and max
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace matrix_reductions
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace minmaxloc
|
2011-11-14 17:02:06 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
void getBufSizeRequired(int cols, int rows, int elem_size, int& b1cols,
|
2011-11-14 17:02:06 +08:00
|
|
|
int& b1rows, int& b2cols, int& b2rows);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxLocCaller(const PtrStepSzb src, double* minval, double* maxval,
|
2011-10-13 23:14:04 +08:00
|
|
|
int minloc[2], int maxloc[2], PtrStepb valBuf, PtrStepb locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxLocMaskCaller(const PtrStepSzb src, const PtrStepb mask, double* minval, double* maxval,
|
2011-11-14 17:02:06 +08:00
|
|
|
int minloc[2], int maxloc[2], PtrStepb valBuf, PtrStepb locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxLocMultipassCaller(const PtrStepSzb src, double* minval, double* maxval,
|
2011-11-09 21:13:52 +08:00
|
|
|
int minloc[2], int maxloc[2], PtrStepb valBuf, PtrStepb locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
void minMaxLocMaskMultipassCaller(const PtrStepSzb src, const PtrStepb mask, double* minval, double* maxval,
|
2011-11-14 17:02:06 +08:00
|
|
|
int minloc[2], int maxloc[2], PtrStepb valBuf, PtrStepb locBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}}
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
void cv::gpu::minMaxLoc(const GpuMat& src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc, const GpuMat& mask)
|
2012-03-26 22:33:43 +08:00
|
|
|
{
|
2011-01-18 20:36:01 +08:00
|
|
|
GpuMat valBuf, locBuf;
|
|
|
|
minMaxLoc(src, minVal, maxVal, minLoc, maxLoc, mask, valBuf, locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::gpu::minMaxLoc(const GpuMat& src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc,
|
2011-01-18 20:36:01 +08:00
|
|
|
const GpuMat& mask, GpuMat& valBuf, GpuMat& locBuf)
|
2010-12-20 17:51:25 +08:00
|
|
|
{
|
2011-11-14 17:02:06 +08:00
|
|
|
using namespace ::cv::gpu::device::matrix_reductions::minmaxloc;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*Caller)(const PtrStepSzb, double*, double*, int[2], int[2], PtrStepb, PtrStepb);
|
|
|
|
typedef void (*MaskedCaller)(const PtrStepSzb, const PtrStepb, double*, double*, int[2], int[2], PtrStepb, PtrStepb);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[] =
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
minMaxLocMultipassCaller<unsigned char>, minMaxLocMultipassCaller<char>,
|
|
|
|
minMaxLocMultipassCaller<unsigned short>, minMaxLocMultipassCaller<short>,
|
|
|
|
minMaxLocMultipassCaller<int>, minMaxLocMultipassCaller<float>, 0
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[] =
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
minMaxLocCaller<unsigned char>, minMaxLocCaller<char>,
|
|
|
|
minMaxLocCaller<unsigned short>, minMaxLocCaller<short>,
|
|
|
|
minMaxLocCaller<int>, minMaxLocCaller<float>, minMaxLocCaller<double>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static MaskedCaller masked_multipass_callers[] =
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
|
|
|
minMaxLocMaskMultipassCaller<unsigned char>, minMaxLocMaskMultipassCaller<char>,
|
2012-03-26 22:33:43 +08:00
|
|
|
minMaxLocMaskMultipassCaller<unsigned short>, minMaxLocMaskMultipassCaller<short>,
|
|
|
|
minMaxLocMaskMultipassCaller<int>, minMaxLocMaskMultipassCaller<float>, 0
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static MaskedCaller masked_singlepass_callers[] =
|
|
|
|
{
|
|
|
|
minMaxLocMaskCaller<unsigned char>, minMaxLocMaskCaller<char>,
|
|
|
|
minMaxLocMaskCaller<unsigned short>, minMaxLocMaskCaller<short>,
|
|
|
|
minMaxLocMaskCaller<int>, minMaxLocMaskCaller<float>, minMaxLocMaskCaller<double>
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_64F);
|
2010-12-20 17:51:25 +08:00
|
|
|
CV_Assert(src.channels() == 1);
|
|
|
|
CV_Assert(mask.empty() || (mask.type() == CV_8U && src.size() == mask.size()));
|
2011-01-20 23:08:48 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
if (src.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
if (!TargetArchs::builtWith(NATIVE_DOUBLE) || !DeviceInfo().supports(NATIVE_DOUBLE))
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "The device doesn't support double");
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:51:25 +08:00
|
|
|
double minVal_; if (!minVal) minVal = &minVal_;
|
|
|
|
double maxVal_; if (!maxVal) maxVal = &maxVal_;
|
|
|
|
int minLoc_[2];
|
|
|
|
int maxLoc_[2];
|
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Size valbuf_size, locbuf_size;
|
2012-03-26 22:33:43 +08:00
|
|
|
getBufSizeRequired(src.cols, src.rows, static_cast<int>(src.elemSize()), valbuf_size.width,
|
2011-01-21 15:43:11 +08:00
|
|
|
valbuf_size.height, locbuf_size.width, locbuf_size.height);
|
2011-01-19 18:54:58 +08:00
|
|
|
ensureSizeIsEnough(valbuf_size, CV_8U, valBuf);
|
|
|
|
ensureSizeIsEnough(locbuf_size, CV_8U, locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
if (mask.empty())
|
|
|
|
{
|
2011-01-19 18:54:58 +08:00
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = singlepass_callers;
|
|
|
|
|
|
|
|
Caller caller = callers[src.type()];
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(caller != 0);
|
2011-01-18 20:36:01 +08:00
|
|
|
caller(src, minVal, maxVal, minLoc_, maxLoc_, valBuf, locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-19 18:54:58 +08:00
|
|
|
MaskedCaller* callers = masked_multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = masked_singlepass_callers;
|
|
|
|
|
|
|
|
MaskedCaller caller = callers[src.type()];
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(caller != 0);
|
2011-01-18 20:36:01 +08:00
|
|
|
caller(src, mask, minVal, maxVal, minLoc_, maxLoc_, valBuf, locBuf);
|
2010-12-20 17:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (minLoc) { minLoc->x = minLoc_[0]; minLoc->y = minLoc_[1]; }
|
|
|
|
if (maxLoc) { maxLoc->x = maxLoc_[0]; maxLoc->y = maxLoc_[1]; }
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Count non-zero elements
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace matrix_reductions
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace countnonzero
|
2011-11-14 17:02:06 +08:00
|
|
|
{
|
|
|
|
void getBufSizeRequired(int cols, int rows, int& bufcols, int& bufrows);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
int countNonZeroCaller(const PtrStepSzb src, PtrStepb buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
template <typename T>
|
2012-08-23 21:45:50 +08:00
|
|
|
int countNonZeroMultipassCaller(const PtrStepSzb src, PtrStepb buf);
|
2011-11-14 17:02:06 +08:00
|
|
|
}
|
2011-11-09 21:13:52 +08:00
|
|
|
}
|
2011-11-14 17:02:06 +08:00
|
|
|
}}}
|
2010-12-20 17:51:25 +08:00
|
|
|
|
|
|
|
int cv::gpu::countNonZero(const GpuMat& src)
|
|
|
|
{
|
|
|
|
GpuMat buf;
|
|
|
|
return countNonZero(src, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cv::gpu::countNonZero(const GpuMat& src, GpuMat& buf)
|
|
|
|
{
|
2011-11-14 17:02:06 +08:00
|
|
|
using namespace ::cv::gpu::device::matrix_reductions::countnonzero;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef int (*Caller)(const PtrStepSzb src, PtrStepb buf);
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller multipass_callers[7] =
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
|
|
|
countNonZeroMultipassCaller<unsigned char>, countNonZeroMultipassCaller<char>,
|
|
|
|
countNonZeroMultipassCaller<unsigned short>, countNonZeroMultipassCaller<short>,
|
2012-03-26 22:33:43 +08:00
|
|
|
countNonZeroMultipassCaller<int>, countNonZeroMultipassCaller<float>, 0
|
2011-11-09 21:13:52 +08:00
|
|
|
};
|
2011-01-19 18:54:58 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static Caller singlepass_callers[7] =
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
|
|
|
countNonZeroCaller<unsigned char>, countNonZeroCaller<char>,
|
|
|
|
countNonZeroCaller<unsigned short>, countNonZeroCaller<short>,
|
|
|
|
countNonZeroCaller<int>, countNonZeroCaller<float>, countNonZeroCaller<double> };
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(src.depth() <= CV_64F);
|
2010-12-20 17:51:25 +08:00
|
|
|
CV_Assert(src.channels() == 1);
|
2011-01-20 23:08:48 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
if (src.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
if (!TargetArchs::builtWith(NATIVE_DOUBLE) || !DeviceInfo().supports(NATIVE_DOUBLE))
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "The device doesn't support double");
|
|
|
|
}
|
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Size buf_size;
|
|
|
|
getBufSizeRequired(src.cols, src.rows, buf_size.width, buf_size.height);
|
|
|
|
ensureSizeIsEnough(buf_size, CV_8U, buf);
|
|
|
|
|
|
|
|
Caller* callers = multipass_callers;
|
2011-02-15 23:09:54 +08:00
|
|
|
if (TargetArchs::builtWith(GLOBAL_ATOMICS) && DeviceInfo().supports(GLOBAL_ATOMICS))
|
2011-01-19 18:54:58 +08:00
|
|
|
callers = singlepass_callers;
|
2010-12-20 17:51:25 +08:00
|
|
|
|
2011-01-19 18:54:58 +08:00
|
|
|
Caller caller = callers[src.type()];
|
2012-03-26 22:33:43 +08:00
|
|
|
CV_Assert(caller != 0);
|
2010-12-20 17:51:25 +08:00
|
|
|
return caller(src, buf);
|
|
|
|
}
|
|
|
|
|
2011-09-22 15:08:26 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// reduce
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace cv { namespace gpu { namespace device
|
2011-11-09 21:13:52 +08:00
|
|
|
{
|
2012-03-26 22:33:43 +08:00
|
|
|
namespace matrix_reductions
|
2011-11-14 17:02:06 +08:00
|
|
|
{
|
2012-08-23 21:45:50 +08:00
|
|
|
template <typename T, typename S, typename D> void reduceRows_gpu(const PtrStepSzb& src, const PtrStepSzb& dst, int reduceOp, cudaStream_t stream);
|
|
|
|
template <typename T, typename S, typename D> void reduceCols_gpu(const PtrStepSzb& src, int cn, const PtrStepSzb& dst, int reduceOp, cudaStream_t stream);
|
2011-11-14 17:02:06 +08:00
|
|
|
}
|
|
|
|
}}}
|
2011-09-22 15:08:26 +08:00
|
|
|
|
|
|
|
void cv::gpu::reduce(const GpuMat& src, GpuMat& dst, int dim, int reduceOp, int dtype, Stream& stream)
|
|
|
|
{
|
2011-11-14 17:02:06 +08:00
|
|
|
using namespace ::cv::gpu::device::matrix_reductions;
|
2011-11-09 21:13:52 +08:00
|
|
|
|
2011-09-22 15:08:26 +08:00
|
|
|
CV_Assert(src.depth() <= CV_32F && src.channels() <= 4 && dtype <= CV_32F);
|
|
|
|
CV_Assert(dim == 0 || dim == 1);
|
|
|
|
CV_Assert(reduceOp == CV_REDUCE_SUM || reduceOp == CV_REDUCE_AVG || reduceOp == CV_REDUCE_MAX || reduceOp == CV_REDUCE_MIN);
|
|
|
|
|
|
|
|
if (dtype < 0)
|
|
|
|
dtype = src.depth();
|
|
|
|
|
|
|
|
dst.create(1, dim == 0 ? src.cols : src.rows, CV_MAKETYPE(dtype, src.channels()));
|
|
|
|
|
|
|
|
if (dim == 0)
|
|
|
|
{
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*caller_t)(const PtrStepSzb& src, const PtrStepSzb& dst, int reduceOp, cudaStream_t stream);
|
2011-09-22 15:08:26 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static const caller_t callers[6][6] =
|
2011-09-22 15:08:26 +08:00
|
|
|
{
|
|
|
|
{
|
|
|
|
reduceRows_gpu<unsigned char, int, unsigned char>,
|
|
|
|
0/*reduceRows_gpu<unsigned char, int, signed char>*/,
|
|
|
|
0/*reduceRows_gpu<unsigned char, int, unsigned short>*/,
|
|
|
|
0/*reduceRows_gpu<unsigned char, int, short>*/,
|
|
|
|
reduceRows_gpu<unsigned char, int, int>,
|
|
|
|
reduceRows_gpu<unsigned char, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceRows_gpu<signed char, int, unsigned char>*/,
|
|
|
|
0/*reduceRows_gpu<signed char, int, signed char>*/,
|
|
|
|
0/*reduceRows_gpu<signed char, int, unsigned short>*/,
|
|
|
|
0/*reduceRows_gpu<signed char, int, short>*/,
|
|
|
|
0/*reduceRows_gpu<signed char, int, int>*/,
|
|
|
|
0/*reduceRows_gpu<signed char, int, float>*/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceRows_gpu<unsigned short, int, unsigned char>*/,
|
|
|
|
0/*reduceRows_gpu<unsigned short, int, signed char>*/,
|
|
|
|
reduceRows_gpu<unsigned short, int, unsigned short>,
|
|
|
|
0/*reduceRows_gpu<unsigned short, int, short>*/,
|
|
|
|
reduceRows_gpu<unsigned short, int, int>,
|
|
|
|
reduceRows_gpu<unsigned short, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceRows_gpu<short, int, unsigned char>*/,
|
|
|
|
0/*reduceRows_gpu<short, int, signed char>*/,
|
|
|
|
0/*reduceRows_gpu<short, int, unsigned short>*/,
|
|
|
|
reduceRows_gpu<short, int, short>,
|
|
|
|
reduceRows_gpu<short, int, int>,
|
|
|
|
reduceRows_gpu<short, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceRows_gpu<int, int, unsigned char>*/,
|
|
|
|
0/*reduceRows_gpu<int, int, signed char>*/,
|
|
|
|
0/*reduceRows_gpu<int, int, unsigned short>*/,
|
|
|
|
0/*reduceRows_gpu<int, int, short>*/,
|
|
|
|
reduceRows_gpu<int, int, int>,
|
|
|
|
reduceRows_gpu<int, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceRows_gpu<float, float, unsigned char>*/,
|
|
|
|
0/*reduceRows_gpu<float, float, signed char>*/,
|
|
|
|
0/*reduceRows_gpu<float, float, unsigned short>*/,
|
|
|
|
0/*reduceRows_gpu<float, float, short>*/,
|
|
|
|
0/*reduceRows_gpu<float, float, int>*/,
|
|
|
|
reduceRows_gpu<float, float, float>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const caller_t func = callers[src.depth()][dst.depth()];
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-09-22 15:08:26 +08:00
|
|
|
if (!func)
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "Unsupported combination of input and output array formats");
|
|
|
|
|
|
|
|
func(src.reshape(1), dst.reshape(1), reduceOp, StreamAccessor::getStream(stream));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-23 21:45:50 +08:00
|
|
|
typedef void (*caller_t)(const PtrStepSzb& src, int cn, const PtrStepSzb& dst, int reduceOp, cudaStream_t stream);
|
2011-09-22 15:08:26 +08:00
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
static const caller_t callers[6][6] =
|
2011-09-22 15:08:26 +08:00
|
|
|
{
|
|
|
|
{
|
|
|
|
reduceCols_gpu<unsigned char, int, unsigned char>,
|
|
|
|
0/*reduceCols_gpu<unsigned char, int, signed char>*/,
|
|
|
|
0/*reduceCols_gpu<unsigned char, int, unsigned short>*/,
|
|
|
|
0/*reduceCols_gpu<unsigned char, int, short>*/,
|
|
|
|
reduceCols_gpu<unsigned char, int, int>,
|
|
|
|
reduceCols_gpu<unsigned char, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceCols_gpu<signed char, int, unsigned char>*/,
|
|
|
|
0/*reduceCols_gpu<signed char, int, signed char>*/,
|
|
|
|
0/*reduceCols_gpu<signed char, int, unsigned short>*/,
|
|
|
|
0/*reduceCols_gpu<signed char, int, short>*/,
|
|
|
|
0/*reduceCols_gpu<signed char, int, int>*/,
|
|
|
|
0/*reduceCols_gpu<signed char, int, float>*/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceCols_gpu<unsigned short, int, unsigned char>*/,
|
|
|
|
0/*reduceCols_gpu<unsigned short, int, signed char>*/,
|
|
|
|
reduceCols_gpu<unsigned short, int, unsigned short>,
|
|
|
|
0/*reduceCols_gpu<unsigned short, int, short>*/,
|
|
|
|
reduceCols_gpu<unsigned short, int, int>,
|
|
|
|
reduceCols_gpu<unsigned short, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceCols_gpu<short, int, unsigned char>*/,
|
|
|
|
0/*reduceCols_gpu<short, int, signed char>*/,
|
|
|
|
0/*reduceCols_gpu<short, int, unsigned short>*/,
|
|
|
|
reduceCols_gpu<short, int, short>,
|
|
|
|
reduceCols_gpu<short, int, int>,
|
|
|
|
reduceCols_gpu<short, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceCols_gpu<int, int, unsigned char>*/,
|
|
|
|
0/*reduceCols_gpu<int, int, signed char>*/,
|
|
|
|
0/*reduceCols_gpu<int, int, unsigned short>*/,
|
|
|
|
0/*reduceCols_gpu<int, int, short>*/,
|
|
|
|
reduceCols_gpu<int, int, int>,
|
|
|
|
reduceCols_gpu<int, int, float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*reduceCols_gpu<float, unsigned char>*/,
|
|
|
|
0/*reduceCols_gpu<float, signed char>*/,
|
|
|
|
0/*reduceCols_gpu<float, unsigned short>*/,
|
|
|
|
0/*reduceCols_gpu<float, short>*/,
|
|
|
|
0/*reduceCols_gpu<float, int>*/,
|
|
|
|
reduceCols_gpu<float, float, float>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const caller_t func = callers[src.depth()][dst.depth()];
|
2012-03-26 22:33:43 +08:00
|
|
|
|
2011-09-22 15:08:26 +08:00
|
|
|
if (!func)
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "Unsupported combination of input and output array formats");
|
|
|
|
|
2012-03-26 22:33:43 +08:00
|
|
|
func(src, src.channels(), dst, reduceOp, StreamAccessor::getStream(stream));
|
2011-09-22 15:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:51:25 +08:00
|
|
|
#endif
|