vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e
This commit is contained in:
@@ -0,0 +1,602 @@
|
||||
/*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"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
void cv::cuda::gemm(InputArray, InputArray, double, InputArray, double, OutputArray, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::mulSpectrums(InputArray, InputArray, OutputArray, int, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::mulAndScaleSpectrums(InputArray, InputArray, OutputArray, int, float, bool, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::dft(InputArray, OutputArray, Size, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
Ptr<DFT> cv::cuda::createDFT(Size, int) { throw_no_cuda(); return Ptr<DFT>(); }
|
||||
|
||||
Ptr<Convolution> cv::cuda::createConvolution(Size) { throw_no_cuda(); return Ptr<Convolution>(); }
|
||||
|
||||
#else /* !defined (HAVE_CUDA) */
|
||||
|
||||
namespace
|
||||
{
|
||||
#define error_entry(entry) { entry, #entry }
|
||||
|
||||
struct ErrorEntry
|
||||
{
|
||||
int code;
|
||||
const char* str;
|
||||
};
|
||||
|
||||
struct ErrorEntryComparer
|
||||
{
|
||||
int code;
|
||||
ErrorEntryComparer(int code_) : code(code_) {}
|
||||
bool operator()(const ErrorEntry& e) const { return e.code == code; }
|
||||
};
|
||||
|
||||
String getErrorString(int code, const ErrorEntry* errors, size_t n)
|
||||
{
|
||||
size_t idx = std::find_if(errors, errors + n, ErrorEntryComparer(code)) - errors;
|
||||
|
||||
const char* msg = (idx != n) ? errors[idx].str : "Unknown error code";
|
||||
String str = cv::format("%s [Code = %d]", msg, code);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CUBLAS
|
||||
namespace
|
||||
{
|
||||
const ErrorEntry cublas_errors[] =
|
||||
{
|
||||
error_entry( CUBLAS_STATUS_SUCCESS ),
|
||||
error_entry( CUBLAS_STATUS_NOT_INITIALIZED ),
|
||||
error_entry( CUBLAS_STATUS_ALLOC_FAILED ),
|
||||
error_entry( CUBLAS_STATUS_INVALID_VALUE ),
|
||||
error_entry( CUBLAS_STATUS_ARCH_MISMATCH ),
|
||||
error_entry( CUBLAS_STATUS_MAPPING_ERROR ),
|
||||
error_entry( CUBLAS_STATUS_EXECUTION_FAILED ),
|
||||
error_entry( CUBLAS_STATUS_INTERNAL_ERROR )
|
||||
};
|
||||
|
||||
const size_t cublas_error_num = sizeof(cublas_errors) / sizeof(cublas_errors[0]);
|
||||
|
||||
static inline void ___cublasSafeCall(cublasStatus_t err, const char* file, const int line, const char* func)
|
||||
{
|
||||
if (CUBLAS_STATUS_SUCCESS != err)
|
||||
{
|
||||
String msg = getErrorString(err, cublas_errors, cublas_error_num);
|
||||
cv::error(cv::Error::GpuApiCallError, msg, func, file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define cublasSafeCall(expr) ___cublasSafeCall(expr, __FILE__, __LINE__, CV_Func)
|
||||
#endif // HAVE_CUBLAS
|
||||
|
||||
#ifdef HAVE_CUFFT
|
||||
namespace
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// CUFFT errors
|
||||
|
||||
const ErrorEntry cufft_errors[] =
|
||||
{
|
||||
error_entry( CUFFT_INVALID_PLAN ),
|
||||
error_entry( CUFFT_ALLOC_FAILED ),
|
||||
error_entry( CUFFT_INVALID_TYPE ),
|
||||
error_entry( CUFFT_INVALID_VALUE ),
|
||||
error_entry( CUFFT_INTERNAL_ERROR ),
|
||||
error_entry( CUFFT_EXEC_FAILED ),
|
||||
error_entry( CUFFT_SETUP_FAILED ),
|
||||
error_entry( CUFFT_INVALID_SIZE ),
|
||||
error_entry( CUFFT_UNALIGNED_DATA )
|
||||
};
|
||||
|
||||
const int cufft_error_num = sizeof(cufft_errors) / sizeof(cufft_errors[0]);
|
||||
|
||||
void ___cufftSafeCall(int err, const char* file, const int line, const char* func)
|
||||
{
|
||||
if (CUFFT_SUCCESS != err)
|
||||
{
|
||||
String msg = getErrorString(err, cufft_errors, cufft_error_num);
|
||||
cv::error(cv::Error::GpuApiCallError, msg, func, file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define cufftSafeCall(expr) ___cufftSafeCall(expr, __FILE__, __LINE__, CV_Func)
|
||||
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// gemm
|
||||
|
||||
void cv::cuda::gemm(InputArray _src1, InputArray _src2, double alpha, InputArray _src3, double beta, OutputArray _dst, int flags, Stream& stream)
|
||||
{
|
||||
#ifndef HAVE_CUBLAS
|
||||
CV_UNUSED(_src1);
|
||||
CV_UNUSED(_src2);
|
||||
CV_UNUSED(alpha);
|
||||
CV_UNUSED(_src3);
|
||||
CV_UNUSED(beta);
|
||||
CV_UNUSED(_dst);
|
||||
CV_UNUSED(flags);
|
||||
CV_UNUSED(stream);
|
||||
CV_Error(Error::StsNotImplemented, "The library was build without CUBLAS");
|
||||
#else
|
||||
// CUBLAS works with column-major matrices
|
||||
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
GpuMat src3 = getInputMat(_src3, stream);
|
||||
|
||||
CV_Assert( src1.type() == CV_32FC1 || src1.type() == CV_32FC2 || src1.type() == CV_64FC1 || src1.type() == CV_64FC2 );
|
||||
CV_Assert( src2.type() == src1.type() && (src3.empty() || src3.type() == src1.type()) );
|
||||
|
||||
if (src1.depth() == CV_64F)
|
||||
{
|
||||
if (!deviceSupports(NATIVE_DOUBLE))
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "The device doesn't support double");
|
||||
}
|
||||
|
||||
bool tr1 = (flags & GEMM_1_T) != 0;
|
||||
bool tr2 = (flags & GEMM_2_T) != 0;
|
||||
bool tr3 = (flags & GEMM_3_T) != 0;
|
||||
|
||||
if (src1.type() == CV_64FC2)
|
||||
{
|
||||
if (tr1 || tr2 || tr3)
|
||||
CV_Error(cv::Error::StsNotImplemented, "transpose operation doesn't implemented for CV_64FC2 type");
|
||||
}
|
||||
|
||||
Size src1Size = tr1 ? Size(src1.rows, src1.cols) : src1.size();
|
||||
Size src2Size = tr2 ? Size(src2.rows, src2.cols) : src2.size();
|
||||
Size src3Size = tr3 ? Size(src3.rows, src3.cols) : src3.size();
|
||||
Size dstSize(src2Size.width, src1Size.height);
|
||||
|
||||
CV_Assert( src1Size.width == src2Size.height );
|
||||
CV_Assert( src3.empty() || src3Size == dstSize );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, dstSize, src1.type(), stream);
|
||||
|
||||
if (beta != 0)
|
||||
{
|
||||
if (src3.empty())
|
||||
{
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tr3)
|
||||
{
|
||||
cuda::transpose(src3, dst, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
src3.copyTo(dst, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cublasHandle_t handle;
|
||||
cublasSafeCall( cublasCreate_v2(&handle) );
|
||||
|
||||
cublasSafeCall( cublasSetStream_v2(handle, StreamAccessor::getStream(stream)) );
|
||||
|
||||
cublasSafeCall( cublasSetPointerMode_v2(handle, CUBLAS_POINTER_MODE_HOST) );
|
||||
|
||||
const float alphaf = static_cast<float>(alpha);
|
||||
const float betaf = static_cast<float>(beta);
|
||||
|
||||
const cuComplex alphacf = make_cuComplex(alphaf, 0);
|
||||
const cuComplex betacf = make_cuComplex(betaf, 0);
|
||||
|
||||
const cuDoubleComplex alphac = make_cuDoubleComplex(alpha, 0);
|
||||
const cuDoubleComplex betac = make_cuDoubleComplex(beta, 0);
|
||||
|
||||
cublasOperation_t transa = tr2 ? CUBLAS_OP_T : CUBLAS_OP_N;
|
||||
cublasOperation_t transb = tr1 ? CUBLAS_OP_T : CUBLAS_OP_N;
|
||||
|
||||
switch (src1.type())
|
||||
{
|
||||
case CV_32FC1:
|
||||
cublasSafeCall( cublasSgemm_v2(handle, transa, transb, tr2 ? src2.rows : src2.cols, tr1 ? src1.cols : src1.rows, tr2 ? src2.cols : src2.rows,
|
||||
&alphaf,
|
||||
src2.ptr<float>(), static_cast<int>(src2.step / sizeof(float)),
|
||||
src1.ptr<float>(), static_cast<int>(src1.step / sizeof(float)),
|
||||
&betaf,
|
||||
dst.ptr<float>(), static_cast<int>(dst.step / sizeof(float))) );
|
||||
break;
|
||||
|
||||
case CV_64FC1:
|
||||
cublasSafeCall( cublasDgemm_v2(handle, transa, transb, tr2 ? src2.rows : src2.cols, tr1 ? src1.cols : src1.rows, tr2 ? src2.cols : src2.rows,
|
||||
&alpha,
|
||||
src2.ptr<double>(), static_cast<int>(src2.step / sizeof(double)),
|
||||
src1.ptr<double>(), static_cast<int>(src1.step / sizeof(double)),
|
||||
&beta,
|
||||
dst.ptr<double>(), static_cast<int>(dst.step / sizeof(double))) );
|
||||
break;
|
||||
|
||||
case CV_32FC2:
|
||||
cublasSafeCall( cublasCgemm_v2(handle, transa, transb, tr2 ? src2.rows : src2.cols, tr1 ? src1.cols : src1.rows, tr2 ? src2.cols : src2.rows,
|
||||
&alphacf,
|
||||
src2.ptr<cuComplex>(), static_cast<int>(src2.step / sizeof(cuComplex)),
|
||||
src1.ptr<cuComplex>(), static_cast<int>(src1.step / sizeof(cuComplex)),
|
||||
&betacf,
|
||||
dst.ptr<cuComplex>(), static_cast<int>(dst.step / sizeof(cuComplex))) );
|
||||
break;
|
||||
|
||||
case CV_64FC2:
|
||||
cublasSafeCall( cublasZgemm_v2(handle, transa, transb, tr2 ? src2.rows : src2.cols, tr1 ? src1.cols : src1.rows, tr2 ? src2.cols : src2.rows,
|
||||
&alphac,
|
||||
src2.ptr<cuDoubleComplex>(), static_cast<int>(src2.step / sizeof(cuDoubleComplex)),
|
||||
src1.ptr<cuDoubleComplex>(), static_cast<int>(src1.step / sizeof(cuDoubleComplex)),
|
||||
&betac,
|
||||
dst.ptr<cuDoubleComplex>(), static_cast<int>(dst.step / sizeof(cuDoubleComplex))) );
|
||||
break;
|
||||
}
|
||||
|
||||
cublasSafeCall( cublasDestroy_v2(handle) );
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
#endif
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// DFT function
|
||||
|
||||
void cv::cuda::dft(InputArray _src, OutputArray _dst, Size dft_size, int flags, Stream& stream)
|
||||
{
|
||||
if (getInputMat(_src, stream).channels() == 2)
|
||||
flags |= DFT_COMPLEX_INPUT;
|
||||
|
||||
Ptr<DFT> dft = createDFT(dft_size, flags);
|
||||
dft->compute(_src, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// DFT algorithm
|
||||
|
||||
#ifdef HAVE_CUFFT
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class DFTImpl : public DFT
|
||||
{
|
||||
Size dft_size, dft_size_opt;
|
||||
bool is_1d_input, is_row_dft, is_scaled_dft, is_inverse, is_complex_input, is_complex_output;
|
||||
|
||||
cufftType dft_type;
|
||||
cufftHandle plan;
|
||||
|
||||
public:
|
||||
DFTImpl(Size dft_size, int flags)
|
||||
: dft_size(dft_size),
|
||||
dft_size_opt(dft_size),
|
||||
is_1d_input((dft_size.height == 1) || (dft_size.width == 1)),
|
||||
is_row_dft((flags & DFT_ROWS) != 0),
|
||||
is_scaled_dft((flags & DFT_SCALE) != 0),
|
||||
is_inverse((flags & DFT_INVERSE) != 0),
|
||||
is_complex_input((flags & DFT_COMPLEX_INPUT) != 0),
|
||||
is_complex_output(!(flags & DFT_REAL_OUTPUT)),
|
||||
dft_type(!is_complex_input ? CUFFT_R2C : (is_complex_output ? CUFFT_C2C : CUFFT_C2R))
|
||||
{
|
||||
// We don't support unpacked output (in the case of real input)
|
||||
CV_Assert( !(flags & DFT_COMPLEX_OUTPUT) );
|
||||
|
||||
// We don't support real-to-real transform
|
||||
CV_Assert( is_complex_input || is_complex_output );
|
||||
|
||||
if (is_1d_input && !is_row_dft)
|
||||
{
|
||||
// If the source matrix is single column handle it as single row
|
||||
dft_size_opt.width = std::max(dft_size.width, dft_size.height);
|
||||
dft_size_opt.height = std::min(dft_size.width, dft_size.height);
|
||||
}
|
||||
|
||||
CV_Assert( dft_size_opt.width > 1 );
|
||||
|
||||
if (is_1d_input || is_row_dft)
|
||||
cufftSafeCall( cufftPlan1d(&plan, dft_size_opt.width, dft_type, dft_size_opt.height) );
|
||||
else
|
||||
cufftSafeCall( cufftPlan2d(&plan, dft_size_opt.height, dft_size_opt.width, dft_type) );
|
||||
}
|
||||
|
||||
~DFTImpl()
|
||||
{
|
||||
cufftSafeCall( cufftDestroy(plan) );
|
||||
}
|
||||
|
||||
void compute(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.type() == CV_32FC1 || src.type() == CV_32FC2 );
|
||||
CV_Assert( is_complex_input == (src.channels() == 2) );
|
||||
|
||||
// Make sure here we work with the continuous input,
|
||||
// as CUFFT can't handle gaps
|
||||
GpuMat src_cont;
|
||||
if (src.isContinuous())
|
||||
{
|
||||
src_cont = src;
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferPool pool(stream);
|
||||
src_cont.allocator = pool.getAllocator();
|
||||
createContinuous(src.rows, src.cols, src.type(), src_cont);
|
||||
src.copyTo(src_cont, stream);
|
||||
}
|
||||
|
||||
cufftSafeCall( cufftSetStream(plan, StreamAccessor::getStream(stream)) );
|
||||
|
||||
if (is_complex_input)
|
||||
{
|
||||
if (is_complex_output)
|
||||
{
|
||||
createContinuous(dft_size, CV_32FC2, _dst);
|
||||
GpuMat dst = _dst.getGpuMat();
|
||||
|
||||
cufftSafeCall(cufftExecC2C(
|
||||
plan, src_cont.ptr<cufftComplex>(), dst.ptr<cufftComplex>(),
|
||||
is_inverse ? CUFFT_INVERSE : CUFFT_FORWARD));
|
||||
}
|
||||
else
|
||||
{
|
||||
createContinuous(dft_size, CV_32F, _dst);
|
||||
GpuMat dst = _dst.getGpuMat();
|
||||
|
||||
cufftSafeCall(cufftExecC2R(
|
||||
plan, src_cont.ptr<cufftComplex>(), dst.ptr<cufftReal>()));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We could swap dft_size for efficiency. Here we must reflect it
|
||||
if (dft_size == dft_size_opt)
|
||||
createContinuous(Size(dft_size.width / 2 + 1, dft_size.height), CV_32FC2, _dst);
|
||||
else
|
||||
createContinuous(Size(dft_size.width, dft_size.height / 2 + 1), CV_32FC2, _dst);
|
||||
|
||||
GpuMat dst = _dst.getGpuMat();
|
||||
|
||||
cufftSafeCall(cufftExecR2C(
|
||||
plan, src_cont.ptr<cufftReal>(), dst.ptr<cufftComplex>()));
|
||||
}
|
||||
|
||||
if (is_scaled_dft)
|
||||
cuda::multiply(_dst, Scalar::all(1. / dft_size.area()), _dst, 1, -1, stream);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Ptr<DFT> cv::cuda::createDFT(Size dft_size, int flags)
|
||||
{
|
||||
#ifndef HAVE_CUFFT
|
||||
CV_UNUSED(dft_size);
|
||||
CV_UNUSED(flags);
|
||||
CV_Error(Error::StsNotImplemented, "The library was build without CUFFT");
|
||||
return Ptr<DFT>();
|
||||
#else
|
||||
return makePtr<DFTImpl>(dft_size, flags);
|
||||
#endif
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Convolution
|
||||
|
||||
#ifdef HAVE_CUFFT
|
||||
|
||||
namespace
|
||||
{
|
||||
class ConvolutionImpl : public Convolution
|
||||
{
|
||||
public:
|
||||
explicit ConvolutionImpl(Size user_block_size_) : user_block_size(user_block_size_), planR2C(0), planC2R(0) {}
|
||||
~ConvolutionImpl();
|
||||
|
||||
void convolve(InputArray image, InputArray templ, OutputArray result, bool ccorr = false, Stream& stream = Stream::Null());
|
||||
|
||||
private:
|
||||
void create(Size image_size, Size templ_size);
|
||||
static Size estimateBlockSize(Size result_size);
|
||||
|
||||
Size result_size;
|
||||
Size block_size;
|
||||
Size user_block_size;
|
||||
Size dft_size;
|
||||
|
||||
cufftHandle planR2C, planC2R;
|
||||
Size plan_size;
|
||||
|
||||
GpuMat image_spect, templ_spect, result_spect;
|
||||
GpuMat image_block, templ_block, result_data;
|
||||
};
|
||||
|
||||
void ConvolutionImpl::create(Size image_size, Size templ_size)
|
||||
{
|
||||
result_size = Size(image_size.width - templ_size.width + 1,
|
||||
image_size.height - templ_size.height + 1);
|
||||
|
||||
block_size = user_block_size;
|
||||
if (user_block_size.width == 0 || user_block_size.height == 0)
|
||||
block_size = estimateBlockSize(result_size);
|
||||
|
||||
dft_size.width = 1 << int(ceil(std::log(block_size.width + templ_size.width - 1.) / std::log(2.)));
|
||||
dft_size.height = 1 << int(ceil(std::log(block_size.height + templ_size.height - 1.) / std::log(2.)));
|
||||
|
||||
// CUFFT has hard-coded kernels for power-of-2 sizes (up to 8192),
|
||||
// see CUDA Toolkit 4.1 CUFFT Library Programming Guide
|
||||
if (dft_size.width > 8192)
|
||||
dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1);
|
||||
if (dft_size.height > 8192)
|
||||
dft_size.height = getOptimalDFTSize(block_size.height + templ_size.height - 1);
|
||||
|
||||
// To avoid wasting time doing small DFTs
|
||||
dft_size.width = std::max(dft_size.width, 512);
|
||||
dft_size.height = std::max(dft_size.height, 512);
|
||||
|
||||
createContinuous(dft_size, CV_32F, image_block);
|
||||
createContinuous(dft_size, CV_32F, templ_block);
|
||||
createContinuous(dft_size, CV_32F, result_data);
|
||||
|
||||
int spect_len = dft_size.height * (dft_size.width / 2 + 1);
|
||||
createContinuous(1, spect_len, CV_32FC2, image_spect);
|
||||
createContinuous(1, spect_len, CV_32FC2, templ_spect);
|
||||
createContinuous(1, spect_len, CV_32FC2, result_spect);
|
||||
|
||||
// Use maximum result matrix block size for the estimated DFT block size
|
||||
block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
|
||||
block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);
|
||||
|
||||
if (dft_size != plan_size)
|
||||
{
|
||||
if (planR2C != 0)
|
||||
cufftSafeCall( cufftDestroy(planR2C) );
|
||||
if (planC2R != 0)
|
||||
cufftSafeCall( cufftDestroy(planC2R) );
|
||||
|
||||
cufftSafeCall( cufftPlan2d(&planC2R, dft_size.height, dft_size.width, CUFFT_C2R) );
|
||||
cufftSafeCall( cufftPlan2d(&planR2C, dft_size.height, dft_size.width, CUFFT_R2C) );
|
||||
|
||||
plan_size = dft_size;
|
||||
}
|
||||
}
|
||||
|
||||
ConvolutionImpl::~ConvolutionImpl()
|
||||
{
|
||||
if (planR2C != 0)
|
||||
cufftSafeCall( cufftDestroy(planR2C) );
|
||||
if (planC2R != 0)
|
||||
cufftSafeCall( cufftDestroy(planC2R) );
|
||||
}
|
||||
|
||||
Size ConvolutionImpl::estimateBlockSize(Size result_size)
|
||||
{
|
||||
int width = (result_size.width + 2) / 3;
|
||||
int height = (result_size.height + 2) / 3;
|
||||
width = std::min(width, result_size.width);
|
||||
height = std::min(height, result_size.height);
|
||||
return Size(width, height);
|
||||
}
|
||||
|
||||
void ConvolutionImpl::convolve(InputArray _image, InputArray _templ, OutputArray _result, bool ccorr, Stream& _stream)
|
||||
{
|
||||
GpuMat image = getInputMat(_image, _stream);
|
||||
GpuMat templ = getInputMat(_templ, _stream);
|
||||
|
||||
CV_Assert( image.type() == CV_32FC1 );
|
||||
CV_Assert( templ.type() == CV_32FC1 );
|
||||
|
||||
create(image.size(), templ.size());
|
||||
|
||||
GpuMat result = getOutputMat(_result, result_size, CV_32FC1, _stream);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
|
||||
cufftSafeCall( cufftSetStream(planR2C, stream) );
|
||||
cufftSafeCall( cufftSetStream(planC2R, stream) );
|
||||
|
||||
GpuMat templ_roi(templ.size(), CV_32FC1, templ.data, templ.step);
|
||||
cuda::copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0,
|
||||
templ_block.cols - templ_roi.cols, 0, Scalar(), _stream);
|
||||
|
||||
cufftSafeCall( cufftExecR2C(planR2C, templ_block.ptr<cufftReal>(), templ_spect.ptr<cufftComplex>()) );
|
||||
|
||||
// Process all blocks of the result matrix
|
||||
for (int y = 0; y < result.rows; y += block_size.height)
|
||||
{
|
||||
for (int x = 0; x < result.cols; x += block_size.width)
|
||||
{
|
||||
Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,
|
||||
std::min(y + dft_size.height, image.rows) - y);
|
||||
GpuMat image_roi(image_roi_size, CV_32F, (void*)(image.ptr<float>(y) + x),
|
||||
image.step);
|
||||
cuda::copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,
|
||||
0, image_block.cols - image_roi.cols, 0, Scalar(), _stream);
|
||||
|
||||
cufftSafeCall(cufftExecR2C(planR2C, image_block.ptr<cufftReal>(),
|
||||
image_spect.ptr<cufftComplex>()));
|
||||
cuda::mulAndScaleSpectrums(image_spect, templ_spect, result_spect, 0,
|
||||
1.f / dft_size.area(), ccorr, _stream);
|
||||
cufftSafeCall(cufftExecC2R(planC2R, result_spect.ptr<cufftComplex>(),
|
||||
result_data.ptr<cufftReal>()));
|
||||
|
||||
Size result_roi_size(std::min(x + block_size.width, result.cols) - x,
|
||||
std::min(y + block_size.height, result.rows) - y);
|
||||
GpuMat result_roi(result_roi_size, result.type(),
|
||||
(void*)(result.ptr<float>(y) + x), result.step);
|
||||
GpuMat result_block(result_roi_size, result_data.type(),
|
||||
result_data.ptr(), result_data.step);
|
||||
|
||||
result_block.copyTo(result_roi, _stream);
|
||||
}
|
||||
}
|
||||
|
||||
syncOutput(result, _result, _stream);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Ptr<Convolution> cv::cuda::createConvolution(Size user_block_size)
|
||||
{
|
||||
#ifndef HAVE_CUFFT
|
||||
CV_UNUSED(user_block_size);
|
||||
CV_Error(Error::StsNotImplemented, "The library was build without CUFFT");
|
||||
return Ptr<Convolution>();
|
||||
#else
|
||||
return makePtr<ConvolutionImpl>(user_block_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* !defined (HAVE_CUDA) */
|
||||
@@ -0,0 +1,216 @@
|
||||
/*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"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
void cv::cuda::merge(const GpuMat*, size_t, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::merge(const std::vector<GpuMat>&, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::split(InputArray, GpuMat*, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::split(InputArray, std::vector<GpuMat>&, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::transpose(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::flip(InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::copyMakeBorder(InputArray, OutputArray, int, int, int, int, int, Scalar, Stream&) { throw_no_cuda(); }
|
||||
|
||||
#else /* !defined (HAVE_CUDA) */
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// flip
|
||||
|
||||
namespace
|
||||
{
|
||||
template<int DEPTH> struct NppTypeTraits;
|
||||
template<> struct NppTypeTraits<CV_8U> { typedef Npp8u npp_t; };
|
||||
template<> struct NppTypeTraits<CV_8S> { typedef Npp8s npp_t; };
|
||||
template<> struct NppTypeTraits<CV_16U> { typedef Npp16u npp_t; };
|
||||
template<> struct NppTypeTraits<CV_16S> { typedef Npp16s npp_t; };
|
||||
template<> struct NppTypeTraits<CV_32S> { typedef Npp32s npp_t; };
|
||||
template<> struct NppTypeTraits<CV_32F> { typedef Npp32f npp_t; };
|
||||
template<> struct NppTypeTraits<CV_64F> { typedef Npp64f npp_t; };
|
||||
|
||||
template <int DEPTH> struct NppMirrorFunc
|
||||
{
|
||||
typedef typename NppTypeTraits<DEPTH>::npp_t npp_t;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus (*func_t)(const npp_t* pSrc, int nSrcStep, npp_t* pDst, int nDstStep, NppiSize oROI, NppiAxis flip, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus(*func_t)(const npp_t* pSrc, int nSrcStep, npp_t* pDst, int nDstStep, NppiSize oROI, NppiAxis flip);
|
||||
#endif
|
||||
};
|
||||
|
||||
template <int DEPTH, typename NppMirrorFunc<DEPTH>::func_t func> struct NppMirror
|
||||
{
|
||||
typedef typename NppMirrorFunc<DEPTH>::npp_t npp_t;
|
||||
|
||||
static void call(const GpuMat& src, GpuMat& dst, int flipCode, cudaStream_t stream)
|
||||
{
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = src.cols;
|
||||
sz.height = src.rows;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall( func(src.ptr<npp_t>(), static_cast<int>(src.step),
|
||||
dst.ptr<npp_t>(), static_cast<int>(dst.step), sz,
|
||||
(flipCode == 0 ? NPP_HORIZONTAL_AXIS : (flipCode > 0 ? NPP_VERTICAL_AXIS : NPP_BOTH_AXIS)), h) );
|
||||
#else
|
||||
nppSafeCall( func(src.ptr<npp_t>(), static_cast<int>(src.step),
|
||||
dst.ptr<npp_t>(), static_cast<int>(dst.step), sz,
|
||||
(flipCode == 0 ? NPP_HORIZONTAL_AXIS : (flipCode > 0 ? NPP_VERTICAL_AXIS : NPP_BOTH_AXIS))) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
};
|
||||
|
||||
template <int DEPTH> struct NppMirrorIFunc
|
||||
{
|
||||
typedef typename NppTypeTraits<DEPTH>::npp_t npp_t;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus (*func_t)(npp_t* pSrcDst, int nSrcDstStep, NppiSize oROI, NppiAxis flip, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus(*func_t)(npp_t* pSrcDst, int nSrcDstStep, NppiSize oROI, NppiAxis flip);
|
||||
#endif
|
||||
};
|
||||
|
||||
template <int DEPTH, typename NppMirrorIFunc<DEPTH>::func_t func> struct NppMirrorI
|
||||
{
|
||||
typedef typename NppMirrorIFunc<DEPTH>::npp_t npp_t;
|
||||
|
||||
static void call(GpuMat& srcDst, int flipCode, cudaStream_t stream)
|
||||
{
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = srcDst.cols;
|
||||
sz.height = srcDst.rows;
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(func(srcDst.ptr<npp_t>(), static_cast<int>(srcDst.step),
|
||||
sz,
|
||||
(flipCode == 0 ? NPP_HORIZONTAL_AXIS : (flipCode > 0 ? NPP_VERTICAL_AXIS : NPP_BOTH_AXIS)), h) );
|
||||
#else
|
||||
nppSafeCall( func(srcDst.ptr<npp_t>(), static_cast<int>(srcDst.step),
|
||||
sz,
|
||||
(flipCode == 0 ? NPP_HORIZONTAL_AXIS : (flipCode > 0 ? NPP_VERTICAL_AXIS : NPP_BOTH_AXIS))) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void cv::cuda::flip(InputArray _src, OutputArray _dst, int flipCode, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat& dst, int flipCode, cudaStream_t stream);
|
||||
static const func_t funcs[6][4] =
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
{NppMirror<CV_8U, nppiMirror_8u_C1R_Ctx>::call, 0, NppMirror<CV_8U, nppiMirror_8u_C3R_Ctx>::call, NppMirror<CV_8U, nppiMirror_8u_C4R_Ctx>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirror<CV_16U, nppiMirror_16u_C1R_Ctx>::call, 0, NppMirror<CV_16U, nppiMirror_16u_C3R_Ctx>::call, NppMirror<CV_16U, nppiMirror_16u_C4R_Ctx>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirror<CV_32S, nppiMirror_32s_C1R_Ctx>::call, 0, NppMirror<CV_32S, nppiMirror_32s_C3R_Ctx>::call, NppMirror<CV_32S, nppiMirror_32s_C4R_Ctx>::call},
|
||||
{NppMirror<CV_32F, nppiMirror_32f_C1R_Ctx>::call, 0, NppMirror<CV_32F, nppiMirror_32f_C3R_Ctx>::call, NppMirror<CV_32F, nppiMirror_32f_C4R_Ctx>::call}
|
||||
#else
|
||||
{ NppMirror<CV_8U, nppiMirror_8u_C1R>::call, 0, NppMirror<CV_8U, nppiMirror_8u_C3R>::call, NppMirror<CV_8U, nppiMirror_8u_C4R>::call },
|
||||
{0,0,0,0},
|
||||
{NppMirror<CV_16U, nppiMirror_16u_C1R>::call, 0, NppMirror<CV_16U, nppiMirror_16u_C3R>::call, NppMirror<CV_16U, nppiMirror_16u_C4R>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirror<CV_32S, nppiMirror_32s_C1R>::call, 0, NppMirror<CV_32S, nppiMirror_32s_C3R>::call, NppMirror<CV_32S, nppiMirror_32s_C4R>::call},
|
||||
{NppMirror<CV_32F, nppiMirror_32f_C1R>::call, 0, NppMirror<CV_32F, nppiMirror_32f_C3R>::call, NppMirror<CV_32F, nppiMirror_32f_C4R>::call}
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef void (*ifunc_t)(GpuMat& srcDst, int flipCode, cudaStream_t stream);
|
||||
static const ifunc_t ifuncs[6][4] =
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
{NppMirrorI<CV_8U, nppiMirror_8u_C1IR_Ctx>::call, 0, NppMirrorI<CV_8U, nppiMirror_8u_C3IR_Ctx>::call, NppMirrorI<CV_8U, nppiMirror_8u_C4IR_Ctx>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirrorI<CV_16U, nppiMirror_16u_C1IR_Ctx>::call, 0, NppMirrorI<CV_16U, nppiMirror_16u_C3IR_Ctx>::call, NppMirrorI<CV_16U, nppiMirror_16u_C4IR_Ctx>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirrorI<CV_32S, nppiMirror_32s_C1IR_Ctx>::call, 0, NppMirrorI<CV_32S, nppiMirror_32s_C3IR_Ctx>::call, NppMirrorI<CV_32S, nppiMirror_32s_C4IR_Ctx>::call},
|
||||
{NppMirrorI<CV_32F, nppiMirror_32f_C1IR_Ctx>::call, 0, NppMirrorI<CV_32F, nppiMirror_32f_C3IR_Ctx>::call, NppMirrorI<CV_32F, nppiMirror_32f_C4IR_Ctx>::call}
|
||||
#else
|
||||
{ NppMirrorI<CV_8U, nppiMirror_8u_C1IR>::call, 0, NppMirrorI<CV_8U, nppiMirror_8u_C3IR>::call, NppMirrorI<CV_8U, nppiMirror_8u_C4IR>::call },
|
||||
{0,0,0,0},
|
||||
{NppMirrorI<CV_16U, nppiMirror_16u_C1IR>::call, 0, NppMirrorI<CV_16U, nppiMirror_16u_C3IR>::call, NppMirrorI<CV_16U, nppiMirror_16u_C4IR>::call},
|
||||
{0,0,0,0},
|
||||
{NppMirrorI<CV_32S, nppiMirror_32s_C1IR>::call, 0, NppMirrorI<CV_32S, nppiMirror_32s_C3IR>::call, NppMirrorI<CV_32S, nppiMirror_32s_C4IR>::call},
|
||||
{NppMirrorI<CV_32F, nppiMirror_32f_C1IR>::call, 0, NppMirrorI<CV_32F, nppiMirror_32f_C3IR>::call, NppMirrorI<CV_32F, nppiMirror_32f_C4IR>::call}
|
||||
#endif
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert(src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32S || src.depth() == CV_32F);
|
||||
CV_Assert(src.channels() == 1 || src.channels() == 3 || src.channels() == 4);
|
||||
|
||||
_dst.create(src.size(), src.type());
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
bool isInplace = (src.data == dst.data);
|
||||
bool isSizeOdd = (src.cols & 1) == 1 || (src.rows & 1) == 1;
|
||||
if (isInplace && isSizeOdd)
|
||||
CV_Error(Error::BadROISize, "In-place version of flip only accepts even width/height");
|
||||
|
||||
if (isInplace == false)
|
||||
funcs[src.depth()][src.channels() - 1](src, dst, flipCode, StreamAccessor::getStream(stream));
|
||||
else // in-place
|
||||
ifuncs[src.depth()][src.channels() - 1](src, flipCode, StreamAccessor::getStream(stream));
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif /* !defined (HAVE_CUDA) */
|
||||
@@ -0,0 +1,188 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
__device__ __forceinline__ int _abs(int a)
|
||||
{
|
||||
return ::abs(a);
|
||||
}
|
||||
__device__ __forceinline__ float _abs(float a)
|
||||
{
|
||||
return ::fabsf(a);
|
||||
}
|
||||
__device__ __forceinline__ double _abs(double a)
|
||||
{
|
||||
return ::fabs(a);
|
||||
}
|
||||
|
||||
template <typename T> struct AbsDiffOp1 : binary_function<T, T, T>
|
||||
{
|
||||
__device__ __forceinline__ T operator ()(T a, T b) const
|
||||
{
|
||||
return saturate_cast<T>(_abs(a - b));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void absDiffMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary_< TransformPolicy<T> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<T>(dst), AbsDiffOp1<T>(), stream);
|
||||
}
|
||||
|
||||
struct AbsDiffOp2 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vabsdiff2(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void absDiffMat_v2(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 1;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, AbsDiffOp2(), stream);
|
||||
}
|
||||
|
||||
struct AbsDiffOp4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vabsdiff4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void absDiffMat_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, AbsDiffOp4(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
absDiffMat_v1<uchar>,
|
||||
absDiffMat_v1<schar>,
|
||||
absDiffMat_v1<ushort>,
|
||||
absDiffMat_v1<short>,
|
||||
absDiffMat_v1<int>,
|
||||
absDiffMat_v1<float>,
|
||||
absDiffMat_v1<double>
|
||||
};
|
||||
|
||||
const int depth = src1.depth();
|
||||
|
||||
CV_DbgAssert( depth <= CV_64F );
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
if (depth == CV_8U || depth == CV_16U)
|
||||
{
|
||||
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
|
||||
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
|
||||
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
|
||||
|
||||
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
|
||||
|
||||
if (isAllAligned)
|
||||
{
|
||||
if (depth == CV_8U && (src1_.cols & 3) == 0)
|
||||
{
|
||||
absDiffMat_v4(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
else if (depth == CV_16U && (src1_.cols & 1) == 0)
|
||||
{
|
||||
absDiffMat_v2(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const func_t func = funcs[depth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_, src2_, dst_, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,135 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/cuda/cuda_compat.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void absDiffScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
using cv::cuda::device::compat::double4Compat;
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct AbsDiffScalarOp : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
abs_func<ScalarType> f;
|
||||
return saturate_cast<DstType>(f(saturate_cast<ScalarType>(a) - val));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarDepth>
|
||||
void absDiffScalarImpl(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<ScalarDepth, VecTraits<SrcType>::cn>::type ScalarType;
|
||||
|
||||
cv::Scalar_<ScalarDepth> value_ = value;
|
||||
|
||||
AbsDiffScalarOp<SrcType, ScalarType, SrcType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<SrcType>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void absDiffScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar val, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX][4] =
|
||||
{
|
||||
{
|
||||
absDiffScalarImpl<uchar, float>, absDiffScalarImpl<uchar2, float>, absDiffScalarImpl<uchar3, float>, absDiffScalarImpl<uchar4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<schar, float>, absDiffScalarImpl<char2, float>, absDiffScalarImpl<char3, float>, absDiffScalarImpl<char4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<ushort, float>, absDiffScalarImpl<ushort2, float>, absDiffScalarImpl<ushort3, float>, absDiffScalarImpl<ushort4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<short, float>, absDiffScalarImpl<short2, float>, absDiffScalarImpl<short3, float>, absDiffScalarImpl<short4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<int, float>, absDiffScalarImpl<int2, float>, absDiffScalarImpl<int3, float>, absDiffScalarImpl<int4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<float, float>, absDiffScalarImpl<float2, float>, absDiffScalarImpl<float3, float>, absDiffScalarImpl<float4, float>
|
||||
},
|
||||
{
|
||||
absDiffScalarImpl<double, double>, absDiffScalarImpl<double2, double>, absDiffScalarImpl<double3, double>, absDiffScalarImpl<double4Compat, double>
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && cn <= 4 && src.type() == dst.type());
|
||||
|
||||
const func_t func = funcs[sdepth][cn - 1];
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, val, dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,225 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void addMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename D> struct AddOp1 : binary_function<T, T, D>
|
||||
{
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return saturate_cast<D>(a + b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename D>
|
||||
void addMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
if (mask.data)
|
||||
gridTransformBinary(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), AddOp1<T, D>(), globPtr<uchar>(mask), stream);
|
||||
else
|
||||
gridTransformBinary(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), AddOp1<T, D>(), stream);
|
||||
}
|
||||
|
||||
struct AddOp2 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vadd2(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void addMat_v2(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 1;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, AddOp2(), stream);
|
||||
}
|
||||
|
||||
struct AddOp4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vadd4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void addMat_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, AddOp4(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void addMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][7] =
|
||||
{
|
||||
{
|
||||
addMat_v1<uchar, uchar>,
|
||||
addMat_v1<uchar, schar>,
|
||||
addMat_v1<uchar, ushort>,
|
||||
addMat_v1<uchar, short>,
|
||||
addMat_v1<uchar, int>,
|
||||
addMat_v1<uchar, float>,
|
||||
addMat_v1<uchar, double>
|
||||
},
|
||||
{
|
||||
addMat_v1<schar, uchar>,
|
||||
addMat_v1<schar, schar>,
|
||||
addMat_v1<schar, ushort>,
|
||||
addMat_v1<schar, short>,
|
||||
addMat_v1<schar, int>,
|
||||
addMat_v1<schar, float>,
|
||||
addMat_v1<schar, double>
|
||||
},
|
||||
{
|
||||
0 /*addMat_v1<ushort, uchar>*/,
|
||||
0 /*addMat_v1<ushort, schar>*/,
|
||||
addMat_v1<ushort, ushort>,
|
||||
addMat_v1<ushort, short>,
|
||||
addMat_v1<ushort, int>,
|
||||
addMat_v1<ushort, float>,
|
||||
addMat_v1<ushort, double>
|
||||
},
|
||||
{
|
||||
0 /*addMat_v1<short, uchar>*/,
|
||||
0 /*addMat_v1<short, schar>*/,
|
||||
addMat_v1<short, ushort>,
|
||||
addMat_v1<short, short>,
|
||||
addMat_v1<short, int>,
|
||||
addMat_v1<short, float>,
|
||||
addMat_v1<short, double>
|
||||
},
|
||||
{
|
||||
0 /*addMat_v1<int, uchar>*/,
|
||||
0 /*addMat_v1<int, schar>*/,
|
||||
0 /*addMat_v1<int, ushort>*/,
|
||||
0 /*addMat_v1<int, short>*/,
|
||||
addMat_v1<int, int>,
|
||||
addMat_v1<int, float>,
|
||||
addMat_v1<int, double>
|
||||
},
|
||||
{
|
||||
0 /*addMat_v1<float, uchar>*/,
|
||||
0 /*addMat_v1<float, schar>*/,
|
||||
0 /*addMat_v1<float, ushort>*/,
|
||||
0 /*addMat_v1<float, short>*/,
|
||||
0 /*addMat_v1<float, int>*/,
|
||||
addMat_v1<float, float>,
|
||||
addMat_v1<float, double>
|
||||
},
|
||||
{
|
||||
0 /*addMat_v1<double, uchar>*/,
|
||||
0 /*addMat_v1<double, schar>*/,
|
||||
0 /*addMat_v1<double, ushort>*/,
|
||||
0 /*addMat_v1<double, short>*/,
|
||||
0 /*addMat_v1<double, int>*/,
|
||||
0 /*addMat_v1<double, float>*/,
|
||||
addMat_v1<double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src1.depth();
|
||||
const int ddepth = dst.depth();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F );
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
if (mask.empty() && (sdepth == CV_8U || sdepth == CV_16U) && ddepth == sdepth)
|
||||
{
|
||||
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
|
||||
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
|
||||
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
|
||||
|
||||
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
|
||||
|
||||
if (isAllAligned)
|
||||
{
|
||||
if (sdepth == CV_8U && (src1_.cols & 3) == 0)
|
||||
{
|
||||
addMat_v4(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
else if (sdepth == CV_16U && (src1_.cols & 1) == 0)
|
||||
{
|
||||
addMat_v2(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_, src2_, dst_, mask, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,182 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/cuda/cuda_compat.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void addScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
using cv::cuda::device::compat::double4Compat;
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct AddScalarOp : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(saturate_cast<ScalarType>(a) + val);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarDepth, typename DstType>
|
||||
void addScalarImpl(const GpuMat& src, cv::Scalar value, GpuMat& dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<ScalarDepth, VecTraits<SrcType>::cn>::type ScalarType;
|
||||
|
||||
cv::Scalar_<ScalarDepth> value_ = value;
|
||||
|
||||
AddScalarOp<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
if (mask.data)
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, globPtr<uchar>(mask), stream);
|
||||
else
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void addScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar val, GpuMat& dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][7][4] =
|
||||
{
|
||||
{
|
||||
{addScalarImpl<uchar, float, uchar>, addScalarImpl<uchar2, float, uchar2>, addScalarImpl<uchar3, float, uchar3>, addScalarImpl<uchar4, float, uchar4>},
|
||||
{addScalarImpl<uchar, float, schar>, addScalarImpl<uchar2, float, char2>, addScalarImpl<uchar3, float, char3>, addScalarImpl<uchar4, float, char4>},
|
||||
{addScalarImpl<uchar, float, ushort>, addScalarImpl<uchar2, float, ushort2>, addScalarImpl<uchar3, float, ushort3>, addScalarImpl<uchar4, float, ushort4>},
|
||||
{addScalarImpl<uchar, float, short>, addScalarImpl<uchar2, float, short2>, addScalarImpl<uchar3, float, short3>, addScalarImpl<uchar4, float, short4>},
|
||||
{addScalarImpl<uchar, float, int>, addScalarImpl<uchar2, float, int2>, addScalarImpl<uchar3, float, int3>, addScalarImpl<uchar4, float, int4>},
|
||||
{addScalarImpl<uchar, float, float>, addScalarImpl<uchar2, float, float2>, addScalarImpl<uchar3, float, float3>, addScalarImpl<uchar4, float, float4>},
|
||||
{addScalarImpl<uchar, double, double>, addScalarImpl<uchar2, double, double2>, addScalarImpl<uchar3, double, double3>, addScalarImpl<uchar4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{addScalarImpl<schar, float, uchar>, addScalarImpl<char2, float, uchar2>, addScalarImpl<char3, float, uchar3>, addScalarImpl<char4, float, uchar4>},
|
||||
{addScalarImpl<schar, float, schar>, addScalarImpl<char2, float, char2>, addScalarImpl<char3, float, char3>, addScalarImpl<char4, float, char4>},
|
||||
{addScalarImpl<schar, float, ushort>, addScalarImpl<char2, float, ushort2>, addScalarImpl<char3, float, ushort3>, addScalarImpl<char4, float, ushort4>},
|
||||
{addScalarImpl<schar, float, short>, addScalarImpl<char2, float, short2>, addScalarImpl<char3, float, short3>, addScalarImpl<char4, float, short4>},
|
||||
{addScalarImpl<schar, float, int>, addScalarImpl<char2, float, int2>, addScalarImpl<char3, float, int3>, addScalarImpl<char4, float, int4>},
|
||||
{addScalarImpl<schar, float, float>, addScalarImpl<char2, float, float2>, addScalarImpl<char3, float, float3>, addScalarImpl<char4, float, float4>},
|
||||
{addScalarImpl<schar, double, double>, addScalarImpl<char2, double, double2>, addScalarImpl<char3, double, double3>, addScalarImpl<char4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*addScalarImpl<ushort, float, uchar>*/, 0 /*addScalarImpl<ushort2, float, uchar2>*/, 0 /*addScalarImpl<ushort3, float, uchar3>*/, 0 /*addScalarImpl<ushort4, float, uchar4>*/},
|
||||
{0 /*addScalarImpl<ushort, float, schar>*/, 0 /*addScalarImpl<ushort2, float, char2>*/, 0 /*addScalarImpl<ushort3, float, char3>*/, 0 /*addScalarImpl<ushort4, float, char4>*/},
|
||||
{addScalarImpl<ushort, float, ushort>, addScalarImpl<ushort2, float, ushort2>, addScalarImpl<ushort3, float, ushort3>, addScalarImpl<ushort4, float, ushort4>},
|
||||
{addScalarImpl<ushort, float, short>, addScalarImpl<ushort2, float, short2>, addScalarImpl<ushort3, float, short3>, addScalarImpl<ushort4, float, short4>},
|
||||
{addScalarImpl<ushort, float, int>, addScalarImpl<ushort2, float, int2>, addScalarImpl<ushort3, float, int3>, addScalarImpl<ushort4, float, int4>},
|
||||
{addScalarImpl<ushort, float, float>, addScalarImpl<ushort2, float, float2>, addScalarImpl<ushort3, float, float3>, addScalarImpl<ushort4, float, float4>},
|
||||
{addScalarImpl<ushort, double, double>, addScalarImpl<ushort2, double, double2>, addScalarImpl<ushort3, double, double3>, addScalarImpl<ushort4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*addScalarImpl<short, float, uchar>*/, 0 /*addScalarImpl<short2, float, uchar2>*/, 0 /*addScalarImpl<short3, float, uchar3>*/, 0 /*addScalarImpl<short4, float, uchar4>*/},
|
||||
{0 /*addScalarImpl<short, float, schar>*/, 0 /*addScalarImpl<short2, float, char2>*/, 0 /*addScalarImpl<short3, float, char3>*/, 0 /*addScalarImpl<short4, float, char4>*/},
|
||||
{addScalarImpl<short, float, ushort>, addScalarImpl<short2, float, ushort2>, addScalarImpl<short3, float, ushort3>, addScalarImpl<short4, float, ushort4>},
|
||||
{addScalarImpl<short, float, short>, addScalarImpl<short2, float, short2>, addScalarImpl<short3, float, short3>, addScalarImpl<short4, float, short4>},
|
||||
{addScalarImpl<short, float, int>, addScalarImpl<short2, float, int2>, addScalarImpl<short3, float, int3>, addScalarImpl<short4, float, int4>},
|
||||
{addScalarImpl<short, float, float>, addScalarImpl<short2, float, float2>, addScalarImpl<short3, float, float3>, addScalarImpl<short4, float, float4>},
|
||||
{addScalarImpl<short, double, double>, addScalarImpl<short2, double, double2>, addScalarImpl<short3, double, double3>, addScalarImpl<short4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*addScalarImpl<int, float, uchar>*/, 0 /*addScalarImpl<int2, float, uchar2>*/, 0 /*addScalarImpl<int3, float, uchar3>*/, 0 /*addScalarImpl<int4, float, uchar4>*/},
|
||||
{0 /*addScalarImpl<int, float, schar>*/, 0 /*addScalarImpl<int2, float, char2>*/, 0 /*addScalarImpl<int3, float, char3>*/, 0 /*addScalarImpl<int4, float, char4>*/},
|
||||
{0 /*addScalarImpl<int, float, ushort>*/, 0 /*addScalarImpl<int2, float, ushort2>*/, 0 /*addScalarImpl<int3, float, ushort3>*/, 0 /*addScalarImpl<int4, float, ushort4>*/},
|
||||
{0 /*addScalarImpl<int, float, short>*/, 0 /*addScalarImpl<int2, float, short2>*/, 0 /*addScalarImpl<int3, float, short3>*/, 0 /*addScalarImpl<int4, float, short4>*/},
|
||||
{addScalarImpl<int, float, int>, addScalarImpl<int2, float, int2>, addScalarImpl<int3, float, int3>, addScalarImpl<int4, float, int4>},
|
||||
{addScalarImpl<int, float, float>, addScalarImpl<int2, float, float2>, addScalarImpl<int3, float, float3>, addScalarImpl<int4, float, float4>},
|
||||
{addScalarImpl<int, double, double>, addScalarImpl<int2, double, double2>, addScalarImpl<int3, double, double3>, addScalarImpl<int4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*addScalarImpl<float, float, uchar>*/, 0 /*addScalarImpl<float2, float, uchar2>*/, 0 /*addScalarImpl<float3, float, uchar3>*/, 0 /*addScalarImpl<float4, float, uchar4>*/},
|
||||
{0 /*addScalarImpl<float, float, schar>*/, 0 /*addScalarImpl<float2, float, char2>*/, 0 /*addScalarImpl<float3, float, char3>*/, 0 /*addScalarImpl<float4, float, char4>*/},
|
||||
{0 /*addScalarImpl<float, float, ushort>*/, 0 /*addScalarImpl<float2, float, ushort2>*/, 0 /*addScalarImpl<float3, float, ushort3>*/, 0 /*addScalarImpl<float4, float, ushort4>*/},
|
||||
{0 /*addScalarImpl<float, float, short>*/, 0 /*addScalarImpl<float2, float, short2>*/, 0 /*addScalarImpl<float3, float, short3>*/, 0 /*addScalarImpl<float4, float, short4>*/},
|
||||
{0 /*addScalarImpl<float, float, int>*/, 0 /*addScalarImpl<float2, float, int2>*/, 0 /*addScalarImpl<float3, float, int3>*/, 0 /*addScalarImpl<float4, float, int4>*/},
|
||||
{addScalarImpl<float, float, float>, addScalarImpl<float2, float, float2>, addScalarImpl<float3, float, float3>, addScalarImpl<float4, float, float4>},
|
||||
{addScalarImpl<float, double, double>, addScalarImpl<float2, double, double2>, addScalarImpl<float3, double, double3>, addScalarImpl<float4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*addScalarImpl<double, double, uchar>*/, 0 /*addScalarImpl<double2, double, uchar2>*/, 0 /*addScalarImpl<double3, double, uchar3>*/, 0 /*addScalarImpl<double4, double, uchar4>*/},
|
||||
{0 /*addScalarImpl<double, double, schar>*/, 0 /*addScalarImpl<double2, double, char2>*/, 0 /*addScalarImpl<double3, double, char3>*/, 0 /*addScalarImpl<double4, double, char4>*/},
|
||||
{0 /*addScalarImpl<double, double, ushort>*/, 0 /*addScalarImpl<double2, double, ushort2>*/, 0 /*addScalarImpl<double3, double, ushort3>*/, 0 /*addScalarImpl<double4, double, ushort4>*/},
|
||||
{0 /*addScalarImpl<double, double, short>*/, 0 /*addScalarImpl<double2, double, short2>*/, 0 /*addScalarImpl<double3, double, short3>*/, 0 /*addScalarImpl<double4, double, short4>*/},
|
||||
{0 /*addScalarImpl<double, double, int>*/, 0 /*addScalarImpl<double2, double, int2>*/, 0 /*addScalarImpl<double3, double, int3>*/, 0 /*addScalarImpl<double4, double, int4>*/},
|
||||
{0 /*addScalarImpl<double, double, float>*/, 0 /*addScalarImpl<double2, double, float2>*/, 0 /*addScalarImpl<double3, double, float3>*/, 0 /*addScalarImpl<double4, double, float4>*/},
|
||||
{addScalarImpl<double, double, double>, addScalarImpl<double2, double, double2>, addScalarImpl<double3, double, double3>, addScalarImpl<double4Compat, double, double4Compat>}
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src.depth();
|
||||
const int ddepth = dst.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F && cn <= 4 );
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth][cn - 1];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, val, dst, mask, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,596 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T1, typename T2, typename D, typename S> struct AddWeightedOp : binary_function<T1, T2, D>
|
||||
{
|
||||
S alpha;
|
||||
S beta;
|
||||
S gamma;
|
||||
|
||||
__device__ __forceinline__ D operator ()(T1 a, T2 b) const
|
||||
{
|
||||
return cudev::saturate_cast<D>(a * alpha + b * beta + gamma);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T1, typename T2, typename D>
|
||||
void addWeightedImpl(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
typedef typename LargerType<T1, T2>::type larger_type1;
|
||||
typedef typename LargerType<larger_type1, D>::type larger_type2;
|
||||
typedef typename LargerType<larger_type2, float>::type scalar_type;
|
||||
|
||||
AddWeightedOp<T1, T2, D, scalar_type> op;
|
||||
op.alpha = static_cast<scalar_type>(alpha);
|
||||
op.beta = static_cast<scalar_type>(beta);
|
||||
op.gamma = static_cast<scalar_type>(gamma);
|
||||
|
||||
gridTransformBinary_< TransformPolicy<scalar_type> >(globPtr<T1>(src1), globPtr<T2>(src2), globPtr<D>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::addWeighted(InputArray _src1, double alpha, InputArray _src2, double beta, double gamma, OutputArray _dst, int ddepth, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[7][7][7] =
|
||||
{
|
||||
{
|
||||
{
|
||||
addWeightedImpl<uchar, uchar, uchar >,
|
||||
addWeightedImpl<uchar, uchar, schar >,
|
||||
addWeightedImpl<uchar, uchar, ushort>,
|
||||
addWeightedImpl<uchar, uchar, short >,
|
||||
addWeightedImpl<uchar, uchar, int >,
|
||||
addWeightedImpl<uchar, uchar, float >,
|
||||
addWeightedImpl<uchar, uchar, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, schar, uchar >,
|
||||
addWeightedImpl<uchar, schar, schar >,
|
||||
addWeightedImpl<uchar, schar, ushort>,
|
||||
addWeightedImpl<uchar, schar, short >,
|
||||
addWeightedImpl<uchar, schar, int >,
|
||||
addWeightedImpl<uchar, schar, float >,
|
||||
addWeightedImpl<uchar, schar, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, ushort, uchar >,
|
||||
addWeightedImpl<uchar, ushort, schar >,
|
||||
addWeightedImpl<uchar, ushort, ushort>,
|
||||
addWeightedImpl<uchar, ushort, short >,
|
||||
addWeightedImpl<uchar, ushort, int >,
|
||||
addWeightedImpl<uchar, ushort, float >,
|
||||
addWeightedImpl<uchar, ushort, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, short, uchar >,
|
||||
addWeightedImpl<uchar, short, schar >,
|
||||
addWeightedImpl<uchar, short, ushort>,
|
||||
addWeightedImpl<uchar, short, short >,
|
||||
addWeightedImpl<uchar, short, int >,
|
||||
addWeightedImpl<uchar, short, float >,
|
||||
addWeightedImpl<uchar, short, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, int, uchar >,
|
||||
addWeightedImpl<uchar, int, schar >,
|
||||
addWeightedImpl<uchar, int, ushort>,
|
||||
addWeightedImpl<uchar, int, short >,
|
||||
addWeightedImpl<uchar, int, int >,
|
||||
addWeightedImpl<uchar, int, float >,
|
||||
addWeightedImpl<uchar, int, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, float, uchar >,
|
||||
addWeightedImpl<uchar, float, schar >,
|
||||
addWeightedImpl<uchar, float, ushort>,
|
||||
addWeightedImpl<uchar, float, short >,
|
||||
addWeightedImpl<uchar, float, int >,
|
||||
addWeightedImpl<uchar, float, float >,
|
||||
addWeightedImpl<uchar, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<uchar, double, uchar >,
|
||||
addWeightedImpl<uchar, double, schar >,
|
||||
addWeightedImpl<uchar, double, ushort>,
|
||||
addWeightedImpl<uchar, double, short >,
|
||||
addWeightedImpl<uchar, double, int >,
|
||||
addWeightedImpl<uchar, double, float >,
|
||||
addWeightedImpl<uchar, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<schar, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<schar, uchar, schar >*/,
|
||||
0/*addWeightedImpl<schar, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<schar, uchar, short >*/,
|
||||
0/*addWeightedImpl<schar, uchar, int >*/,
|
||||
0/*addWeightedImpl<schar, uchar, float >*/,
|
||||
0/*addWeightedImpl<schar, uchar, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, schar, uchar >,
|
||||
addWeightedImpl<schar, schar, schar >,
|
||||
addWeightedImpl<schar, schar, ushort>,
|
||||
addWeightedImpl<schar, schar, short >,
|
||||
addWeightedImpl<schar, schar, int >,
|
||||
addWeightedImpl<schar, schar, float >,
|
||||
addWeightedImpl<schar, schar, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, ushort, uchar >,
|
||||
addWeightedImpl<schar, ushort, schar >,
|
||||
addWeightedImpl<schar, ushort, ushort>,
|
||||
addWeightedImpl<schar, ushort, short >,
|
||||
addWeightedImpl<schar, ushort, int >,
|
||||
addWeightedImpl<schar, ushort, float >,
|
||||
addWeightedImpl<schar, ushort, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, short, uchar >,
|
||||
addWeightedImpl<schar, short, schar >,
|
||||
addWeightedImpl<schar, short, ushort>,
|
||||
addWeightedImpl<schar, short, short >,
|
||||
addWeightedImpl<schar, short, int >,
|
||||
addWeightedImpl<schar, short, float >,
|
||||
addWeightedImpl<schar, short, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, int, uchar >,
|
||||
addWeightedImpl<schar, int, schar >,
|
||||
addWeightedImpl<schar, int, ushort>,
|
||||
addWeightedImpl<schar, int, short >,
|
||||
addWeightedImpl<schar, int, int >,
|
||||
addWeightedImpl<schar, int, float >,
|
||||
addWeightedImpl<schar, int, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, float, uchar >,
|
||||
addWeightedImpl<schar, float, schar >,
|
||||
addWeightedImpl<schar, float, ushort>,
|
||||
addWeightedImpl<schar, float, short >,
|
||||
addWeightedImpl<schar, float, int >,
|
||||
addWeightedImpl<schar, float, float >,
|
||||
addWeightedImpl<schar, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<schar, double, uchar >,
|
||||
addWeightedImpl<schar, double, schar >,
|
||||
addWeightedImpl<schar, double, ushort>,
|
||||
addWeightedImpl<schar, double, short >,
|
||||
addWeightedImpl<schar, double, int >,
|
||||
addWeightedImpl<schar, double, float >,
|
||||
addWeightedImpl<schar, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<ushort, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<ushort, uchar, schar >*/,
|
||||
0/*addWeightedImpl<ushort, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<ushort, uchar, short >*/,
|
||||
0/*addWeightedImpl<ushort, uchar, int >*/,
|
||||
0/*addWeightedImpl<ushort, uchar, float >*/,
|
||||
0/*addWeightedImpl<ushort, uchar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<ushort, schar, uchar >*/,
|
||||
0/*addWeightedImpl<ushort, schar, schar >*/,
|
||||
0/*addWeightedImpl<ushort, schar, ushort>*/,
|
||||
0/*addWeightedImpl<ushort, schar, short >*/,
|
||||
0/*addWeightedImpl<ushort, schar, int >*/,
|
||||
0/*addWeightedImpl<ushort, schar, float >*/,
|
||||
0/*addWeightedImpl<ushort, schar, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<ushort, ushort, uchar >,
|
||||
addWeightedImpl<ushort, ushort, schar >,
|
||||
addWeightedImpl<ushort, ushort, ushort>,
|
||||
addWeightedImpl<ushort, ushort, short >,
|
||||
addWeightedImpl<ushort, ushort, int >,
|
||||
addWeightedImpl<ushort, ushort, float >,
|
||||
addWeightedImpl<ushort, ushort, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<ushort, short, uchar >,
|
||||
addWeightedImpl<ushort, short, schar >,
|
||||
addWeightedImpl<ushort, short, ushort>,
|
||||
addWeightedImpl<ushort, short, short >,
|
||||
addWeightedImpl<ushort, short, int >,
|
||||
addWeightedImpl<ushort, short, float >,
|
||||
addWeightedImpl<ushort, short, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<ushort, int, uchar >,
|
||||
addWeightedImpl<ushort, int, schar >,
|
||||
addWeightedImpl<ushort, int, ushort>,
|
||||
addWeightedImpl<ushort, int, short >,
|
||||
addWeightedImpl<ushort, int, int >,
|
||||
addWeightedImpl<ushort, int, float >,
|
||||
addWeightedImpl<ushort, int, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<ushort, float, uchar >,
|
||||
addWeightedImpl<ushort, float, schar >,
|
||||
addWeightedImpl<ushort, float, ushort>,
|
||||
addWeightedImpl<ushort, float, short >,
|
||||
addWeightedImpl<ushort, float, int >,
|
||||
addWeightedImpl<ushort, float, float >,
|
||||
addWeightedImpl<ushort, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<ushort, double, uchar >,
|
||||
addWeightedImpl<ushort, double, schar >,
|
||||
addWeightedImpl<ushort, double, ushort>,
|
||||
addWeightedImpl<ushort, double, short >,
|
||||
addWeightedImpl<ushort, double, int >,
|
||||
addWeightedImpl<ushort, double, float >,
|
||||
addWeightedImpl<ushort, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<short, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<short, uchar, schar >*/,
|
||||
0/*addWeightedImpl<short, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<short, uchar, short >*/,
|
||||
0/*addWeightedImpl<short, uchar, int >*/,
|
||||
0/*addWeightedImpl<short, uchar, float >*/,
|
||||
0/*addWeightedImpl<short, uchar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<short, schar, uchar >*/,
|
||||
0/*addWeightedImpl<short, schar, schar >*/,
|
||||
0/*addWeightedImpl<short, schar, ushort>*/,
|
||||
0/*addWeightedImpl<short, schar, short >*/,
|
||||
0/*addWeightedImpl<short, schar, int >*/,
|
||||
0/*addWeightedImpl<short, schar, float >*/,
|
||||
0/*addWeightedImpl<short, schar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<short, ushort, uchar >*/,
|
||||
0/*addWeightedImpl<short, ushort, schar >*/,
|
||||
0/*addWeightedImpl<short, ushort, ushort>*/,
|
||||
0/*addWeightedImpl<short, ushort, short >*/,
|
||||
0/*addWeightedImpl<short, ushort, int >*/,
|
||||
0/*addWeightedImpl<short, ushort, float >*/,
|
||||
0/*addWeightedImpl<short, ushort, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<short, short, uchar >,
|
||||
addWeightedImpl<short, short, schar >,
|
||||
addWeightedImpl<short, short, ushort>,
|
||||
addWeightedImpl<short, short, short >,
|
||||
addWeightedImpl<short, short, int >,
|
||||
addWeightedImpl<short, short, float >,
|
||||
addWeightedImpl<short, short, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<short, int, uchar >,
|
||||
addWeightedImpl<short, int, schar >,
|
||||
addWeightedImpl<short, int, ushort>,
|
||||
addWeightedImpl<short, int, short >,
|
||||
addWeightedImpl<short, int, int >,
|
||||
addWeightedImpl<short, int, float >,
|
||||
addWeightedImpl<short, int, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<short, float, uchar >,
|
||||
addWeightedImpl<short, float, schar >,
|
||||
addWeightedImpl<short, float, ushort>,
|
||||
addWeightedImpl<short, float, short >,
|
||||
addWeightedImpl<short, float, int >,
|
||||
addWeightedImpl<short, float, float >,
|
||||
addWeightedImpl<short, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<short, double, uchar >,
|
||||
addWeightedImpl<short, double, schar >,
|
||||
addWeightedImpl<short, double, ushort>,
|
||||
addWeightedImpl<short, double, short >,
|
||||
addWeightedImpl<short, double, int >,
|
||||
addWeightedImpl<short, double, float >,
|
||||
addWeightedImpl<short, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<int, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<int, uchar, schar >*/,
|
||||
0/*addWeightedImpl<int, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<int, uchar, short >*/,
|
||||
0/*addWeightedImpl<int, uchar, int >*/,
|
||||
0/*addWeightedImpl<int, uchar, float >*/,
|
||||
0/*addWeightedImpl<int, uchar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<int, schar, uchar >*/,
|
||||
0/*addWeightedImpl<int, schar, schar >*/,
|
||||
0/*addWeightedImpl<int, schar, ushort>*/,
|
||||
0/*addWeightedImpl<int, schar, short >*/,
|
||||
0/*addWeightedImpl<int, schar, int >*/,
|
||||
0/*addWeightedImpl<int, schar, float >*/,
|
||||
0/*addWeightedImpl<int, schar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<int, ushort, uchar >*/,
|
||||
0/*addWeightedImpl<int, ushort, schar >*/,
|
||||
0/*addWeightedImpl<int, ushort, ushort>*/,
|
||||
0/*addWeightedImpl<int, ushort, short >*/,
|
||||
0/*addWeightedImpl<int, ushort, int >*/,
|
||||
0/*addWeightedImpl<int, ushort, float >*/,
|
||||
0/*addWeightedImpl<int, ushort, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<int, short, uchar >*/,
|
||||
0/*addWeightedImpl<int, short, schar >*/,
|
||||
0/*addWeightedImpl<int, short, ushort>*/,
|
||||
0/*addWeightedImpl<int, short, short >*/,
|
||||
0/*addWeightedImpl<int, short, int >*/,
|
||||
0/*addWeightedImpl<int, short, float >*/,
|
||||
0/*addWeightedImpl<int, short, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<int, int, uchar >,
|
||||
addWeightedImpl<int, int, schar >,
|
||||
addWeightedImpl<int, int, ushort>,
|
||||
addWeightedImpl<int, int, short >,
|
||||
addWeightedImpl<int, int, int >,
|
||||
addWeightedImpl<int, int, float >,
|
||||
addWeightedImpl<int, int, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<int, float, uchar >,
|
||||
addWeightedImpl<int, float, schar >,
|
||||
addWeightedImpl<int, float, ushort>,
|
||||
addWeightedImpl<int, float, short >,
|
||||
addWeightedImpl<int, float, int >,
|
||||
addWeightedImpl<int, float, float >,
|
||||
addWeightedImpl<int, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<int, double, uchar >,
|
||||
addWeightedImpl<int, double, schar >,
|
||||
addWeightedImpl<int, double, ushort>,
|
||||
addWeightedImpl<int, double, short >,
|
||||
addWeightedImpl<int, double, int >,
|
||||
addWeightedImpl<int, double, float >,
|
||||
addWeightedImpl<int, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<float, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<float, uchar, schar >*/,
|
||||
0/*addWeightedImpl<float, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<float, uchar, short >*/,
|
||||
0/*addWeightedImpl<float, uchar, int >*/,
|
||||
0/*addWeightedImpl<float, uchar, float >*/,
|
||||
0/*addWeightedImpl<float, uchar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<float, schar, uchar >*/,
|
||||
0/*addWeightedImpl<float, schar, schar >*/,
|
||||
0/*addWeightedImpl<float, schar, ushort>*/,
|
||||
0/*addWeightedImpl<float, schar, short >*/,
|
||||
0/*addWeightedImpl<float, schar, int >*/,
|
||||
0/*addWeightedImpl<float, schar, float >*/,
|
||||
0/*addWeightedImpl<float, schar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<float, ushort, uchar >*/,
|
||||
0/*addWeightedImpl<float, ushort, schar >*/,
|
||||
0/*addWeightedImpl<float, ushort, ushort>*/,
|
||||
0/*addWeightedImpl<float, ushort, short >*/,
|
||||
0/*addWeightedImpl<float, ushort, int >*/,
|
||||
0/*addWeightedImpl<float, ushort, float >*/,
|
||||
0/*addWeightedImpl<float, ushort, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<float, short, uchar >*/,
|
||||
0/*addWeightedImpl<float, short, schar >*/,
|
||||
0/*addWeightedImpl<float, short, ushort>*/,
|
||||
0/*addWeightedImpl<float, short, short >*/,
|
||||
0/*addWeightedImpl<float, short, int >*/,
|
||||
0/*addWeightedImpl<float, short, float >*/,
|
||||
0/*addWeightedImpl<float, short, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<float, int, uchar >*/,
|
||||
0/*addWeightedImpl<float, int, schar >*/,
|
||||
0/*addWeightedImpl<float, int, ushort>*/,
|
||||
0/*addWeightedImpl<float, int, short >*/,
|
||||
0/*addWeightedImpl<float, int, int >*/,
|
||||
0/*addWeightedImpl<float, int, float >*/,
|
||||
0/*addWeightedImpl<float, int, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<float, float, uchar >,
|
||||
addWeightedImpl<float, float, schar >,
|
||||
addWeightedImpl<float, float, ushort>,
|
||||
addWeightedImpl<float, float, short >,
|
||||
addWeightedImpl<float, float, int >,
|
||||
addWeightedImpl<float, float, float >,
|
||||
addWeightedImpl<float, float, double>
|
||||
},
|
||||
{
|
||||
addWeightedImpl<float, double, uchar >,
|
||||
addWeightedImpl<float, double, schar >,
|
||||
addWeightedImpl<float, double, ushort>,
|
||||
addWeightedImpl<float, double, short >,
|
||||
addWeightedImpl<float, double, int >,
|
||||
addWeightedImpl<float, double, float >,
|
||||
addWeightedImpl<float, double, double>
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
0/*addWeightedImpl<double, uchar, uchar >*/,
|
||||
0/*addWeightedImpl<double, uchar, schar >*/,
|
||||
0/*addWeightedImpl<double, uchar, ushort>*/,
|
||||
0/*addWeightedImpl<double, uchar, short >*/,
|
||||
0/*addWeightedImpl<double, uchar, int >*/,
|
||||
0/*addWeightedImpl<double, uchar, float >*/,
|
||||
0/*addWeightedImpl<double, uchar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<double, schar, uchar >*/,
|
||||
0/*addWeightedImpl<double, schar, schar >*/,
|
||||
0/*addWeightedImpl<double, schar, ushort>*/,
|
||||
0/*addWeightedImpl<double, schar, short >*/,
|
||||
0/*addWeightedImpl<double, schar, int >*/,
|
||||
0/*addWeightedImpl<double, schar, float >*/,
|
||||
0/*addWeightedImpl<double, schar, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<double, ushort, uchar >*/,
|
||||
0/*addWeightedImpl<double, ushort, schar >*/,
|
||||
0/*addWeightedImpl<double, ushort, ushort>*/,
|
||||
0/*addWeightedImpl<double, ushort, short >*/,
|
||||
0/*addWeightedImpl<double, ushort, int >*/,
|
||||
0/*addWeightedImpl<double, ushort, float >*/,
|
||||
0/*addWeightedImpl<double, ushort, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<double, short, uchar >*/,
|
||||
0/*addWeightedImpl<double, short, schar >*/,
|
||||
0/*addWeightedImpl<double, short, ushort>*/,
|
||||
0/*addWeightedImpl<double, short, short >*/,
|
||||
0/*addWeightedImpl<double, short, int >*/,
|
||||
0/*addWeightedImpl<double, short, float >*/,
|
||||
0/*addWeightedImpl<double, short, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<double, int, uchar >*/,
|
||||
0/*addWeightedImpl<double, int, schar >*/,
|
||||
0/*addWeightedImpl<double, int, ushort>*/,
|
||||
0/*addWeightedImpl<double, int, short >*/,
|
||||
0/*addWeightedImpl<double, int, int >*/,
|
||||
0/*addWeightedImpl<double, int, float >*/,
|
||||
0/*addWeightedImpl<double, int, double>*/
|
||||
},
|
||||
{
|
||||
0/*addWeightedImpl<double, float, uchar >*/,
|
||||
0/*addWeightedImpl<double, float, schar >*/,
|
||||
0/*addWeightedImpl<double, float, ushort>*/,
|
||||
0/*addWeightedImpl<double, float, short >*/,
|
||||
0/*addWeightedImpl<double, float, int >*/,
|
||||
0/*addWeightedImpl<double, float, float >*/,
|
||||
0/*addWeightedImpl<double, float, double>*/
|
||||
},
|
||||
{
|
||||
addWeightedImpl<double, double, uchar >,
|
||||
addWeightedImpl<double, double, schar >,
|
||||
addWeightedImpl<double, double, ushort>,
|
||||
addWeightedImpl<double, double, short >,
|
||||
addWeightedImpl<double, double, int >,
|
||||
addWeightedImpl<double, double, float >,
|
||||
addWeightedImpl<double, double, double>
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
int sdepth1 = src1.depth();
|
||||
int sdepth2 = src2.depth();
|
||||
|
||||
ddepth = ddepth >= 0 ? CV_MAT_DEPTH(ddepth) : std::max(sdepth1, sdepth2);
|
||||
const int cn = src1.channels();
|
||||
|
||||
CV_Assert( src2.size() == src1.size() && src2.channels() == cn );
|
||||
CV_Assert( sdepth1 <= CV_64F && sdepth2 <= CV_64F && ddepth <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), CV_MAKE_TYPE(ddepth, cn), stream);
|
||||
|
||||
GpuMat src1_single = src1.reshape(1);
|
||||
GpuMat src2_single = src2.reshape(1);
|
||||
GpuMat dst_single = dst.reshape(1);
|
||||
|
||||
if (sdepth1 > sdepth2)
|
||||
{
|
||||
src1_single.swap(src2_single);
|
||||
std::swap(alpha, beta);
|
||||
std::swap(sdepth1, sdepth2);
|
||||
}
|
||||
|
||||
const func_t func = funcs[sdepth1][sdepth2][ddepth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_single, alpha, src2_single, beta, gamma, dst_single, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,230 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
void bitMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// bitwise_not
|
||||
|
||||
void cv::cuda::bitwise_not(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
const int depth = src.depth();
|
||||
|
||||
CV_DbgAssert( depth <= CV_32F );
|
||||
CV_DbgAssert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == src.size()) );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
const int bcols = (int) (src.cols * src.elemSize());
|
||||
|
||||
if ((bcols & 3) == 0)
|
||||
{
|
||||
const int vcols = bcols >> 2;
|
||||
|
||||
GlobPtrSz<uint> vsrc = globPtr((uint*) src.data, src.step, src.rows, vcols);
|
||||
GlobPtrSz<uint> vdst = globPtr((uint*) dst.data, dst.step, src.rows, vcols);
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<uint>(), stream);
|
||||
}
|
||||
else if ((bcols & 1) == 0)
|
||||
{
|
||||
const int vcols = bcols >> 1;
|
||||
|
||||
GlobPtrSz<ushort> vsrc = globPtr((ushort*) src.data, src.step, src.rows, vcols);
|
||||
GlobPtrSz<ushort> vdst = globPtr((ushort*) dst.data, dst.step, src.rows, vcols);
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<ushort>(), stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobPtrSz<uchar> vsrc = globPtr((uchar*) src.data, src.step, src.rows, bcols);
|
||||
GlobPtrSz<uchar> vdst = globPtr((uchar*) dst.data, dst.step, src.rows, bcols);
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<uchar>(), stream);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (depth == CV_32F || depth == CV_32S)
|
||||
{
|
||||
GlobPtrSz<uint> vsrc = globPtr((uint*) src.data, src.step, src.rows, src.cols * src.channels());
|
||||
GlobPtrSz<uint> vdst = globPtr((uint*) dst.data, dst.step, src.rows, src.cols * src.channels());
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<uint>(), singleMaskChannels(globPtr<uchar>(mask), src.channels()), stream);
|
||||
}
|
||||
else if (depth == CV_16S || depth == CV_16U)
|
||||
{
|
||||
GlobPtrSz<ushort> vsrc = globPtr((ushort*) src.data, src.step, src.rows, src.cols * src.channels());
|
||||
GlobPtrSz<ushort> vdst = globPtr((ushort*) dst.data, dst.step, src.rows, src.cols * src.channels());
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<ushort>(), singleMaskChannels(globPtr<uchar>(mask), src.channels()), stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobPtrSz<uchar> vsrc = globPtr((uchar*) src.data, src.step, src.rows, src.cols * src.channels());
|
||||
GlobPtrSz<uchar> vdst = globPtr((uchar*) dst.data, dst.step, src.rows, src.cols * src.channels());
|
||||
|
||||
gridTransformUnary(vsrc, vdst, bit_not<uchar>(), singleMaskChannels(globPtr<uchar>(mask), src.channels()), stream);
|
||||
}
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// Binary bitwise logical operations
|
||||
|
||||
namespace
|
||||
{
|
||||
template <template <typename> class Op, typename T>
|
||||
void bitMatOp(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
GlobPtrSz<T> vsrc1 = globPtr((T*) src1.data, src1.step, src1.rows, src1.cols * src1.channels());
|
||||
GlobPtrSz<T> vsrc2 = globPtr((T*) src2.data, src2.step, src1.rows, src1.cols * src1.channels());
|
||||
GlobPtrSz<T> vdst = globPtr((T*) dst.data, dst.step, src1.rows, src1.cols * src1.channels());
|
||||
|
||||
if (mask.data)
|
||||
gridTransformBinary(vsrc1, vsrc2, vdst, Op<T>(), singleMaskChannels(globPtr<uchar>(mask), src1.channels()), stream);
|
||||
else
|
||||
gridTransformBinary(vsrc1, vsrc2, vdst, Op<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void bitMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs32[] =
|
||||
{
|
||||
bitMatOp<bit_and, uint>,
|
||||
bitMatOp<bit_or, uint>,
|
||||
bitMatOp<bit_xor, uint>
|
||||
};
|
||||
static const func_t funcs16[] =
|
||||
{
|
||||
bitMatOp<bit_and, ushort>,
|
||||
bitMatOp<bit_or, ushort>,
|
||||
bitMatOp<bit_xor, ushort>
|
||||
};
|
||||
static const func_t funcs8[] =
|
||||
{
|
||||
bitMatOp<bit_and, uchar>,
|
||||
bitMatOp<bit_or, uchar>,
|
||||
bitMatOp<bit_xor, uchar>
|
||||
};
|
||||
|
||||
const int depth = src1.depth();
|
||||
|
||||
CV_DbgAssert( depth <= CV_32F );
|
||||
CV_DbgAssert( op >= 0 && op < 3 );
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
const int bcols = (int) (src1.cols * src1.elemSize());
|
||||
|
||||
if ((bcols & 3) == 0)
|
||||
{
|
||||
const int vcols = bcols >> 2;
|
||||
|
||||
GpuMat vsrc1(src1.rows, vcols, CV_32SC1, src1.data, src1.step);
|
||||
GpuMat vsrc2(src1.rows, vcols, CV_32SC1, src2.data, src2.step);
|
||||
GpuMat vdst(src1.rows, vcols, CV_32SC1, dst.data, dst.step);
|
||||
|
||||
funcs32[op](vsrc1, vsrc2, vdst, GpuMat(), stream);
|
||||
}
|
||||
else if ((bcols & 1) == 0)
|
||||
{
|
||||
const int vcols = bcols >> 1;
|
||||
|
||||
GpuMat vsrc1(src1.rows, vcols, CV_16UC1, src1.data, src1.step);
|
||||
GpuMat vsrc2(src1.rows, vcols, CV_16UC1, src2.data, src2.step);
|
||||
GpuMat vdst(src1.rows, vcols, CV_16UC1, dst.data, dst.step);
|
||||
|
||||
funcs16[op](vsrc1, vsrc2, vdst, GpuMat(), stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
GpuMat vsrc1(src1.rows, bcols, CV_8UC1, src1.data, src1.step);
|
||||
GpuMat vsrc2(src1.rows, bcols, CV_8UC1, src2.data, src2.step);
|
||||
GpuMat vdst(src1.rows, bcols, CV_8UC1, dst.data, dst.step);
|
||||
|
||||
funcs8[op](vsrc1, vsrc2, vdst, GpuMat(), stream);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (depth == CV_32F || depth == CV_32S)
|
||||
{
|
||||
funcs32[op](src1, src2, dst, mask, stream);
|
||||
}
|
||||
else if (depth == CV_16S || depth == CV_16U)
|
||||
{
|
||||
funcs16[op](src1, src2, dst, mask, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
funcs8[op](src1, src2, dst, mask, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,206 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void bitScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <template <typename> class Op, typename T>
|
||||
void bitScalarOp(const GpuMat& src, uint value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary(globPtr<T>(src), globPtr<T>(dst), bind2nd(Op<T>(), value), stream);
|
||||
}
|
||||
|
||||
typedef void (*bit_scalar_func_t)(const GpuMat& src, uint value, GpuMat& dst, Stream& stream);
|
||||
|
||||
template <typename T, bit_scalar_func_t func> struct BitScalar
|
||||
{
|
||||
static void call(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
func(src, cv::saturate_cast<T>(value[0]), dst, stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <bit_scalar_func_t func> struct BitScalar4
|
||||
{
|
||||
static void call(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
uint packedVal = 0;
|
||||
|
||||
packedVal |= cv::saturate_cast<uchar>(value[0]);
|
||||
packedVal |= cv::saturate_cast<uchar>(value[1]) << 8;
|
||||
packedVal |= cv::saturate_cast<uchar>(value[2]) << 16;
|
||||
packedVal |= cv::saturate_cast<uchar>(value[3]) << 24;
|
||||
|
||||
func(src, packedVal, dst, stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <int DEPTH, int cn> struct NppBitwiseCFunc
|
||||
{
|
||||
typedef typename NPPTypeTraits<DEPTH>::npp_type npp_type;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus(*func_t)(const npp_type* pSrc1, int nSrc1Step, const npp_type* pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus (*func_t)(const npp_type* pSrc1, int nSrc1Step, const npp_type* pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI);
|
||||
#endif
|
||||
};
|
||||
|
||||
template <int DEPTH, int cn, typename NppBitwiseCFunc<DEPTH, cn>::func_t func> struct NppBitwiseC
|
||||
{
|
||||
typedef typename NppBitwiseCFunc<DEPTH, cn>::npp_type npp_type;
|
||||
|
||||
static void call(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& _stream)
|
||||
{
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiSize oSizeROI;
|
||||
oSizeROI.width = src.cols;
|
||||
oSizeROI.height = src.rows;
|
||||
|
||||
const npp_type pConstants[] =
|
||||
{
|
||||
cv::saturate_cast<npp_type>(value[0]),
|
||||
cv::saturate_cast<npp_type>(value[1]),
|
||||
cv::saturate_cast<npp_type>(value[2]),
|
||||
cv::saturate_cast<npp_type>(value[3])
|
||||
};
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(func(src.ptr<npp_type>(), static_cast<int>(src.step), pConstants, dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI, h));
|
||||
#else
|
||||
nppSafeCall( func(src.ptr<npp_type>(), static_cast<int>(src.step), pConstants, dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void bitScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op)
|
||||
{
|
||||
CV_UNUSED(mask);
|
||||
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[3][6][4] =
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
{
|
||||
{BitScalar<uchar, bitScalarOp<bit_and, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiAndC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_and, uint> >::call},
|
||||
{BitScalar<uchar, bitScalarOp<bit_and, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiAndC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_and, uint> >::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_and, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiAndC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiAndC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_and, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiAndC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiAndC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_and, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiAndC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiAndC_32s_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_and, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiAndC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiAndC_32s_C4R_Ctx>::call}
|
||||
},
|
||||
{
|
||||
{BitScalar<uchar, bitScalarOp<bit_or, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiOrC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_or, uint> >::call},
|
||||
{BitScalar<uchar, bitScalarOp<bit_or, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiOrC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_or, uint> >::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_or, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiOrC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiOrC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_or, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiOrC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiOrC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_or, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiOrC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiOrC_32s_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_or, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiOrC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiOrC_32s_C4R_Ctx>::call}
|
||||
},
|
||||
{
|
||||
{BitScalar<uchar, bitScalarOp<bit_xor, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiXorC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_xor, uint> >::call},
|
||||
{BitScalar<uchar, bitScalarOp<bit_xor, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiXorC_8u_C3R_Ctx >::call, BitScalar4< bitScalarOp<bit_xor, uint> >::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_xor, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiXorC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiXorC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_xor, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiXorC_16u_C3R_Ctx>::call, NppBitwiseC<CV_16U, 4, nppiXorC_16u_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_xor, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiXorC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiXorC_32s_C4R_Ctx>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_xor, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiXorC_32s_C3R_Ctx>::call, NppBitwiseC<CV_32S, 4, nppiXorC_32s_C4R_Ctx>::call}
|
||||
}
|
||||
#else
|
||||
{
|
||||
{ BitScalar<uchar, bitScalarOp<bit_and, uchar> >::call, 0, NppBitwiseC<CV_8U, 3, nppiAndC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_and, uint> >::call },
|
||||
{ BitScalar<uchar, bitScalarOp<bit_and, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiAndC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_and, uint> >::call },
|
||||
{ BitScalar<ushort, bitScalarOp<bit_and, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiAndC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiAndC_16u_C4R>::call },
|
||||
{ BitScalar<ushort, bitScalarOp<bit_and, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiAndC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiAndC_16u_C4R>::call },
|
||||
{ BitScalar<uint, bitScalarOp<bit_and, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiAndC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiAndC_32s_C4R>::call },
|
||||
{ BitScalar<uint, bitScalarOp<bit_and, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiAndC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiAndC_32s_C4R>::call }
|
||||
},
|
||||
{
|
||||
{BitScalar<uchar, bitScalarOp<bit_or, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiOrC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_or, uint> >::call},
|
||||
{BitScalar<uchar, bitScalarOp<bit_or, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiOrC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_or, uint> >::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_or, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiOrC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiOrC_16u_C4R>::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_or, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiOrC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiOrC_16u_C4R>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_or, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiOrC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiOrC_32s_C4R>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_or, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiOrC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiOrC_32s_C4R>::call}
|
||||
},
|
||||
{
|
||||
{BitScalar<uchar, bitScalarOp<bit_xor, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiXorC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_xor, uint> >::call},
|
||||
{BitScalar<uchar, bitScalarOp<bit_xor, uchar> >::call , 0, NppBitwiseC<CV_8U , 3, nppiXorC_8u_C3R >::call, BitScalar4< bitScalarOp<bit_xor, uint> >::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_xor, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiXorC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiXorC_16u_C4R>::call},
|
||||
{BitScalar<ushort, bitScalarOp<bit_xor, ushort> >::call, 0, NppBitwiseC<CV_16U, 3, nppiXorC_16u_C3R>::call, NppBitwiseC<CV_16U, 4, nppiXorC_16u_C4R>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_xor, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiXorC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiXorC_32s_C4R>::call},
|
||||
{BitScalar<uint, bitScalarOp<bit_xor, uint> >::call , 0, NppBitwiseC<CV_32S, 3, nppiXorC_32s_C3R>::call, NppBitwiseC<CV_32S, 4, nppiXorC_32s_C4R>::call}
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
const int depth = src.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( depth <= CV_32F );
|
||||
CV_DbgAssert( cn == 1 || cn == 3 || cn == 4 );
|
||||
CV_DbgAssert( mask.empty() );
|
||||
CV_DbgAssert( op >= 0 && op < 3 );
|
||||
|
||||
funcs[op][depth][cn - 1](src, value, dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,219 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void cmpMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <class Op, typename T> struct CmpOp : binary_function<T, T, uchar>
|
||||
{
|
||||
__device__ __forceinline__ uchar operator()(T a, T b) const
|
||||
{
|
||||
Op op;
|
||||
return -op(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <template <typename> class Op, typename T>
|
||||
void cmpMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
CmpOp<Op<T>, T> op;
|
||||
gridTransformBinary_< TransformPolicy<T> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<uchar>(dst), op, stream);
|
||||
}
|
||||
|
||||
struct VCmpEq4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vcmpeq4(a, b);
|
||||
}
|
||||
};
|
||||
struct VCmpNe4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vcmpne4(a, b);
|
||||
}
|
||||
};
|
||||
struct VCmpLt4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vcmplt4(a, b);
|
||||
}
|
||||
};
|
||||
struct VCmpLe4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vcmple4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void cmpMatEq_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, VCmpEq4(), stream);
|
||||
}
|
||||
void cmpMatNe_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, VCmpNe4(), stream);
|
||||
}
|
||||
void cmpMatLt_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, VCmpLt4(), stream);
|
||||
}
|
||||
void cmpMatLe_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, VCmpLe4(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cmpMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[7][4] =
|
||||
{
|
||||
{cmpMat_v1<equal_to, uchar> , cmpMat_v1<not_equal_to, uchar> , cmpMat_v1<less, uchar> , cmpMat_v1<less_equal, uchar> },
|
||||
{cmpMat_v1<equal_to, schar> , cmpMat_v1<not_equal_to, schar> , cmpMat_v1<less, schar> , cmpMat_v1<less_equal, schar> },
|
||||
{cmpMat_v1<equal_to, ushort>, cmpMat_v1<not_equal_to, ushort>, cmpMat_v1<less, ushort>, cmpMat_v1<less_equal, ushort>},
|
||||
{cmpMat_v1<equal_to, short> , cmpMat_v1<not_equal_to, short> , cmpMat_v1<less, short> , cmpMat_v1<less_equal, short> },
|
||||
{cmpMat_v1<equal_to, int> , cmpMat_v1<not_equal_to, int> , cmpMat_v1<less, int> , cmpMat_v1<less_equal, int> },
|
||||
{cmpMat_v1<equal_to, float> , cmpMat_v1<not_equal_to, float> , cmpMat_v1<less, float> , cmpMat_v1<less_equal, float> },
|
||||
{cmpMat_v1<equal_to, double>, cmpMat_v1<not_equal_to, double>, cmpMat_v1<less, double>, cmpMat_v1<less_equal, double>}
|
||||
};
|
||||
|
||||
typedef void (*func_v4_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
static const func_v4_t funcs_v4[] =
|
||||
{
|
||||
cmpMatEq_v4, cmpMatNe_v4, cmpMatLt_v4, cmpMatLe_v4
|
||||
};
|
||||
|
||||
const int depth = src1.depth();
|
||||
|
||||
CV_DbgAssert( depth <= CV_64F );
|
||||
|
||||
static const int codes[] =
|
||||
{
|
||||
0, 2, 3, 2, 3, 1
|
||||
};
|
||||
const GpuMat* psrc1[] =
|
||||
{
|
||||
&src1, &src2, &src2, &src1, &src1, &src1
|
||||
};
|
||||
const GpuMat* psrc2[] =
|
||||
{
|
||||
&src2, &src1, &src1, &src2, &src2, &src2
|
||||
};
|
||||
|
||||
const int code = codes[cmpop];
|
||||
|
||||
GpuMat src1_ = psrc1[cmpop]->reshape(1);
|
||||
GpuMat src2_ = psrc2[cmpop]->reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
if (depth == CV_8U && (src1_.cols & 3) == 0)
|
||||
{
|
||||
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
|
||||
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
|
||||
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
|
||||
|
||||
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
|
||||
|
||||
if (isAllAligned)
|
||||
{
|
||||
funcs_v4[code](src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const func_t func = funcs[depth][code];
|
||||
|
||||
func(src1_, src2_, dst_, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,225 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void cmpScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <class Op, typename T> struct CmpOp : binary_function<T, T, uchar>
|
||||
{
|
||||
__device__ __forceinline__ uchar operator()(T a, T b) const
|
||||
{
|
||||
Op op;
|
||||
return -op(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
#define MAKE_VEC(_type, _cn) typename MakeVec<_type, _cn>::type
|
||||
|
||||
template <class Op, typename T, int cn> struct CmpScalarOp;
|
||||
|
||||
template <class Op, typename T>
|
||||
struct CmpScalarOp<Op, T, 1> : unary_function<T, uchar>
|
||||
{
|
||||
T val;
|
||||
|
||||
__device__ __forceinline__ uchar operator()(T src) const
|
||||
{
|
||||
CmpOp<Op, T> op;
|
||||
return op(src, val);
|
||||
}
|
||||
};
|
||||
|
||||
template <class Op, typename T>
|
||||
struct CmpScalarOp<Op, T, 2> : unary_function<MAKE_VEC(T, 2), MAKE_VEC(uchar, 2)>
|
||||
{
|
||||
MAKE_VEC(T, 2) val;
|
||||
|
||||
__device__ __forceinline__ MAKE_VEC(uchar, 2) operator()(const MAKE_VEC(T, 2) & src) const
|
||||
{
|
||||
CmpOp<Op, T> op;
|
||||
return VecTraits<MAKE_VEC(uchar, 2)>::make(op(src.x, val.x), op(src.y, val.y));
|
||||
}
|
||||
};
|
||||
|
||||
template <class Op, typename T>
|
||||
struct CmpScalarOp<Op, T, 3> : unary_function<MAKE_VEC(T, 3), MAKE_VEC(uchar, 3)>
|
||||
{
|
||||
MAKE_VEC(T, 3) val;
|
||||
|
||||
__device__ __forceinline__ MAKE_VEC(uchar, 3) operator()(const MAKE_VEC(T, 3) & src) const
|
||||
{
|
||||
CmpOp<Op, T> op;
|
||||
return VecTraits<MAKE_VEC(uchar, 3)>::make(op(src.x, val.x), op(src.y, val.y), op(src.z, val.z));
|
||||
}
|
||||
};
|
||||
|
||||
template <class Op, typename T>
|
||||
struct CmpScalarOp<Op, T, 4> : unary_function<MAKE_VEC(T, 4), MAKE_VEC(uchar, 4)>
|
||||
{
|
||||
MAKE_VEC(T, 4) val;
|
||||
|
||||
__device__ __forceinline__ MAKE_VEC(uchar, 4) operator()(const MAKE_VEC(T, 4) & src) const
|
||||
{
|
||||
CmpOp<Op, T> op;
|
||||
return VecTraits<MAKE_VEC(uchar, 4)>::make(op(src.x, val.x), op(src.y, val.y), op(src.z, val.z), op(src.w, val.w));
|
||||
}
|
||||
};
|
||||
|
||||
#undef TYPE_VEC
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <template <typename> class Op, typename T, int cn>
|
||||
void cmpScalarImpl(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, cn>::type src_type;
|
||||
typedef typename MakeVec<uchar, cn>::type dst_type;
|
||||
|
||||
cv::Scalar_<T> value_ = value;
|
||||
|
||||
CmpScalarOp<Op<T>, T, cn> op;
|
||||
op.val = VecTraits<src_type>::make(value_.val);
|
||||
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<src_type>(src), globPtr<dst_type>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cmpScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[7][6][4] =
|
||||
{
|
||||
{
|
||||
{cmpScalarImpl<equal_to, uchar, 1>, cmpScalarImpl<equal_to, uchar, 2>, cmpScalarImpl<equal_to, uchar, 3>, cmpScalarImpl<equal_to, uchar, 4>},
|
||||
{cmpScalarImpl<greater, uchar, 1>, cmpScalarImpl<greater, uchar, 2>, cmpScalarImpl<greater, uchar, 3>, cmpScalarImpl<greater, uchar, 4>},
|
||||
{cmpScalarImpl<greater_equal, uchar, 1>, cmpScalarImpl<greater_equal, uchar, 2>, cmpScalarImpl<greater_equal, uchar, 3>, cmpScalarImpl<greater_equal, uchar, 4>},
|
||||
{cmpScalarImpl<less, uchar, 1>, cmpScalarImpl<less, uchar, 2>, cmpScalarImpl<less, uchar, 3>, cmpScalarImpl<less, uchar, 4>},
|
||||
{cmpScalarImpl<less_equal, uchar, 1>, cmpScalarImpl<less_equal, uchar, 2>, cmpScalarImpl<less_equal, uchar, 3>, cmpScalarImpl<less_equal, uchar, 4>},
|
||||
{cmpScalarImpl<not_equal_to, uchar, 1>, cmpScalarImpl<not_equal_to, uchar, 2>, cmpScalarImpl<not_equal_to, uchar, 3>, cmpScalarImpl<not_equal_to, uchar, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, schar, 1>, cmpScalarImpl<equal_to, schar, 2>, cmpScalarImpl<equal_to, schar, 3>, cmpScalarImpl<equal_to, schar, 4>},
|
||||
{cmpScalarImpl<greater, schar, 1>, cmpScalarImpl<greater, schar, 2>, cmpScalarImpl<greater, schar, 3>, cmpScalarImpl<greater, schar, 4>},
|
||||
{cmpScalarImpl<greater_equal, schar, 1>, cmpScalarImpl<greater_equal, schar, 2>, cmpScalarImpl<greater_equal, schar, 3>, cmpScalarImpl<greater_equal, schar, 4>},
|
||||
{cmpScalarImpl<less, schar, 1>, cmpScalarImpl<less, schar, 2>, cmpScalarImpl<less, schar, 3>, cmpScalarImpl<less, schar, 4>},
|
||||
{cmpScalarImpl<less_equal, schar, 1>, cmpScalarImpl<less_equal, schar, 2>, cmpScalarImpl<less_equal, schar, 3>, cmpScalarImpl<less_equal, schar, 4>},
|
||||
{cmpScalarImpl<not_equal_to, schar, 1>, cmpScalarImpl<not_equal_to, schar, 2>, cmpScalarImpl<not_equal_to, schar, 3>, cmpScalarImpl<not_equal_to, schar, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, ushort, 1>, cmpScalarImpl<equal_to, ushort, 2>, cmpScalarImpl<equal_to, ushort, 3>, cmpScalarImpl<equal_to, ushort, 4>},
|
||||
{cmpScalarImpl<greater, ushort, 1>, cmpScalarImpl<greater, ushort, 2>, cmpScalarImpl<greater, ushort, 3>, cmpScalarImpl<greater, ushort, 4>},
|
||||
{cmpScalarImpl<greater_equal, ushort, 1>, cmpScalarImpl<greater_equal, ushort, 2>, cmpScalarImpl<greater_equal, ushort, 3>, cmpScalarImpl<greater_equal, ushort, 4>},
|
||||
{cmpScalarImpl<less, ushort, 1>, cmpScalarImpl<less, ushort, 2>, cmpScalarImpl<less, ushort, 3>, cmpScalarImpl<less, ushort, 4>},
|
||||
{cmpScalarImpl<less_equal, ushort, 1>, cmpScalarImpl<less_equal, ushort, 2>, cmpScalarImpl<less_equal, ushort, 3>, cmpScalarImpl<less_equal, ushort, 4>},
|
||||
{cmpScalarImpl<not_equal_to, ushort, 1>, cmpScalarImpl<not_equal_to, ushort, 2>, cmpScalarImpl<not_equal_to, ushort, 3>, cmpScalarImpl<not_equal_to, ushort, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, short, 1>, cmpScalarImpl<equal_to, short, 2>, cmpScalarImpl<equal_to, short, 3>, cmpScalarImpl<equal_to, short, 4>},
|
||||
{cmpScalarImpl<greater, short, 1>, cmpScalarImpl<greater, short, 2>, cmpScalarImpl<greater, short, 3>, cmpScalarImpl<greater, short, 4>},
|
||||
{cmpScalarImpl<greater_equal, short, 1>, cmpScalarImpl<greater_equal, short, 2>, cmpScalarImpl<greater_equal, short, 3>, cmpScalarImpl<greater_equal, short, 4>},
|
||||
{cmpScalarImpl<less, short, 1>, cmpScalarImpl<less, short, 2>, cmpScalarImpl<less, short, 3>, cmpScalarImpl<less, short, 4>},
|
||||
{cmpScalarImpl<less_equal, short, 1>, cmpScalarImpl<less_equal, short, 2>, cmpScalarImpl<less_equal, short, 3>, cmpScalarImpl<less_equal, short, 4>},
|
||||
{cmpScalarImpl<not_equal_to, short, 1>, cmpScalarImpl<not_equal_to, short, 2>, cmpScalarImpl<not_equal_to, short, 3>, cmpScalarImpl<not_equal_to, short, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, int, 1>, cmpScalarImpl<equal_to, int, 2>, cmpScalarImpl<equal_to, int, 3>, cmpScalarImpl<equal_to, int, 4>},
|
||||
{cmpScalarImpl<greater, int, 1>, cmpScalarImpl<greater, int, 2>, cmpScalarImpl<greater, int, 3>, cmpScalarImpl<greater, int, 4>},
|
||||
{cmpScalarImpl<greater_equal, int, 1>, cmpScalarImpl<greater_equal, int, 2>, cmpScalarImpl<greater_equal, int, 3>, cmpScalarImpl<greater_equal, int, 4>},
|
||||
{cmpScalarImpl<less, int, 1>, cmpScalarImpl<less, int, 2>, cmpScalarImpl<less, int, 3>, cmpScalarImpl<less, int, 4>},
|
||||
{cmpScalarImpl<less_equal, int, 1>, cmpScalarImpl<less_equal, int, 2>, cmpScalarImpl<less_equal, int, 3>, cmpScalarImpl<less_equal, int, 4>},
|
||||
{cmpScalarImpl<not_equal_to, int, 1>, cmpScalarImpl<not_equal_to, int, 2>, cmpScalarImpl<not_equal_to, int, 3>, cmpScalarImpl<not_equal_to, int, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, float, 1>, cmpScalarImpl<equal_to, float, 2>, cmpScalarImpl<equal_to, float, 3>, cmpScalarImpl<equal_to, float, 4>},
|
||||
{cmpScalarImpl<greater, float, 1>, cmpScalarImpl<greater, float, 2>, cmpScalarImpl<greater, float, 3>, cmpScalarImpl<greater, float, 4>},
|
||||
{cmpScalarImpl<greater_equal, float, 1>, cmpScalarImpl<greater_equal, float, 2>, cmpScalarImpl<greater_equal, float, 3>, cmpScalarImpl<greater_equal, float, 4>},
|
||||
{cmpScalarImpl<less, float, 1>, cmpScalarImpl<less, float, 2>, cmpScalarImpl<less, float, 3>, cmpScalarImpl<less, float, 4>},
|
||||
{cmpScalarImpl<less_equal, float, 1>, cmpScalarImpl<less_equal, float, 2>, cmpScalarImpl<less_equal, float, 3>, cmpScalarImpl<less_equal, float, 4>},
|
||||
{cmpScalarImpl<not_equal_to, float, 1>, cmpScalarImpl<not_equal_to, float, 2>, cmpScalarImpl<not_equal_to, float, 3>, cmpScalarImpl<not_equal_to, float, 4>}
|
||||
},
|
||||
{
|
||||
{cmpScalarImpl<equal_to, double, 1>, cmpScalarImpl<equal_to, double, 2>, cmpScalarImpl<equal_to, double, 3>, cmpScalarImpl<equal_to, double, 4>},
|
||||
{cmpScalarImpl<greater, double, 1>, cmpScalarImpl<greater, double, 2>, cmpScalarImpl<greater, double, 3>, cmpScalarImpl<greater, double, 4>},
|
||||
{cmpScalarImpl<greater_equal, double, 1>, cmpScalarImpl<greater_equal, double, 2>, cmpScalarImpl<greater_equal, double, 3>, cmpScalarImpl<greater_equal, double, 4>},
|
||||
{cmpScalarImpl<less, double, 1>, cmpScalarImpl<less, double, 2>, cmpScalarImpl<less, double, 3>, cmpScalarImpl<less, double, 4>},
|
||||
{cmpScalarImpl<less_equal, double, 1>, cmpScalarImpl<less_equal, double, 2>, cmpScalarImpl<less_equal, double, 3>, cmpScalarImpl<less_equal, double, 4>},
|
||||
{cmpScalarImpl<not_equal_to, double, 1>, cmpScalarImpl<not_equal_to, double, 2>, cmpScalarImpl<not_equal_to, double, 3>, cmpScalarImpl<not_equal_to, double, 4>}
|
||||
}
|
||||
};
|
||||
|
||||
if (inv)
|
||||
{
|
||||
// src1 is a scalar; swap it with src2
|
||||
cmpop = cmpop == cv::CMP_LT ? cv::CMP_GT : cmpop == cv::CMP_LE ? cv::CMP_GE :
|
||||
cmpop == cv::CMP_GE ? cv::CMP_LE : cmpop == cv::CMP_GT ? cv::CMP_LT : cmpop;
|
||||
}
|
||||
|
||||
const int depth = src.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_Assert( depth <= CV_64F && cn <= 4 );
|
||||
|
||||
funcs[depth][cmpop][cn - 1](src, val, dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,159 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ShiftMap
|
||||
{
|
||||
typedef int2 value_type;
|
||||
typedef int index_type;
|
||||
|
||||
int top;
|
||||
int left;
|
||||
|
||||
__device__ __forceinline__ int2 operator ()(int y, int x) const
|
||||
{
|
||||
return make_int2(x - left, y - top);
|
||||
}
|
||||
};
|
||||
|
||||
struct ShiftMapSz : ShiftMap
|
||||
{
|
||||
int rows, cols;
|
||||
};
|
||||
}
|
||||
|
||||
namespace cv { namespace cudev {
|
||||
|
||||
template <> struct PtrTraits<ShiftMapSz> : PtrTraitsBase<ShiftMapSz, ShiftMap>
|
||||
{
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, int cn>
|
||||
void copyMakeBorderImpl(const GpuMat& src, GpuMat& dst, int top, int left, int borderMode, cv::Scalar borderValue, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, cn>::type src_type;
|
||||
|
||||
cv::Scalar_<T> borderValue_ = borderValue;
|
||||
const src_type brdVal = VecTraits<src_type>::make(borderValue_.val);
|
||||
|
||||
ShiftMapSz map;
|
||||
map.top = top;
|
||||
map.left = left;
|
||||
map.rows = dst.rows;
|
||||
map.cols = dst.cols;
|
||||
|
||||
switch (borderMode)
|
||||
{
|
||||
case cv::BORDER_CONSTANT:
|
||||
gridCopy(remapPtr(brdConstant(globPtr<src_type>(src), brdVal), map), globPtr<src_type>(dst), stream);
|
||||
break;
|
||||
case cv::BORDER_REPLICATE:
|
||||
gridCopy(remapPtr(brdReplicate(globPtr<src_type>(src)), map), globPtr<src_type>(dst), stream);
|
||||
break;
|
||||
case cv::BORDER_REFLECT:
|
||||
gridCopy(remapPtr(brdReflect(globPtr<src_type>(src)), map), globPtr<src_type>(dst), stream);
|
||||
break;
|
||||
case cv::BORDER_WRAP:
|
||||
gridCopy(remapPtr(brdWrap(globPtr<src_type>(src)), map), globPtr<src_type>(dst), stream);
|
||||
break;
|
||||
case cv::BORDER_REFLECT_101:
|
||||
gridCopy(remapPtr(brdReflect101(globPtr<src_type>(src)), map), globPtr<src_type>(dst), stream);
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::copyMakeBorder(InputArray _src, OutputArray _dst, int top, int bottom, int left, int right, int borderType, Scalar value, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat& dst, int top, int left, int borderMode, cv::Scalar borderValue, Stream& stream);
|
||||
static const func_t funcs[6][4] =
|
||||
{
|
||||
{ copyMakeBorderImpl<uchar , 1> , copyMakeBorderImpl<uchar , 2> , copyMakeBorderImpl<uchar , 3> , copyMakeBorderImpl<uchar , 4> },
|
||||
{0 /*copyMakeBorderImpl<schar , 1>*/, 0 /*copyMakeBorderImpl<schar , 2>*/, 0 /*copyMakeBorderImpl<schar , 3>*/, 0 /*copyMakeBorderImpl<schar , 4>*/},
|
||||
{ copyMakeBorderImpl<ushort, 1> , 0 /*copyMakeBorderImpl<ushort, 2>*/, copyMakeBorderImpl<ushort, 3> , copyMakeBorderImpl<ushort, 4> },
|
||||
{ copyMakeBorderImpl<short , 1> , 0 /*copyMakeBorderImpl<short , 2>*/, copyMakeBorderImpl<short , 3> , copyMakeBorderImpl<short , 4> },
|
||||
{0 /*copyMakeBorderImpl<int , 1>*/, 0 /*copyMakeBorderImpl<int , 2>*/, 0 /*copyMakeBorderImpl<int , 3>*/, 0 /*copyMakeBorderImpl<int , 4>*/},
|
||||
{ copyMakeBorderImpl<float , 1> , 0 /*copyMakeBorderImpl<float , 2>*/, copyMakeBorderImpl<float , 3> , copyMakeBorderImpl<float ,4> }
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
const int depth = src.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_Assert( depth <= CV_32F && cn <= 4 );
|
||||
CV_Assert( borderType == BORDER_REFLECT_101 || borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT || borderType == BORDER_REFLECT || borderType == BORDER_WRAP );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.rows + top + bottom, src.cols + left + right, src.type(), stream);
|
||||
|
||||
const func_t func = funcs[depth][cn - 1];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, dst, top, left, borderType, value, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,113 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename D>
|
||||
void countNonZeroImpl(const GpuMat& _src, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<D>& dst = (GpuMat_<D>&) _dst;
|
||||
|
||||
gridCountNonZero(src, dst, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::countNonZero(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
countNonZeroImpl<uchar, int>,
|
||||
countNonZeroImpl<schar, int>,
|
||||
countNonZeroImpl<ushort, int>,
|
||||
countNonZeroImpl<short, int>,
|
||||
countNonZeroImpl<int, int>,
|
||||
countNonZeroImpl<float, int>,
|
||||
countNonZeroImpl<double, int>,
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
CV_Assert( src.channels() == 1 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, CV_32SC1, stream);
|
||||
|
||||
const func_t func = funcs[src.depth()];
|
||||
func(src, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
int cv::cuda::countNonZero(InputArray _src)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat buf = pool.getBuffer(1, 1, CV_32SC1);
|
||||
|
||||
countNonZero(_src, buf, stream);
|
||||
|
||||
int data;
|
||||
buf.download(Mat(1, 1, CV_32SC1, &data));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,240 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void divMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int);
|
||||
void divMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
void divMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename D> struct DivOp : binary_function<T, T, D>
|
||||
{
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return b != 0 ? saturate_cast<D>(a / b) : 0;
|
||||
}
|
||||
};
|
||||
template <typename T> struct DivOp<T, float> : binary_function<T, T, float>
|
||||
{
|
||||
__device__ __forceinline__ float operator ()(T a, T b) const
|
||||
{
|
||||
return b != 0 ? static_cast<float>(a) / b : 0.0f;
|
||||
}
|
||||
};
|
||||
template <typename T> struct DivOp<T, double> : binary_function<T, T, double>
|
||||
{
|
||||
__device__ __forceinline__ double operator ()(T a, T b) const
|
||||
{
|
||||
return b != 0 ? static_cast<double>(a) / b : 0.0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename S, typename D> struct DivScaleOp : binary_function<T, T, D>
|
||||
{
|
||||
S scale;
|
||||
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return b != 0 ? saturate_cast<D>(scale * a / b) : 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T, typename S, typename D>
|
||||
void divMatImpl(const GpuMat& src1, const GpuMat& src2, const GpuMat& dst, double scale, Stream& stream)
|
||||
{
|
||||
if (scale == 1)
|
||||
{
|
||||
DivOp<T, D> op;
|
||||
gridTransformBinary_< TransformPolicy<S> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), op, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
DivScaleOp<T, S, D> op;
|
||||
op.scale = static_cast<S>(scale);
|
||||
gridTransformBinary_< TransformPolicy<S> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void divMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, const GpuMat& dst, double scale, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX][CV_DEPTH_MAX] =
|
||||
{
|
||||
{
|
||||
divMatImpl<uchar, float, uchar>,
|
||||
divMatImpl<uchar, float, schar>,
|
||||
divMatImpl<uchar, float, ushort>,
|
||||
divMatImpl<uchar, float, short>,
|
||||
divMatImpl<uchar, float, int>,
|
||||
divMatImpl<uchar, float, float>,
|
||||
divMatImpl<uchar, double, double>
|
||||
},
|
||||
{
|
||||
divMatImpl<schar, float, uchar>,
|
||||
divMatImpl<schar, float, schar>,
|
||||
divMatImpl<schar, float, ushort>,
|
||||
divMatImpl<schar, float, short>,
|
||||
divMatImpl<schar, float, int>,
|
||||
divMatImpl<schar, float, float>,
|
||||
divMatImpl<schar, double, double>
|
||||
},
|
||||
{
|
||||
0 /*divMatImpl<ushort, float, uchar>*/,
|
||||
0 /*divMatImpl<ushort, float, schar>*/,
|
||||
divMatImpl<ushort, float, ushort>,
|
||||
divMatImpl<ushort, float, short>,
|
||||
divMatImpl<ushort, float, int>,
|
||||
divMatImpl<ushort, float, float>,
|
||||
divMatImpl<ushort, double, double>
|
||||
},
|
||||
{
|
||||
0 /*divMatImpl<short, float, uchar>*/,
|
||||
0 /*divMatImpl<short, float, schar>*/,
|
||||
divMatImpl<short, float, ushort>,
|
||||
divMatImpl<short, float, short>,
|
||||
divMatImpl<short, float, int>,
|
||||
divMatImpl<short, float, float>,
|
||||
divMatImpl<short, double, double>
|
||||
},
|
||||
{
|
||||
0 /*divMatImpl<int, float, uchar>*/,
|
||||
0 /*divMatImpl<int, float, schar>*/,
|
||||
0 /*divMatImpl<int, float, ushort>*/,
|
||||
0 /*divMatImpl<int, float, short>*/,
|
||||
divMatImpl<int, float, int>,
|
||||
divMatImpl<int, float, float>,
|
||||
divMatImpl<int, double, double>
|
||||
},
|
||||
{
|
||||
0 /*divMatImpl<float, float, uchar>*/,
|
||||
0 /*divMatImpl<float, float, schar>*/,
|
||||
0 /*divMatImpl<float, float, ushort>*/,
|
||||
0 /*divMatImpl<float, float, short>*/,
|
||||
0 /*divMatImpl<float, float, int>*/,
|
||||
divMatImpl<float, float, float>,
|
||||
divMatImpl<float, double, double>
|
||||
},
|
||||
{
|
||||
0 /*divMatImpl<double, double, uchar>*/,
|
||||
0 /*divMatImpl<double, double, schar>*/,
|
||||
0 /*divMatImpl<double, double, ushort>*/,
|
||||
0 /*divMatImpl<double, double, short>*/,
|
||||
0 /*divMatImpl<double, double, int>*/,
|
||||
0 /*divMatImpl<double, double, float>*/,
|
||||
divMatImpl<double, double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src1.depth();
|
||||
const int ddepth = dst.depth();
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_, src2_, dst_, scale, stream);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
struct DivOpSpecial : binary_function<T, float, T>
|
||||
{
|
||||
__device__ __forceinline__ T operator ()(const T& a, float b) const
|
||||
{
|
||||
typedef typename VecTraits<T>::elem_type elem_type;
|
||||
|
||||
T res = VecTraits<T>::all(0);
|
||||
|
||||
if (b != 0)
|
||||
{
|
||||
b = 1.0f / b;
|
||||
res.x = saturate_cast<elem_type>(a.x * b);
|
||||
res.y = saturate_cast<elem_type>(a.y * b);
|
||||
res.z = saturate_cast<elem_type>(a.z * b);
|
||||
res.w = saturate_cast<elem_type>(a.w * b);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void divMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary(globPtr<uchar4>(src1), globPtr<float>(src2), globPtr<uchar4>(dst), DivOpSpecial<uchar4>(), stream);
|
||||
}
|
||||
|
||||
void divMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary(globPtr<short4>(src1), globPtr<float>(src2), globPtr<short4>(dst), DivOpSpecial<short4>(), stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,262 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/cuda/cuda_compat.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void divScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
using cv::cuda::device::compat::double4Compat;
|
||||
template <typename T, int cn> struct SafeDiv;
|
||||
template <typename T> struct SafeDiv<T, 1>
|
||||
{
|
||||
__device__ __forceinline__ static T op(T a, T b)
|
||||
{
|
||||
return b != 0 ? a / b : 0;
|
||||
}
|
||||
};
|
||||
template <typename T> struct SafeDiv<T, 2>
|
||||
{
|
||||
__device__ __forceinline__ static T op(const T& a, const T& b)
|
||||
{
|
||||
T res;
|
||||
|
||||
res.x = b.x != 0 ? a.x / b.x : 0;
|
||||
res.y = b.y != 0 ? a.y / b.y : 0;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
template <typename T> struct SafeDiv<T, 3>
|
||||
{
|
||||
__device__ __forceinline__ static T op(const T& a, const T& b)
|
||||
{
|
||||
T res;
|
||||
|
||||
res.x = b.x != 0 ? a.x / b.x : 0;
|
||||
res.y = b.y != 0 ? a.y / b.y : 0;
|
||||
res.z = b.z != 0 ? a.z / b.z : 0;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
template <typename T> struct SafeDiv<T, 4>
|
||||
{
|
||||
__device__ __forceinline__ static T op(const T& a, const T& b)
|
||||
{
|
||||
T res;
|
||||
|
||||
res.x = b.x != 0 ? a.x / b.x : 0;
|
||||
res.y = b.y != 0 ? a.y / b.y : 0;
|
||||
res.z = b.z != 0 ? a.z / b.z : 0;
|
||||
res.w = b.w != 0 ? a.w / b.w : 0;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct DivScalarOp : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(SafeDiv<ScalarType, VecTraits<ScalarType>::cn>::op(saturate_cast<ScalarType>(a), val));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct DivScalarOpInv : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(SafeDiv<ScalarType, VecTraits<ScalarType>::cn>::op(val, saturate_cast<ScalarType>(a)));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarDepth, typename DstType>
|
||||
void divScalarImpl(const GpuMat& src, cv::Scalar value, bool inv, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<ScalarDepth, VecTraits<SrcType>::cn>::type ScalarType;
|
||||
|
||||
cv::Scalar_<ScalarDepth> value_ = value;
|
||||
|
||||
if (inv)
|
||||
{
|
||||
DivScalarOpInv<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
DivScalarOp<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void divScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[7][7][4] =
|
||||
{
|
||||
{
|
||||
{divScalarImpl<uchar, float, uchar>, divScalarImpl<uchar2, float, uchar2>, divScalarImpl<uchar3, float, uchar3>, divScalarImpl<uchar4, float, uchar4>},
|
||||
{divScalarImpl<uchar, float, schar>, divScalarImpl<uchar2, float, char2>, divScalarImpl<uchar3, float, char3>, divScalarImpl<uchar4, float, char4>},
|
||||
{divScalarImpl<uchar, float, ushort>, divScalarImpl<uchar2, float, ushort2>, divScalarImpl<uchar3, float, ushort3>, divScalarImpl<uchar4, float, ushort4>},
|
||||
{divScalarImpl<uchar, float, short>, divScalarImpl<uchar2, float, short2>, divScalarImpl<uchar3, float, short3>, divScalarImpl<uchar4, float, short4>},
|
||||
{divScalarImpl<uchar, float, int>, divScalarImpl<uchar2, float, int2>, divScalarImpl<uchar3, float, int3>, divScalarImpl<uchar4, float, int4>},
|
||||
{divScalarImpl<uchar, float, float>, divScalarImpl<uchar2, float, float2>, divScalarImpl<uchar3, float, float3>, divScalarImpl<uchar4, float, float4>},
|
||||
{divScalarImpl<uchar, double, double>, divScalarImpl<uchar2, double, double2>, divScalarImpl<uchar3, double, double3>, divScalarImpl<uchar4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{divScalarImpl<schar, float, uchar>, divScalarImpl<char2, float, uchar2>, divScalarImpl<char3, float, uchar3>, divScalarImpl<char4, float, uchar4>},
|
||||
{divScalarImpl<schar, float, schar>, divScalarImpl<char2, float, char2>, divScalarImpl<char3, float, char3>, divScalarImpl<char4, float, char4>},
|
||||
{divScalarImpl<schar, float, ushort>, divScalarImpl<char2, float, ushort2>, divScalarImpl<char3, float, ushort3>, divScalarImpl<char4, float, ushort4>},
|
||||
{divScalarImpl<schar, float, short>, divScalarImpl<char2, float, short2>, divScalarImpl<char3, float, short3>, divScalarImpl<char4, float, short4>},
|
||||
{divScalarImpl<schar, float, int>, divScalarImpl<char2, float, int2>, divScalarImpl<char3, float, int3>, divScalarImpl<char4, float, int4>},
|
||||
{divScalarImpl<schar, float, float>, divScalarImpl<char2, float, float2>, divScalarImpl<char3, float, float3>, divScalarImpl<char4, float, float4>},
|
||||
{divScalarImpl<schar, double, double>, divScalarImpl<char2, double, double2>, divScalarImpl<char3, double, double3>, divScalarImpl<char4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*divScalarImpl<ushort, float, uchar>*/, 0 /*divScalarImpl<ushort2, float, uchar2>*/, 0 /*divScalarImpl<ushort3, float, uchar3>*/, 0 /*divScalarImpl<ushort4, float, uchar4>*/},
|
||||
{0 /*divScalarImpl<ushort, float, schar>*/, 0 /*divScalarImpl<ushort2, float, char2>*/, 0 /*divScalarImpl<ushort3, float, char3>*/, 0 /*divScalarImpl<ushort4, float, char4>*/},
|
||||
{divScalarImpl<ushort, float, ushort>, divScalarImpl<ushort2, float, ushort2>, divScalarImpl<ushort3, float, ushort3>, divScalarImpl<ushort4, float, ushort4>},
|
||||
{divScalarImpl<ushort, float, short>, divScalarImpl<ushort2, float, short2>, divScalarImpl<ushort3, float, short3>, divScalarImpl<ushort4, float, short4>},
|
||||
{divScalarImpl<ushort, float, int>, divScalarImpl<ushort2, float, int2>, divScalarImpl<ushort3, float, int3>, divScalarImpl<ushort4, float, int4>},
|
||||
{divScalarImpl<ushort, float, float>, divScalarImpl<ushort2, float, float2>, divScalarImpl<ushort3, float, float3>, divScalarImpl<ushort4, float, float4>},
|
||||
{divScalarImpl<ushort, double, double>, divScalarImpl<ushort2, double, double2>, divScalarImpl<ushort3, double, double3>, divScalarImpl<ushort4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*divScalarImpl<short, float, uchar>*/, 0 /*divScalarImpl<short2, float, uchar2>*/, 0 /*divScalarImpl<short3, float, uchar3>*/, 0 /*divScalarImpl<short4, float, uchar4>*/},
|
||||
{0 /*divScalarImpl<short, float, schar>*/, 0 /*divScalarImpl<short2, float, char2>*/, 0 /*divScalarImpl<short3, float, char3>*/, 0 /*divScalarImpl<short4, float, char4>*/},
|
||||
{divScalarImpl<short, float, ushort>, divScalarImpl<short2, float, ushort2>, divScalarImpl<short3, float, ushort3>, divScalarImpl<short4, float, ushort4>},
|
||||
{divScalarImpl<short, float, short>, divScalarImpl<short2, float, short2>, divScalarImpl<short3, float, short3>, divScalarImpl<short4, float, short4>},
|
||||
{divScalarImpl<short, float, int>, divScalarImpl<short2, float, int2>, divScalarImpl<short3, float, int3>, divScalarImpl<short4, float, int4>},
|
||||
{divScalarImpl<short, float, float>, divScalarImpl<short2, float, float2>, divScalarImpl<short3, float, float3>, divScalarImpl<short4, float, float4>},
|
||||
{divScalarImpl<short, double, double>, divScalarImpl<short2, double, double2>, divScalarImpl<short3, double, double3>, divScalarImpl<short4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*divScalarImpl<int, float, uchar>*/, 0 /*divScalarImpl<int2, float, uchar2>*/, 0 /*divScalarImpl<int3, float, uchar3>*/, 0 /*divScalarImpl<int4, float, uchar4>*/},
|
||||
{0 /*divScalarImpl<int, float, schar>*/, 0 /*divScalarImpl<int2, float, char2>*/, 0 /*divScalarImpl<int3, float, char3>*/, 0 /*divScalarImpl<int4, float, char4>*/},
|
||||
{0 /*divScalarImpl<int, float, ushort>*/, 0 /*divScalarImpl<int2, float, ushort2>*/, 0 /*divScalarImpl<int3, float, ushort3>*/, 0 /*divScalarImpl<int4, float, ushort4>*/},
|
||||
{0 /*divScalarImpl<int, float, short>*/, 0 /*divScalarImpl<int2, float, short2>*/, 0 /*divScalarImpl<int3, float, short3>*/, 0 /*divScalarImpl<int4, float, short4>*/},
|
||||
{divScalarImpl<int, float, int>, divScalarImpl<int2, float, int2>, divScalarImpl<int3, float, int3>, divScalarImpl<int4, float, int4>},
|
||||
{divScalarImpl<int, float, float>, divScalarImpl<int2, float, float2>, divScalarImpl<int3, float, float3>, divScalarImpl<int4, float, float4>},
|
||||
{divScalarImpl<int, double, double>, divScalarImpl<int2, double, double2>, divScalarImpl<int3, double, double3>, divScalarImpl<int4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*divScalarImpl<float, float, uchar>*/, 0 /*divScalarImpl<float2, float, uchar2>*/, 0 /*divScalarImpl<float3, float, uchar3>*/, 0 /*divScalarImpl<float4, float, uchar4>*/},
|
||||
{0 /*divScalarImpl<float, float, schar>*/, 0 /*divScalarImpl<float2, float, char2>*/, 0 /*divScalarImpl<float3, float, char3>*/, 0 /*divScalarImpl<float4, float, char4>*/},
|
||||
{0 /*divScalarImpl<float, float, ushort>*/, 0 /*divScalarImpl<float2, float, ushort2>*/, 0 /*divScalarImpl<float3, float, ushort3>*/, 0 /*divScalarImpl<float4, float, ushort4>*/},
|
||||
{0 /*divScalarImpl<float, float, short>*/, 0 /*divScalarImpl<float2, float, short2>*/, 0 /*divScalarImpl<float3, float, short3>*/, 0 /*divScalarImpl<float4, float, short4>*/},
|
||||
{0 /*divScalarImpl<float, float, int>*/, 0 /*divScalarImpl<float2, float, int2>*/, 0 /*divScalarImpl<float3, float, int3>*/, 0 /*divScalarImpl<float4, float, int4>*/},
|
||||
{divScalarImpl<float, float, float>, divScalarImpl<float2, float, float2>, divScalarImpl<float3, float, float3>, divScalarImpl<float4, float, float4>},
|
||||
{divScalarImpl<float, double, double>, divScalarImpl<float2, double, double2>, divScalarImpl<float3, double, double3>, divScalarImpl<float4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*divScalarImpl<double, double, uchar>*/, 0 /*divScalarImpl<double2, double, uchar2>*/, 0 /*divScalarImpl<double3, double, uchar3>*/, 0 /*divScalarImpl<double4, double, uchar4>*/},
|
||||
{0 /*divScalarImpl<double, double, schar>*/, 0 /*divScalarImpl<double2, double, char2>*/, 0 /*divScalarImpl<double3, double, char3>*/, 0 /*divScalarImpl<double4, double, char4>*/},
|
||||
{0 /*divScalarImpl<double, double, ushort>*/, 0 /*divScalarImpl<double2, double, ushort2>*/, 0 /*divScalarImpl<double3, double, ushort3>*/, 0 /*divScalarImpl<double4, double, ushort4>*/},
|
||||
{0 /*divScalarImpl<double, double, short>*/, 0 /*divScalarImpl<double2, double, short2>*/, 0 /*divScalarImpl<double3, double, short3>*/, 0 /*divScalarImpl<double4, double, short4>*/},
|
||||
{0 /*divScalarImpl<double, double, int>*/, 0 /*divScalarImpl<double2, double, int2>*/, 0 /*divScalarImpl<double3, double, int3>*/, 0 /*divScalarImpl<double4, double, int4>*/},
|
||||
{0 /*divScalarImpl<double, double, float>*/, 0 /*divScalarImpl<double2, double, float2>*/, 0 /*divScalarImpl<double3, double, float3>*/, 0 /*divScalarImpl<double4, double, float4>*/},
|
||||
{divScalarImpl<double, double, double>, divScalarImpl<double2, double, double2>, divScalarImpl<double3, double, double3>, divScalarImpl<double4Compat, double, double4Compat>}
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src.depth();
|
||||
const int ddepth = dst.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F && cn <= 4 );
|
||||
|
||||
if (inv)
|
||||
{
|
||||
val[0] *= scale;
|
||||
val[1] *= scale;
|
||||
val[2] *= scale;
|
||||
val[3] *= scale;
|
||||
}
|
||||
else
|
||||
{
|
||||
val[0] /= scale;
|
||||
val[1] /= scale;
|
||||
val[2] /= scale;
|
||||
val[3] /= scale;
|
||||
}
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth][cn - 1];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, val, inv, dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,99 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T, int cn>
|
||||
void inRangeImpl(const GpuMat& src,
|
||||
const Scalar& lowerb,
|
||||
const Scalar& upperb,
|
||||
GpuMat& dst,
|
||||
Stream& stream) {
|
||||
gridTransformUnary(globPtr<typename MakeVec<T, cn>::type>(src),
|
||||
globPtr<uchar>(dst),
|
||||
InRangeFunc<T, cn>(lowerb, upperb),
|
||||
stream);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void cv::cuda::inRange(InputArray _src,
|
||||
const Scalar& _lowerb,
|
||||
const Scalar& _upperb,
|
||||
OutputArray _dst,
|
||||
Stream& stream) {
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
typedef void (*func_t)(const GpuMat& src,
|
||||
const Scalar& lowerb,
|
||||
const Scalar& upperb,
|
||||
GpuMat& dst,
|
||||
Stream& stream);
|
||||
|
||||
// Note: We cannot support 16F with the current implementation because we
|
||||
// use a CUDA vector (e.g. int3) to store the bounds, and there is no CUDA
|
||||
// vector type for float16
|
||||
static constexpr const int MAX_CHANNELS = 4;
|
||||
static constexpr const int NUM_DEPTHS = CV_64F + 1;
|
||||
|
||||
static const std::array<std::array<func_t, NUM_DEPTHS>, MAX_CHANNELS>
|
||||
funcs = {std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 1>,
|
||||
inRangeImpl<schar, 1>,
|
||||
inRangeImpl<ushort, 1>,
|
||||
inRangeImpl<short, 1>,
|
||||
inRangeImpl<int, 1>,
|
||||
inRangeImpl<float, 1>,
|
||||
inRangeImpl<double, 1>},
|
||||
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 2>,
|
||||
inRangeImpl<schar, 2>,
|
||||
inRangeImpl<ushort, 2>,
|
||||
inRangeImpl<short, 2>,
|
||||
inRangeImpl<int, 2>,
|
||||
inRangeImpl<float, 2>,
|
||||
inRangeImpl<double, 2>},
|
||||
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 3>,
|
||||
inRangeImpl<schar, 3>,
|
||||
inRangeImpl<ushort, 3>,
|
||||
inRangeImpl<short, 3>,
|
||||
inRangeImpl<int, 3>,
|
||||
inRangeImpl<float, 3>,
|
||||
inRangeImpl<double, 3>},
|
||||
std::array<func_t, NUM_DEPTHS>{inRangeImpl<uchar, 4>,
|
||||
inRangeImpl<schar, 4>,
|
||||
inRangeImpl<ushort, 4>,
|
||||
inRangeImpl<short, 4>,
|
||||
inRangeImpl<int, 4>,
|
||||
inRangeImpl<float, 4>,
|
||||
inRangeImpl<double, 4>}};
|
||||
|
||||
CV_CheckLE(src.channels(), MAX_CHANNELS, "Src must have <= 4 channels");
|
||||
CV_CheckLE(src.depth(),
|
||||
CV_64F,
|
||||
"Src must have depth 8U, 8S, 16U, 16S, 32S, 32F, or 64F");
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), CV_8UC1, stream);
|
||||
|
||||
const func_t func = funcs.at(src.channels() - 1).at(src.depth());
|
||||
func(src, _lowerb, _upperb, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,107 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// integral
|
||||
|
||||
void cv::cuda::integral(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.type() == CV_8UC1 );
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat_<int> res(src.size(), pool.getAllocator());
|
||||
|
||||
gridIntegral(globPtr<uchar>(src), res, stream);
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.rows + 1, src.cols + 1, CV_32SC1, stream);
|
||||
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
|
||||
GpuMat inner = dst(Rect(1, 1, src.cols, src.rows));
|
||||
res.copyTo(inner, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// sqrIntegral
|
||||
|
||||
void cv::cuda::sqrIntegral(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.type() == CV_8UC1 );
|
||||
|
||||
BufferPool pool(Stream::Null());
|
||||
GpuMat_<double> res(pool.getBuffer(src.size(), CV_64FC1));
|
||||
|
||||
gridIntegral(sqr_(cvt_<int>(globPtr<uchar>(src))), res, stream);
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.rows + 1, src.cols + 1, CV_64FC1, stream);
|
||||
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
|
||||
GpuMat inner = dst(Rect(1, 1, src.cols, src.rows));
|
||||
res.copyTo(inner, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,136 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "../lut.hpp"
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
#include <opencv2/cudev/ptr2d/texture.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace cv { namespace cuda {
|
||||
|
||||
LookUpTableImpl::LookUpTableImpl(InputArray _lut)
|
||||
{
|
||||
if (_lut.kind() == _InputArray::CUDA_GPU_MAT)
|
||||
{
|
||||
d_lut = _lut.getGpuMat();
|
||||
}
|
||||
else
|
||||
{
|
||||
Mat h_lut = _lut.getMat();
|
||||
d_lut.upload(Mat(1, 256, h_lut.type(), h_lut.data));
|
||||
}
|
||||
CV_Assert( d_lut.depth() == CV_8U );
|
||||
CV_Assert( d_lut.rows == 1 && d_lut.cols == 256 );
|
||||
szInBytes = 256 * d_lut.channels() * sizeof(uchar);
|
||||
}
|
||||
|
||||
struct LutTablePtrC1
|
||||
{
|
||||
typedef uchar value_type;
|
||||
typedef uchar index_type;
|
||||
cv::cudev::TexturePtr<uchar> tex;
|
||||
__device__ __forceinline__ uchar operator ()(uchar, uchar x) const {
|
||||
return tex(x);
|
||||
}
|
||||
};
|
||||
|
||||
struct LutTablePtrC3
|
||||
{
|
||||
typedef uchar3 value_type;
|
||||
typedef uchar3 index_type;
|
||||
cv::cudev::TexturePtr<uchar> tex;
|
||||
__device__ __forceinline__ uchar3 operator ()(const uchar3&, const uchar3& x) const {
|
||||
return make_uchar3(tex(x.x * 3), tex(x.y * 3 + 1), tex(x.z * 3 + 2));
|
||||
}
|
||||
};
|
||||
|
||||
void LookUpTableImpl::transform(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
const int cn = src.channels();
|
||||
const int lut_cn = d_lut.channels();
|
||||
|
||||
CV_Assert( src.type() == CV_8UC1 || src.type() == CV_8UC3 );
|
||||
CV_Assert( lut_cn == 1 || lut_cn == cn );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
if (lut_cn == 1)
|
||||
{
|
||||
GpuMat_<uchar> src1(src.reshape(1));
|
||||
GpuMat_<uchar> dst1(dst.reshape(1));
|
||||
cv::cudev::Texture<uchar> tex(szInBytes, reinterpret_cast<uchar*>(d_lut.data));
|
||||
LutTablePtrC1 tbl;
|
||||
tbl.tex = TexturePtr<uchar>(tex);
|
||||
dst1.assign(lut_(src1, tbl), stream);
|
||||
}
|
||||
else if (lut_cn == 3)
|
||||
{
|
||||
GpuMat_<uchar3>& src3 = (GpuMat_<uchar3>&) src;
|
||||
GpuMat_<uchar3>& dst3 = (GpuMat_<uchar3>&) dst;
|
||||
cv::cudev::Texture<uchar> tex(szInBytes, reinterpret_cast<uchar*>(d_lut.data));
|
||||
LutTablePtrC3 tbl;
|
||||
tbl.tex = TexturePtr<uchar>(tex);
|
||||
dst3.assign(lut_(src3, tbl), stream);
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
} }
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,341 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// abs
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
void absMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), abs_func<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::abs(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
absMat<uchar>,
|
||||
absMat<schar>,
|
||||
absMat<ushort>,
|
||||
absMat<short>,
|
||||
absMat<int>,
|
||||
absMat<float>,
|
||||
absMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// sqr
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T> struct SqrOp : unary_function<T, T>
|
||||
{
|
||||
__device__ __forceinline__ T operator ()(T x) const
|
||||
{
|
||||
return cudev::saturate_cast<T>(x * x);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void sqrMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), SqrOp<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::sqr(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
sqrMat<uchar>,
|
||||
sqrMat<schar>,
|
||||
sqrMat<ushort>,
|
||||
sqrMat<short>,
|
||||
sqrMat<int>,
|
||||
sqrMat<float>,
|
||||
sqrMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// sqrt
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
void sqrtMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), sqrt_func<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::sqrt(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
sqrtMat<uchar>,
|
||||
sqrtMat<schar>,
|
||||
sqrtMat<ushort>,
|
||||
sqrtMat<short>,
|
||||
sqrtMat<int>,
|
||||
sqrtMat<float>,
|
||||
sqrtMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/// exp
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T> struct ExpOp : unary_function<T, T>
|
||||
{
|
||||
__device__ __forceinline__ T operator ()(T x) const
|
||||
{
|
||||
exp_func<T> f;
|
||||
return cudev::saturate_cast<T>(f(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void expMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), ExpOp<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::exp(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
expMat<uchar>,
|
||||
expMat<schar>,
|
||||
expMat<ushort>,
|
||||
expMat<short>,
|
||||
expMat<int>,
|
||||
expMat<float>,
|
||||
expMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// log
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
void logMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), log_func<T>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::log(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
logMat<uchar>,
|
||||
logMat<schar>,
|
||||
logMat<ushort>,
|
||||
logMat<short>,
|
||||
logMat<int>,
|
||||
logMat<float>,
|
||||
logMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// pow
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename T, bool Signed = numeric_limits<T>::is_signed> struct PowOp : unary_function<T, T>
|
||||
{
|
||||
typedef typename LargerType<T, float>::type LargerType;
|
||||
LargerType power;
|
||||
|
||||
__device__ __forceinline__ T operator()(T e) const
|
||||
{
|
||||
T res = cudev::saturate_cast<T>(__powf(e < 0 ? -e : e, power));
|
||||
|
||||
if ((e < 0) && (1 & static_cast<int>(power)))
|
||||
res *= -1;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> struct PowOp<T, false> : unary_function<T, T>
|
||||
{
|
||||
typedef typename LargerType<T, float>::type LargerType;
|
||||
LargerType power;
|
||||
|
||||
__device__ __forceinline__ T operator()(T e) const
|
||||
{
|
||||
return cudev::saturate_cast<T>(__powf(e, power));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void powMat(const GpuMat& src, double power, const GpuMat& dst, Stream& stream)
|
||||
{
|
||||
PowOp<T> op;
|
||||
op.power = static_cast<typename LargerType<T, float>::type>(power);
|
||||
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::pow(InputArray _src, double power, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, double power, const GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
powMat<uchar>,
|
||||
powMat<schar>,
|
||||
powMat<ushort>,
|
||||
powMat<short>,
|
||||
powMat<int>,
|
||||
powMat<float>,
|
||||
powMat<double>
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() <= CV_64F );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()](src.reshape(1), power, dst.reshape(1), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,193 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename R>
|
||||
void minMaxImpl(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<R>& dst = (GpuMat_<R>&) _dst;
|
||||
|
||||
if (mask.empty())
|
||||
gridFindMinMaxVal(src, dst, stream);
|
||||
else
|
||||
gridFindMinMaxVal(src, dst, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
|
||||
template <typename T, typename R>
|
||||
void minMaxImpl(const GpuMat& src, const GpuMat& mask, double* minVal, double* maxVal)
|
||||
{
|
||||
BufferPool pool(Stream::Null());
|
||||
GpuMat buf(pool.getBuffer(1, 2, DataType<R>::type));
|
||||
|
||||
minMaxImpl<T, R>(src, mask, buf, Stream::Null());
|
||||
|
||||
R data[2];
|
||||
buf.download(Mat(1, 2, buf.type(), data));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::findMinMax(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
minMaxImpl<uchar, int>,
|
||||
minMaxImpl<schar, int>,
|
||||
minMaxImpl<ushort, int>,
|
||||
minMaxImpl<short, int>,
|
||||
minMaxImpl<int, int>,
|
||||
minMaxImpl<float, float>,
|
||||
minMaxImpl<double, double>
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( src.channels() == 1 );
|
||||
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int dst_depth = src_depth < CV_32F ? CV_32S : src_depth;
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 2, dst_depth, stream);
|
||||
|
||||
const func_t func = funcs[src.depth()];
|
||||
CV_Assert(func);
|
||||
|
||||
func(src, mask, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::minMax(InputArray _src, double* minVal, double* maxVal, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
findMinMax(_src, dst, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double vals[2];
|
||||
dst.createMatHeader().convertTo(Mat(1, 2, CV_64FC1, &vals[0]), CV_64F);
|
||||
|
||||
if (minVal)
|
||||
*minVal = vals[0];
|
||||
|
||||
if (maxVal)
|
||||
*maxVal = vals[1];
|
||||
}
|
||||
|
||||
namespace cv { namespace cuda { namespace device {
|
||||
|
||||
void findMaxAbs(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream);
|
||||
|
||||
}}}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename R>
|
||||
void findMaxAbsImpl(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<R>& dst = (GpuMat_<R>&) _dst;
|
||||
|
||||
if (mask.empty())
|
||||
gridFindMaxVal(abs_(src), dst, stream);
|
||||
else
|
||||
gridFindMaxVal(abs_(src), dst, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::device::findMaxAbs(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
findMaxAbsImpl<uchar, int>,
|
||||
findMaxAbsImpl<schar, int>,
|
||||
findMaxAbsImpl<ushort, int>,
|
||||
findMaxAbsImpl<short, int>,
|
||||
findMaxAbsImpl<int, int>,
|
||||
findMaxAbsImpl<float, float>,
|
||||
findMaxAbsImpl<double, double>
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( src.channels() == 1 );
|
||||
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int dst_depth = src_depth < CV_32F ? CV_32S : src_depth;
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, dst_depth, stream);
|
||||
|
||||
const func_t func = funcs[src.depth()];
|
||||
CV_Assert(func);
|
||||
|
||||
func(src, mask, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,242 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void minMaxMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int op);
|
||||
|
||||
void minMaxScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int op);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// minMaxMat
|
||||
|
||||
namespace
|
||||
{
|
||||
template <template <typename> class Op, typename T>
|
||||
void minMaxMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary(globPtr<T>(src1), globPtr<T>(src2), globPtr<T>(dst), Op<T>(), stream);
|
||||
}
|
||||
|
||||
struct MinOp2 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vmin2(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
struct MaxOp2 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vmax2(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
template <class Op2>
|
||||
void minMaxMat_v2(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 1;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, Op2(), stream);
|
||||
}
|
||||
|
||||
struct MinOp4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vmin4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
struct MaxOp4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vmax4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
template <class Op4>
|
||||
void minMaxMat_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, Op4(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void minMaxMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int op)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs_v1[2][CV_DEPTH_MAX] =
|
||||
{
|
||||
{
|
||||
minMaxMat_v1<minimum, uchar>,
|
||||
minMaxMat_v1<minimum, schar>,
|
||||
minMaxMat_v1<minimum, ushort>,
|
||||
minMaxMat_v1<minimum, short>,
|
||||
minMaxMat_v1<minimum, int>,
|
||||
minMaxMat_v1<minimum, float>,
|
||||
minMaxMat_v1<minimum, double>
|
||||
},
|
||||
{
|
||||
minMaxMat_v1<maximum, uchar>,
|
||||
minMaxMat_v1<maximum, schar>,
|
||||
minMaxMat_v1<maximum, ushort>,
|
||||
minMaxMat_v1<maximum, short>,
|
||||
minMaxMat_v1<maximum, int>,
|
||||
minMaxMat_v1<maximum, float>,
|
||||
minMaxMat_v1<maximum, double>
|
||||
}
|
||||
};
|
||||
|
||||
static const func_t funcs_v2[2] =
|
||||
{
|
||||
minMaxMat_v2<MinOp2>, minMaxMat_v2<MaxOp2>
|
||||
};
|
||||
|
||||
static const func_t funcs_v4[2] =
|
||||
{
|
||||
minMaxMat_v4<MinOp4>, minMaxMat_v4<MaxOp4>
|
||||
};
|
||||
|
||||
const int depth = src1.depth();
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
if (depth == CV_8U || depth == CV_16U)
|
||||
{
|
||||
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
|
||||
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
|
||||
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
|
||||
|
||||
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
|
||||
|
||||
if (isAllAligned)
|
||||
{
|
||||
if (depth == CV_8U && (src1_.cols & 3) == 0)
|
||||
{
|
||||
funcs_v4[op](src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
else if (depth == CV_16U && (src1_.cols & 1) == 0)
|
||||
{
|
||||
funcs_v2[op](src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const func_t func = funcs_v1[op][depth];
|
||||
CV_Assert(func);
|
||||
|
||||
func(src1_, src2_, dst_, stream);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
/// minMaxScalar
|
||||
|
||||
namespace
|
||||
{
|
||||
template <template <typename> class Op, typename T>
|
||||
void minMaxScalar(const GpuMat& src, double value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformUnary(globPtr<T>(src), globPtr<T>(dst), bind2nd(Op<T>(), cv::saturate_cast<T>(value)), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void minMaxScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int op)
|
||||
{
|
||||
CV_DbgAssert( src.channels() == 1 );
|
||||
|
||||
typedef void (*func_t)(const GpuMat& src, double value, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[2][CV_DEPTH_MAX] =
|
||||
{
|
||||
{
|
||||
minMaxScalar<minimum, uchar>,
|
||||
minMaxScalar<minimum, schar>,
|
||||
minMaxScalar<minimum, ushort>,
|
||||
minMaxScalar<minimum, short>,
|
||||
minMaxScalar<minimum, int>,
|
||||
minMaxScalar<minimum, float>,
|
||||
minMaxScalar<minimum, double>
|
||||
},
|
||||
{
|
||||
minMaxScalar<maximum, uchar>,
|
||||
minMaxScalar<maximum, schar>,
|
||||
minMaxScalar<maximum, ushort>,
|
||||
minMaxScalar<maximum, short>,
|
||||
minMaxScalar<maximum, int>,
|
||||
minMaxScalar<maximum, float>,
|
||||
minMaxScalar<maximum, double>
|
||||
}
|
||||
};
|
||||
|
||||
auto f = funcs[op][src.depth()];
|
||||
CV_Assert(f);
|
||||
|
||||
f(src, value[0], dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,160 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename R>
|
||||
void minMaxLocImpl(const GpuMat& _src, const GpuMat& mask, GpuMat& _valBuf, GpuMat& _locBuf, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<R>& valBuf = (GpuMat_<R>&) _valBuf;
|
||||
GpuMat_<int>& locBuf = (GpuMat_<int>&) _locBuf;
|
||||
|
||||
if (mask.empty())
|
||||
gridMinMaxLoc(src, valBuf, locBuf, stream);
|
||||
else
|
||||
gridMinMaxLoc(src, valBuf, locBuf, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::findMinMaxLoc(InputArray _src, OutputArray _minMaxVals, OutputArray _loc, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _valBuf, GpuMat& _locBuf, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
minMaxLocImpl<uchar, int>,
|
||||
minMaxLocImpl<schar, int>,
|
||||
minMaxLocImpl<ushort, int>,
|
||||
minMaxLocImpl<short, int>,
|
||||
minMaxLocImpl<int, int>,
|
||||
minMaxLocImpl<float, float>,
|
||||
minMaxLocImpl<double, double>
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( src.channels() == 1 );
|
||||
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat valBuf(pool.getAllocator());
|
||||
GpuMat locBuf(pool.getAllocator());
|
||||
|
||||
const func_t func = funcs[src_depth];
|
||||
CV_Assert(func);
|
||||
func(src, mask, valBuf, locBuf, stream);
|
||||
|
||||
GpuMat minMaxVals = valBuf.colRange(0, 1);
|
||||
GpuMat loc = locBuf.colRange(0, 1);
|
||||
|
||||
if (_minMaxVals.kind() == _InputArray::CUDA_GPU_MAT)
|
||||
{
|
||||
minMaxVals.copyTo(_minMaxVals, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
minMaxVals.download(_minMaxVals, stream);
|
||||
}
|
||||
|
||||
if (_loc.kind() == _InputArray::CUDA_GPU_MAT)
|
||||
{
|
||||
loc.copyTo(_loc, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
loc.download(_loc, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::minMaxLoc(InputArray _src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem minMaxVals, locVals;
|
||||
findMinMaxLoc(_src, minMaxVals, locVals, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double vals[2];
|
||||
minMaxVals.createMatHeader().convertTo(Mat(minMaxVals.size(), CV_64FC1, &vals[0]), CV_64F);
|
||||
|
||||
int locs[2];
|
||||
locVals.createMatHeader().copyTo(Mat(locVals.size(), CV_32SC1, &locs[0]));
|
||||
Size size = _src.size();
|
||||
cv::Point locs2D[] = {
|
||||
cv::Point(locs[0] % size.width, locs[0] / size.width),
|
||||
cv::Point(locs[1] % size.width, locs[1] / size.width),
|
||||
};
|
||||
|
||||
if (minVal)
|
||||
*minVal = vals[0];
|
||||
|
||||
if (maxVal)
|
||||
*maxVal = vals[1];
|
||||
|
||||
if (minLoc)
|
||||
*minLoc = locs2D[0];
|
||||
|
||||
if (maxLoc)
|
||||
*maxLoc = locs2D[1];
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,224 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void mulMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int);
|
||||
void mulMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
void mulMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename D> struct MulOp : binary_function<T, T, D>
|
||||
{
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return saturate_cast<D>(a * b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename S, typename D> struct MulScaleOp : binary_function<T, T, D>
|
||||
{
|
||||
S scale;
|
||||
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return saturate_cast<D>(scale * a * b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T, typename S, typename D>
|
||||
void mulMatImpl(const GpuMat& src1, const GpuMat& src2, const GpuMat& dst, double scale, Stream& stream)
|
||||
{
|
||||
if (scale == 1)
|
||||
{
|
||||
MulOp<T, D> op;
|
||||
gridTransformBinary_< TransformPolicy<S> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), op, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
MulScaleOp<T, S, D> op;
|
||||
op.scale = static_cast<S>(scale);
|
||||
gridTransformBinary_< TransformPolicy<S> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mulMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, const GpuMat& dst, double scale, Stream& stream);
|
||||
static const func_t funcs[7][7] =
|
||||
{
|
||||
{
|
||||
mulMatImpl<uchar, float, uchar>,
|
||||
mulMatImpl<uchar, float, schar>,
|
||||
mulMatImpl<uchar, float, ushort>,
|
||||
mulMatImpl<uchar, float, short>,
|
||||
mulMatImpl<uchar, float, int>,
|
||||
mulMatImpl<uchar, float, float>,
|
||||
mulMatImpl<uchar, double, double>
|
||||
},
|
||||
{
|
||||
mulMatImpl<schar, float, uchar>,
|
||||
mulMatImpl<schar, float, schar>,
|
||||
mulMatImpl<schar, float, ushort>,
|
||||
mulMatImpl<schar, float, short>,
|
||||
mulMatImpl<schar, float, int>,
|
||||
mulMatImpl<schar, float, float>,
|
||||
mulMatImpl<schar, double, double>
|
||||
},
|
||||
{
|
||||
0 /*mulMatImpl<ushort, float, uchar>*/,
|
||||
0 /*mulMatImpl<ushort, float, schar>*/,
|
||||
mulMatImpl<ushort, float, ushort>,
|
||||
mulMatImpl<ushort, float, short>,
|
||||
mulMatImpl<ushort, float, int>,
|
||||
mulMatImpl<ushort, float, float>,
|
||||
mulMatImpl<ushort, double, double>
|
||||
},
|
||||
{
|
||||
0 /*mulMatImpl<short, float, uchar>*/,
|
||||
0 /*mulMatImpl<short, float, schar>*/,
|
||||
mulMatImpl<short, float, ushort>,
|
||||
mulMatImpl<short, float, short>,
|
||||
mulMatImpl<short, float, int>,
|
||||
mulMatImpl<short, float, float>,
|
||||
mulMatImpl<short, double, double>
|
||||
},
|
||||
{
|
||||
0 /*mulMatImpl<int, float, uchar>*/,
|
||||
0 /*mulMatImpl<int, float, schar>*/,
|
||||
0 /*mulMatImpl<int, float, ushort>*/,
|
||||
0 /*mulMatImpl<int, float, short>*/,
|
||||
mulMatImpl<int, float, int>,
|
||||
mulMatImpl<int, float, float>,
|
||||
mulMatImpl<int, double, double>
|
||||
},
|
||||
{
|
||||
0 /*mulMatImpl<float, float, uchar>*/,
|
||||
0 /*mulMatImpl<float, float, schar>*/,
|
||||
0 /*mulMatImpl<float, float, ushort>*/,
|
||||
0 /*mulMatImpl<float, float, short>*/,
|
||||
0 /*mulMatImpl<float, float, int>*/,
|
||||
mulMatImpl<float, float, float>,
|
||||
mulMatImpl<float, double, double>
|
||||
},
|
||||
{
|
||||
0 /*mulMatImpl<double, double, uchar>*/,
|
||||
0 /*mulMatImpl<double, double, schar>*/,
|
||||
0 /*mulMatImpl<double, double, ushort>*/,
|
||||
0 /*mulMatImpl<double, double, short>*/,
|
||||
0 /*mulMatImpl<double, double, int>*/,
|
||||
0 /*mulMatImpl<double, double, float>*/,
|
||||
mulMatImpl<double, double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src1.depth();
|
||||
const int ddepth = dst.depth();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F );
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_, src2_, dst_, scale, stream);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
struct MulOpSpecial : binary_function<T, float, T>
|
||||
{
|
||||
__device__ __forceinline__ T operator ()(const T& a, float b) const
|
||||
{
|
||||
typedef typename VecTraits<T>::elem_type elem_type;
|
||||
|
||||
T res;
|
||||
|
||||
res.x = saturate_cast<elem_type>(a.x * b);
|
||||
res.y = saturate_cast<elem_type>(a.y * b);
|
||||
res.z = saturate_cast<elem_type>(a.z * b);
|
||||
res.w = saturate_cast<elem_type>(a.w * b);
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void mulMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary(globPtr<uchar4>(src1), globPtr<float>(src2), globPtr<uchar4>(dst), MulOpSpecial<uchar4>(), stream);
|
||||
}
|
||||
|
||||
void mulMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
gridTransformBinary(globPtr<short4>(src1), globPtr<float>(src2), globPtr<short4>(dst), MulOpSpecial<short4>(), stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,184 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/cuda/cuda_compat.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void mulScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
using cv::cuda::device::compat::double4Compat;
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct MulScalarOp : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(saturate_cast<ScalarType>(a) * val);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarDepth, typename DstType>
|
||||
void mulScalarImpl(const GpuMat& src, cv::Scalar value, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<ScalarDepth, VecTraits<SrcType>::cn>::type ScalarType;
|
||||
|
||||
cv::Scalar_<ScalarDepth> value_ = value;
|
||||
|
||||
MulScalarOp<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void mulScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar val, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[7][7][4] =
|
||||
{
|
||||
{
|
||||
{mulScalarImpl<uchar, float, uchar>, mulScalarImpl<uchar2, float, uchar2>, mulScalarImpl<uchar3, float, uchar3>, mulScalarImpl<uchar4, float, uchar4>},
|
||||
{mulScalarImpl<uchar, float, schar>, mulScalarImpl<uchar2, float, char2>, mulScalarImpl<uchar3, float, char3>, mulScalarImpl<uchar4, float, char4>},
|
||||
{mulScalarImpl<uchar, float, ushort>, mulScalarImpl<uchar2, float, ushort2>, mulScalarImpl<uchar3, float, ushort3>, mulScalarImpl<uchar4, float, ushort4>},
|
||||
{mulScalarImpl<uchar, float, short>, mulScalarImpl<uchar2, float, short2>, mulScalarImpl<uchar3, float, short3>, mulScalarImpl<uchar4, float, short4>},
|
||||
{mulScalarImpl<uchar, float, int>, mulScalarImpl<uchar2, float, int2>, mulScalarImpl<uchar3, float, int3>, mulScalarImpl<uchar4, float, int4>},
|
||||
{mulScalarImpl<uchar, float, float>, mulScalarImpl<uchar2, float, float2>, mulScalarImpl<uchar3, float, float3>, mulScalarImpl<uchar4, float, float4>},
|
||||
{mulScalarImpl<uchar, double, double>, mulScalarImpl<uchar2, double, double2>, mulScalarImpl<uchar3, double, double3>, mulScalarImpl<uchar4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{mulScalarImpl<schar, float, uchar>, mulScalarImpl<char2, float, uchar2>, mulScalarImpl<char3, float, uchar3>, mulScalarImpl<char4, float, uchar4>},
|
||||
{mulScalarImpl<schar, float, schar>, mulScalarImpl<char2, float, char2>, mulScalarImpl<char3, float, char3>, mulScalarImpl<char4, float, char4>},
|
||||
{mulScalarImpl<schar, float, ushort>, mulScalarImpl<char2, float, ushort2>, mulScalarImpl<char3, float, ushort3>, mulScalarImpl<char4, float, ushort4>},
|
||||
{mulScalarImpl<schar, float, short>, mulScalarImpl<char2, float, short2>, mulScalarImpl<char3, float, short3>, mulScalarImpl<char4, float, short4>},
|
||||
{mulScalarImpl<schar, float, int>, mulScalarImpl<char2, float, int2>, mulScalarImpl<char3, float, int3>, mulScalarImpl<char4, float, int4>},
|
||||
{mulScalarImpl<schar, float, float>, mulScalarImpl<char2, float, float2>, mulScalarImpl<char3, float, float3>, mulScalarImpl<char4, float, float4>},
|
||||
{mulScalarImpl<schar, double, double>, mulScalarImpl<char2, double, double2>, mulScalarImpl<char3, double, double3>, mulScalarImpl<char4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*mulScalarImpl<ushort, float, uchar>*/, 0 /*mulScalarImpl<ushort2, float, uchar2>*/, 0 /*mulScalarImpl<ushort3, float, uchar3>*/, 0 /*mulScalarImpl<ushort4, float, uchar4>*/},
|
||||
{0 /*mulScalarImpl<ushort, float, schar>*/, 0 /*mulScalarImpl<ushort2, float, char2>*/, 0 /*mulScalarImpl<ushort3, float, char3>*/, 0 /*mulScalarImpl<ushort4, float, char4>*/},
|
||||
{mulScalarImpl<ushort, float, ushort>, mulScalarImpl<ushort2, float, ushort2>, mulScalarImpl<ushort3, float, ushort3>, mulScalarImpl<ushort4, float, ushort4>},
|
||||
{mulScalarImpl<ushort, float, short>, mulScalarImpl<ushort2, float, short2>, mulScalarImpl<ushort3, float, short3>, mulScalarImpl<ushort4, float, short4>},
|
||||
{mulScalarImpl<ushort, float, int>, mulScalarImpl<ushort2, float, int2>, mulScalarImpl<ushort3, float, int3>, mulScalarImpl<ushort4, float, int4>},
|
||||
{mulScalarImpl<ushort, float, float>, mulScalarImpl<ushort2, float, float2>, mulScalarImpl<ushort3, float, float3>, mulScalarImpl<ushort4, float, float4>},
|
||||
{mulScalarImpl<ushort, double, double>, mulScalarImpl<ushort2, double, double2>, mulScalarImpl<ushort3, double, double3>, mulScalarImpl<ushort4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*mulScalarImpl<short, float, uchar>*/, 0 /*mulScalarImpl<short2, float, uchar2>*/, 0 /*mulScalarImpl<short3, float, uchar3>*/, 0 /*mulScalarImpl<short4, float, uchar4>*/},
|
||||
{0 /*mulScalarImpl<short, float, schar>*/, 0 /*mulScalarImpl<short2, float, char2>*/, 0 /*mulScalarImpl<short3, float, char3>*/, 0 /*mulScalarImpl<short4, float, char4>*/},
|
||||
{mulScalarImpl<short, float, ushort>, mulScalarImpl<short2, float, ushort2>, mulScalarImpl<short3, float, ushort3>, mulScalarImpl<short4, float, ushort4>},
|
||||
{mulScalarImpl<short, float, short>, mulScalarImpl<short2, float, short2>, mulScalarImpl<short3, float, short3>, mulScalarImpl<short4, float, short4>},
|
||||
{mulScalarImpl<short, float, int>, mulScalarImpl<short2, float, int2>, mulScalarImpl<short3, float, int3>, mulScalarImpl<short4, float, int4>},
|
||||
{mulScalarImpl<short, float, float>, mulScalarImpl<short2, float, float2>, mulScalarImpl<short3, float, float3>, mulScalarImpl<short4, float, float4>},
|
||||
{mulScalarImpl<short, double, double>, mulScalarImpl<short2, double, double2>, mulScalarImpl<short3, double, double3>, mulScalarImpl<short4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*mulScalarImpl<int, float, uchar>*/, 0 /*mulScalarImpl<int2, float, uchar2>*/, 0 /*mulScalarImpl<int3, float, uchar3>*/, 0 /*mulScalarImpl<int4, float, uchar4>*/},
|
||||
{0 /*mulScalarImpl<int, float, schar>*/, 0 /*mulScalarImpl<int2, float, char2>*/, 0 /*mulScalarImpl<int3, float, char3>*/, 0 /*mulScalarImpl<int4, float, char4>*/},
|
||||
{0 /*mulScalarImpl<int, float, ushort>*/, 0 /*mulScalarImpl<int2, float, ushort2>*/, 0 /*mulScalarImpl<int3, float, ushort3>*/, 0 /*mulScalarImpl<int4, float, ushort4>*/},
|
||||
{0 /*mulScalarImpl<int, float, short>*/, 0 /*mulScalarImpl<int2, float, short2>*/, 0 /*mulScalarImpl<int3, float, short3>*/, 0 /*mulScalarImpl<int4, float, short4>*/},
|
||||
{mulScalarImpl<int, float, int>, mulScalarImpl<int2, float, int2>, mulScalarImpl<int3, float, int3>, mulScalarImpl<int4, float, int4>},
|
||||
{mulScalarImpl<int, float, float>, mulScalarImpl<int2, float, float2>, mulScalarImpl<int3, float, float3>, mulScalarImpl<int4, float, float4>},
|
||||
{mulScalarImpl<int, double, double>, mulScalarImpl<int2, double, double2>, mulScalarImpl<int3, double, double3>, mulScalarImpl<int4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*mulScalarImpl<float, float, uchar>*/, 0 /*mulScalarImpl<float2, float, uchar2>*/, 0 /*mulScalarImpl<float3, float, uchar3>*/, 0 /*mulScalarImpl<float4, float, uchar4>*/},
|
||||
{0 /*mulScalarImpl<float, float, schar>*/, 0 /*mulScalarImpl<float2, float, char2>*/, 0 /*mulScalarImpl<float3, float, char3>*/, 0 /*mulScalarImpl<float4, float, char4>*/},
|
||||
{0 /*mulScalarImpl<float, float, ushort>*/, 0 /*mulScalarImpl<float2, float, ushort2>*/, 0 /*mulScalarImpl<float3, float, ushort3>*/, 0 /*mulScalarImpl<float4, float, ushort4>*/},
|
||||
{0 /*mulScalarImpl<float, float, short>*/, 0 /*mulScalarImpl<float2, float, short2>*/, 0 /*mulScalarImpl<float3, float, short3>*/, 0 /*mulScalarImpl<float4, float, short4>*/},
|
||||
{0 /*mulScalarImpl<float, float, int>*/, 0 /*mulScalarImpl<float2, float, int2>*/, 0 /*mulScalarImpl<float3, float, int3>*/, 0 /*mulScalarImpl<float4, float, int4>*/},
|
||||
{mulScalarImpl<float, float, float>, mulScalarImpl<float2, float, float2>, mulScalarImpl<float3, float, float3>, mulScalarImpl<float4, float, float4>},
|
||||
{mulScalarImpl<float, double, double>, mulScalarImpl<float2, double, double2>, mulScalarImpl<float3, double, double3>, mulScalarImpl<float4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*mulScalarImpl<double, double, uchar>*/, 0 /*mulScalarImpl<double2, double, uchar2>*/, 0 /*mulScalarImpl<double3, double, uchar3>*/, 0 /*mulScalarImpl<double4, double, uchar4>*/},
|
||||
{0 /*mulScalarImpl<double, double, schar>*/, 0 /*mulScalarImpl<double2, double, char2>*/, 0 /*mulScalarImpl<double3, double, char3>*/, 0 /*mulScalarImpl<double4, double, char4>*/},
|
||||
{0 /*mulScalarImpl<double, double, ushort>*/, 0 /*mulScalarImpl<double2, double, ushort2>*/, 0 /*mulScalarImpl<double3, double, ushort3>*/, 0 /*mulScalarImpl<double4, double, ushort4>*/},
|
||||
{0 /*mulScalarImpl<double, double, short>*/, 0 /*mulScalarImpl<double2, double, short2>*/, 0 /*mulScalarImpl<double3, double, short3>*/, 0 /*mulScalarImpl<double4, double, short4>*/},
|
||||
{0 /*mulScalarImpl<double, double, int>*/, 0 /*mulScalarImpl<double2, double, int2>*/, 0 /*mulScalarImpl<double3, double, int3>*/, 0 /*mulScalarImpl<double4, double, int4>*/},
|
||||
{0 /*mulScalarImpl<double, double, float>*/, 0 /*mulScalarImpl<double2, double, float2>*/, 0 /*mulScalarImpl<double3, double, float3>*/, 0 /*mulScalarImpl<double4, double, float4>*/},
|
||||
{mulScalarImpl<double, double, double>, mulScalarImpl<double2, double, double2>, mulScalarImpl<double3, double, double3>, mulScalarImpl<double4Compat, double, double4Compat>}
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src.depth();
|
||||
const int ddepth = dst.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F && cn <= 4 );
|
||||
|
||||
val[0] *= scale;
|
||||
val[1] *= scale;
|
||||
val[2] *= scale;
|
||||
val[3] *= scale;
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth][cn - 1];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, val, dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,170 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// mulSpectrums
|
||||
|
||||
namespace
|
||||
{
|
||||
__device__ __forceinline__ float real(const float2& val)
|
||||
{
|
||||
return val.x;
|
||||
}
|
||||
|
||||
__device__ __forceinline__ float imag(const float2& val)
|
||||
{
|
||||
return val.y;
|
||||
}
|
||||
|
||||
__device__ __forceinline__ float2 cmul(const float2& a, const float2& b)
|
||||
{
|
||||
return make_float2((real(a) * real(b)) - (imag(a) * imag(b)),
|
||||
(real(a) * imag(b)) + (imag(a) * real(b)));
|
||||
}
|
||||
|
||||
__device__ __forceinline__ float2 conj(const float2& a)
|
||||
{
|
||||
return make_float2(real(a), -imag(a));
|
||||
}
|
||||
|
||||
struct comlex_mul : binary_function<float2, float2, float2>
|
||||
{
|
||||
__device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
|
||||
{
|
||||
return cmul(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
struct comlex_mul_conj : binary_function<float2, float2, float2>
|
||||
{
|
||||
__device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
|
||||
{
|
||||
return cmul(a, conj(b));
|
||||
}
|
||||
};
|
||||
|
||||
struct comlex_mul_scale : binary_function<float2, float2, float2>
|
||||
{
|
||||
float scale;
|
||||
|
||||
__device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
|
||||
{
|
||||
return scale * cmul(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
struct comlex_mul_conj_scale : binary_function<float2, float2, float2>
|
||||
{
|
||||
float scale;
|
||||
|
||||
__device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
|
||||
{
|
||||
return scale * cmul(a, conj(b));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void cv::cuda::mulSpectrums(InputArray _src1, InputArray _src2, OutputArray _dst, int flags, bool conjB, Stream& stream)
|
||||
{
|
||||
CV_UNUSED(flags);
|
||||
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.type() == src2.type() && src1.type() == CV_32FC2 );
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), CV_32FC2, stream);
|
||||
|
||||
if (conjB)
|
||||
gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), comlex_mul_conj(), stream);
|
||||
else
|
||||
gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), comlex_mul(), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::mulAndScaleSpectrums(InputArray _src1, InputArray _src2, OutputArray _dst, int flags, float scale, bool conjB, Stream& stream)
|
||||
{
|
||||
CV_UNUSED(flags);
|
||||
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.type() == src2.type() && src1.type() == CV_32FC2);
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), CV_32FC2, stream);
|
||||
|
||||
if (conjB)
|
||||
{
|
||||
comlex_mul_conj_scale op;
|
||||
op.scale = scale;
|
||||
gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), op, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
comlex_mul_scale op;
|
||||
op.scale = scale;
|
||||
gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), op, stream);
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,190 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
void normDiffInf(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
|
||||
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
|
||||
GpuMat_<int>& dst = (GpuMat_<int>&) _dst;
|
||||
|
||||
gridFindMaxVal(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
|
||||
}
|
||||
|
||||
void normDiffL1(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
|
||||
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
|
||||
GpuMat_<int>& dst = (GpuMat_<int>&) _dst;
|
||||
|
||||
gridCalcSum(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
|
||||
}
|
||||
|
||||
void normDiffL2(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
|
||||
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
|
||||
GpuMat_<double>& dst = (GpuMat_<double>&) _dst;
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat_<double> buf(1, 1, pool.getAllocator());
|
||||
|
||||
gridCalcSum(sqr_(cvt_<double>(src1) - cvt_<double>(src2)), buf, stream);
|
||||
gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::calcNormDiff(InputArray _src1, InputArray _src2, OutputArray _dst, int normType, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream);
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
0, normDiffInf, normDiffL1, 0, normDiffL2
|
||||
};
|
||||
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.type() == CV_8UC1 );
|
||||
CV_Assert( src1.size() == src2.size() && src1.type() == src2.type() );
|
||||
CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, normType == NORM_L2 ? CV_64FC1 : CV_32SC1, stream);
|
||||
|
||||
const func_t func = funcs[normType];
|
||||
func(src1, src2, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
double cv::cuda::norm(InputArray _src1, InputArray _src2, int normType)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
calcNormDiff(_src1, _src2, dst, normType, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double val;
|
||||
dst.createMatHeader().convertTo(Mat(1, 1, CV_64FC1, &val), CV_64F);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
namespace cv { namespace cuda { namespace device {
|
||||
|
||||
void normL2(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _mask, Stream& stream);
|
||||
|
||||
}}}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename R>
|
||||
void normL2Impl(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<R>& dst = (GpuMat_<R>&) _dst;
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat_<double> buf(1, 1, pool.getAllocator());
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
gridCalcSum(sqr_(cvt_<double>(src)), buf, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridCalcSum(sqr_(cvt_<double>(src)), buf, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
|
||||
gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::device::normL2(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
normL2Impl<uchar, double>,
|
||||
normL2Impl<schar, double>,
|
||||
normL2Impl<ushort, double>,
|
||||
normL2Impl<short, double>,
|
||||
normL2Impl<int, double>,
|
||||
normL2Impl<float, double>,
|
||||
normL2Impl<double, double>
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( src.channels() == 1 );
|
||||
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC1, stream);
|
||||
|
||||
const func_t func = funcs[src.depth()];
|
||||
CV_Assert(func);
|
||||
func(src, mask, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,296 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T, typename R, typename I>
|
||||
struct ConvertorMinMax : unary_function<T, R>
|
||||
{
|
||||
typedef typename LargerType<T, R>::type larger_type1;
|
||||
typedef typename LargerType<larger_type1, I>::type larger_type2;
|
||||
typedef typename LargerType<larger_type2, float>::type scalar_type;
|
||||
|
||||
scalar_type dmin, dmax;
|
||||
const I* minMaxVals;
|
||||
|
||||
__device__ R operator ()(typename TypeTraits<T>::parameter_type src) const
|
||||
{
|
||||
const scalar_type smin = minMaxVals[0];
|
||||
const scalar_type smax = minMaxVals[1];
|
||||
|
||||
const scalar_type scale = (dmax - dmin) * (smax - smin > numeric_limits<scalar_type>::epsilon() ? 1.0 / (smax - smin) : 0.0);
|
||||
const scalar_type shift = dmin - smin * scale;
|
||||
|
||||
return cudev::saturate_cast<R>(scale * src + shift);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename R, typename I>
|
||||
void normalizeMinMax(const GpuMat& _src, GpuMat& _dst, double a, double b, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&)_src;
|
||||
GpuMat_<R>& dst = (GpuMat_<R>&)_dst;
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat_<I> minMaxVals(1, 2, pool.getAllocator());
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
gridFindMinMaxVal(src, minMaxVals, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridFindMinMaxVal(src, minMaxVals, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
|
||||
ConvertorMinMax<T, R, I> cvt;
|
||||
cvt.dmin = std::min(a, b);
|
||||
cvt.dmax = std::max(a, b);
|
||||
cvt.minMaxVals = minMaxVals[0];
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
gridTransformUnary(src, dst, cvt, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
gridTransformUnary(src, dst, cvt, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename R, typename I, bool normL2>
|
||||
struct ConvertorNorm : unary_function<T, R>
|
||||
{
|
||||
typedef typename LargerType<T, R>::type larger_type1;
|
||||
typedef typename LargerType<larger_type1, I>::type larger_type2;
|
||||
typedef typename LargerType<larger_type2, float>::type scalar_type;
|
||||
|
||||
scalar_type a;
|
||||
const I* normVal;
|
||||
|
||||
__device__ R operator ()(typename TypeTraits<T>::parameter_type src) const
|
||||
{
|
||||
sqrt_func<scalar_type> sqrt;
|
||||
|
||||
scalar_type scale = normL2 ? sqrt(*normVal) : *normVal;
|
||||
scale = scale > numeric_limits<scalar_type>::epsilon() ? a / scale : 0.0;
|
||||
|
||||
return cudev::saturate_cast<R>(scale * src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename R, typename I>
|
||||
void normalizeNorm(const GpuMat& _src, GpuMat& _dst, double a, int normType, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&)_src;
|
||||
GpuMat_<R>& dst = (GpuMat_<R>&)_dst;
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat_<I> normVal(1, 1, pool.getAllocator());
|
||||
|
||||
if (normType == NORM_L1)
|
||||
{
|
||||
if (mask.empty())
|
||||
{
|
||||
gridCalcSum(abs_(cvt_<I>(src)), normVal, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridCalcSum(abs_(cvt_<I>(src)), normVal, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
else if (normType == NORM_L2)
|
||||
{
|
||||
if (mask.empty())
|
||||
{
|
||||
gridCalcSum(sqr_(cvt_<I>(src)), normVal, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridCalcSum(sqr_(cvt_<I>(src)), normVal, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
else // NORM_INF
|
||||
{
|
||||
if (mask.empty())
|
||||
{
|
||||
gridFindMaxVal(abs_(cvt_<I>(src)), normVal, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridFindMaxVal(abs_(cvt_<I>(src)), normVal, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
|
||||
if (normType == NORM_L2)
|
||||
{
|
||||
ConvertorNorm<T, R, I, true> cvt;
|
||||
cvt.a = a;
|
||||
cvt.normVal = normVal[0];
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
gridTransformUnary(src, dst, cvt, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
gridTransformUnary(src, dst, cvt, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ConvertorNorm<T, R, I, false> cvt;
|
||||
cvt.a = a;
|
||||
cvt.normVal = normVal[0];
|
||||
|
||||
if (mask.empty())
|
||||
{
|
||||
gridTransformUnary(src, dst, cvt, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
dst.setTo(Scalar::all(0), stream);
|
||||
gridTransformUnary(src, dst, cvt, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void cv::cuda::normalize(InputArray _src, OutputArray _dst, double a, double b, int normType, int dtype, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_minmax_t)(const GpuMat& _src, GpuMat& _dst, double a, double b, const GpuMat& mask, Stream& stream);
|
||||
typedef void (*func_norm_t)(const GpuMat& _src, GpuMat& _dst, double a, int normType, const GpuMat& mask, Stream& stream);
|
||||
|
||||
static const func_minmax_t funcs_minmax[CV_DEPTH_MAX] =
|
||||
{
|
||||
normalizeMinMax<uchar, float, float>,
|
||||
normalizeMinMax<schar, float, float>,
|
||||
normalizeMinMax<ushort, float, float>,
|
||||
normalizeMinMax<short, float, float>,
|
||||
normalizeMinMax<int, float, float>,
|
||||
normalizeMinMax<float, float, float>,
|
||||
normalizeMinMax<double, double, double>
|
||||
};
|
||||
|
||||
static const func_norm_t funcs_norm[CV_DEPTH_MAX] =
|
||||
{
|
||||
normalizeNorm<uchar, float, float>,
|
||||
normalizeNorm<schar, float, float>,
|
||||
normalizeNorm<ushort, float, float>,
|
||||
normalizeNorm<short, float, float>,
|
||||
normalizeNorm<int, float, float>,
|
||||
normalizeNorm<float, float, float>,
|
||||
normalizeNorm<double, double, double>
|
||||
};
|
||||
|
||||
CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 || normType == NORM_MINMAX );
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( src.channels() == 1 );
|
||||
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
|
||||
|
||||
if (dtype < 0)
|
||||
{
|
||||
dtype = _dst.fixedType() ? _dst.type() : src.type();
|
||||
}
|
||||
dtype = CV_MAT_DEPTH(dtype);
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int tmp_depth = src_depth <= CV_32F ? CV_32F : src_depth;
|
||||
|
||||
GpuMat dst;
|
||||
if (dtype == tmp_depth)
|
||||
{
|
||||
_dst.create(src.size(), tmp_depth);
|
||||
dst = getOutputMat(_dst, src.size(), tmp_depth, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferPool pool(stream);
|
||||
dst = pool.getBuffer(src.size(), tmp_depth);
|
||||
}
|
||||
|
||||
if (normType == NORM_MINMAX)
|
||||
{
|
||||
const func_minmax_t func = funcs_minmax[src_depth];
|
||||
CV_Assert(func);
|
||||
func(src, dst, a, b, mask, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
const func_norm_t func = funcs_norm[src_depth];
|
||||
CV_Assert(func);
|
||||
func(src, dst, a, normType, mask, stream);
|
||||
}
|
||||
|
||||
if (dtype == tmp_depth)
|
||||
{
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
dst.convertTo(_dst, dtype, stream);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,396 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
//do not use implicit cv::cuda to avoid clash of tuples from ::cuda::std
|
||||
/*using namespace cv;
|
||||
using namespace cv::cuda;*/
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void cv::cuda::magnitude(InputArray _x, InputArray _y, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat x = getInputMat(_x, stream);
|
||||
GpuMat y = getInputMat(_y, stream);
|
||||
|
||||
CV_Assert( x.depth() == CV_32F );
|
||||
CV_Assert( y.type() == x.type() && y.size() == x.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, x.size(), CV_32FC1, stream);
|
||||
|
||||
gridTransformBinary(globPtr<float>(x), globPtr<float>(y), globPtr<float>(dst), magnitude_func<float>(), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::magnitudeSqr(InputArray _x, InputArray _y, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat x = getInputMat(_x, stream);
|
||||
GpuMat y = getInputMat(_y, stream);
|
||||
|
||||
CV_Assert( x.depth() == CV_32F );
|
||||
CV_Assert( y.type() == x.type() && y.size() == x.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, x.size(), CV_32FC1, stream);
|
||||
|
||||
gridTransformBinary(globPtr<float>(x), globPtr<float>(y), globPtr<float>(dst), magnitude_sqr_func<float>(), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::phase(InputArray _x, InputArray _y, OutputArray _dst, bool angleInDegrees, Stream& stream)
|
||||
{
|
||||
GpuMat x = getInputMat(_x, stream);
|
||||
GpuMat y = getInputMat(_y, stream);
|
||||
|
||||
CV_Assert( x.depth() == CV_32F );
|
||||
CV_Assert( y.type() == x.type() && y.size() == x.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, x.size(), CV_32FC1, stream);
|
||||
|
||||
if (angleInDegrees)
|
||||
gridTransformBinary(globPtr<float>(x), globPtr<float>(y), globPtr<float>(dst), direction_func<float, true>(), stream);
|
||||
else
|
||||
gridTransformBinary(globPtr<float>(x), globPtr<float>(y), globPtr<float>(dst), direction_func<float, false>(), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::phase(InputArray _xy, OutputArray _dst, bool angleInDegrees, Stream& stream)
|
||||
{
|
||||
GpuMat xy = getInputMat(_xy, stream);
|
||||
|
||||
CV_Assert( xy.type() == CV_32FC2 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, xy.size(), CV_32FC1, stream);
|
||||
|
||||
if (angleInDegrees)
|
||||
gridTransformUnary(globPtr<float2>(xy), globPtr<float>(dst), direction_interleaved_func<float2, true>(), stream);
|
||||
else
|
||||
gridTransformUnary(globPtr<float2>(xy), globPtr<float>(dst), direction_interleaved_func<float2, false>(), stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::cartToPolar(InputArray _x, InputArray _y, OutputArray _mag, OutputArray _angle, bool angleInDegrees, Stream& stream)
|
||||
{
|
||||
GpuMat x = getInputMat(_x, stream);
|
||||
GpuMat y = getInputMat(_y, stream);
|
||||
|
||||
CV_Assert( x.depth() == CV_32F );
|
||||
CV_Assert( y.type() == x.type() && y.size() == x.size() );
|
||||
|
||||
GpuMat mag = getOutputMat(_mag, x.size(), CV_32FC1, stream);
|
||||
GpuMat angle = getOutputMat(_angle, x.size(), CV_32FC1, stream);
|
||||
|
||||
GpuMat_<float> xc(x);
|
||||
GpuMat_<float> yc(y);
|
||||
GpuMat_<float> magc(mag);
|
||||
GpuMat_<float> anglec(angle);
|
||||
|
||||
if (angleInDegrees)
|
||||
gridTransformBinary(xc, yc, magc, anglec, magnitude_func<float>(), direction_func<float, true>(), stream);
|
||||
else
|
||||
gridTransformBinary(xc, yc, magc, anglec, magnitude_func<float>(), direction_func<float, false>(), stream);
|
||||
|
||||
syncOutput(mag, _mag, stream);
|
||||
syncOutput(angle, _angle, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::cartToPolar(InputArray _xy, OutputArray _mag, OutputArray _angle, bool angleInDegrees, Stream& stream)
|
||||
{
|
||||
GpuMat xy = getInputMat(_xy, stream);
|
||||
|
||||
CV_Assert( xy.type() == CV_32FC2 );
|
||||
|
||||
GpuMat mag = getOutputMat(_mag, xy.size(), CV_32FC1, stream);
|
||||
GpuMat angle = getOutputMat(_angle, xy.size(), CV_32FC1, stream);
|
||||
|
||||
GpuMat_<float> magc(mag);
|
||||
GpuMat_<float> anglec(angle);
|
||||
|
||||
gridTransformUnary(globPtr<float2>(xy), globPtr<float>(magc), magnitude_interleaved_func<float2>(), stream);
|
||||
|
||||
if (angleInDegrees)
|
||||
{
|
||||
gridTransformUnary(globPtr<float2>(xy), globPtr<float>(anglec), direction_interleaved_func<float2, true>(), stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridTransformUnary(globPtr<float2>(xy), globPtr<float>(anglec), direction_interleaved_func<float2, false>(), stream);
|
||||
}
|
||||
|
||||
syncOutput(mag, _mag, stream);
|
||||
syncOutput(angle, _angle, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::cartToPolar(InputArray _xy, OutputArray _magAngle, bool angleInDegrees, Stream& stream)
|
||||
{
|
||||
GpuMat xy = getInputMat(_xy, stream);
|
||||
|
||||
CV_Assert( xy.type() == CV_32FC2 );
|
||||
|
||||
GpuMat magAngle = getOutputMat(_magAngle, xy.size(), CV_32FC2, stream);
|
||||
|
||||
if (angleInDegrees)
|
||||
{
|
||||
gridTransformUnary(globPtr<float2>(xy),
|
||||
globPtr<float2>(magAngle),
|
||||
magnitude_direction_interleaved_func<float2, true>(),
|
||||
stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
gridTransformUnary(globPtr<float2>(xy),
|
||||
globPtr<float2>(magAngle),
|
||||
magnitude_direction_interleaved_func<float2, false>(),
|
||||
stream);
|
||||
}
|
||||
|
||||
syncOutput(magAngle, _magAngle, stream);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T> struct sincos_op
|
||||
{
|
||||
__device__ __forceinline__ void operator()(T a, T *sptr, T *cptr) const
|
||||
{
|
||||
::sincos(a, sptr, cptr);
|
||||
}
|
||||
};
|
||||
template <> struct sincos_op<float>
|
||||
{
|
||||
__device__ __forceinline__ void operator()(float a, float *sptr, float *cptr) const
|
||||
{
|
||||
::sincosf(a, sptr, cptr);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, bool useMag>
|
||||
__global__ void polarToCartImpl_(const PtrStep<T> mag, const PtrStepSz<T> angle, PtrStep<T> xmat, PtrStep<T> ymat, const T scale)
|
||||
{
|
||||
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
||||
|
||||
if (x >= angle.cols || y >= angle.rows)
|
||||
return;
|
||||
|
||||
const T mag_val = useMag ? mag(y, x) : static_cast<T>(1.0);
|
||||
const T angle_val = angle(y, x);
|
||||
|
||||
T sin_a, cos_a;
|
||||
sincos_op<T> op;
|
||||
op(scale * angle_val, &sin_a, &cos_a);
|
||||
|
||||
xmat(y, x) = mag_val * cos_a;
|
||||
ymat(y, x) = mag_val * sin_a;
|
||||
}
|
||||
|
||||
template <typename T, bool useMag>
|
||||
__global__ void polarToCartDstInterleavedImpl_(const PtrStep<T> mag, const PtrStepSz<T> angle, PtrStep<typename MakeVec<T, 2>::type > xymat, const T scale)
|
||||
{
|
||||
typedef typename MakeVec<T, 2>::type T2;
|
||||
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
||||
|
||||
if (x >= angle.cols || y >= angle.rows)
|
||||
return;
|
||||
|
||||
const T mag_val = useMag ? mag(y, x) : static_cast<T>(1.0);
|
||||
const T angle_val = angle(y, x);
|
||||
|
||||
T sin_a, cos_a;
|
||||
sincos_op<T> op;
|
||||
op(scale * angle_val, &sin_a, &cos_a);
|
||||
|
||||
const T2 xy = {mag_val * cos_a, mag_val * sin_a};
|
||||
xymat(y, x) = xy;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
__global__ void polarToCartInterleavedImpl_(const PtrStepSz<typename MakeVec<T, 2>::type > magAngle, PtrStep<typename MakeVec<T, 2>::type > xymat, const T scale)
|
||||
{
|
||||
typedef typename MakeVec<T, 2>::type T2;
|
||||
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
||||
|
||||
if (x >= magAngle.cols || y >= magAngle.rows)
|
||||
return;
|
||||
|
||||
const T2 magAngle_val = magAngle(y, x);
|
||||
const T mag_val = magAngle_val.x;
|
||||
const T angle_val = magAngle_val.y;
|
||||
|
||||
T sin_a, cos_a;
|
||||
sincos_op<T> op;
|
||||
op(scale * angle_val, &sin_a, &cos_a);
|
||||
|
||||
const T2 xy = {mag_val * cos_a, mag_val * sin_a};
|
||||
xymat(y, x) = xy;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void polarToCartImpl(const GpuMat& mag, const GpuMat& angle, GpuMat& x, GpuMat& y, bool angleInDegrees, cudaStream_t& stream)
|
||||
{
|
||||
const dim3 block(32, 8);
|
||||
const dim3 grid(divUp(angle.cols, block.x), divUp(angle.rows, block.y));
|
||||
|
||||
const T scale = angleInDegrees ? static_cast<T>(CV_PI / 180.0) : static_cast<T>(1.0);
|
||||
|
||||
if (mag.empty())
|
||||
polarToCartImpl_<T, false> <<<grid, block, 0, stream >>>(mag, angle, x, y, scale);
|
||||
else
|
||||
polarToCartImpl_<T, true> <<<grid, block, 0, stream >>>(mag, angle, x, y, scale);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void polarToCartDstInterleavedImpl(const GpuMat& mag, const GpuMat& angle, GpuMat& xy, bool angleInDegrees, cudaStream_t& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, 2>::type T2;
|
||||
|
||||
const dim3 block(32, 8);
|
||||
const dim3 grid(divUp(angle.cols, block.x), divUp(angle.rows, block.y));
|
||||
|
||||
const T scale = angleInDegrees ? static_cast<T>(CV_PI / 180.0) : static_cast<T>(1.0);
|
||||
|
||||
if (mag.empty())
|
||||
polarToCartDstInterleavedImpl_<T, false> <<<grid, block, 0, stream >>>(mag, angle, xy, scale);
|
||||
else
|
||||
polarToCartDstInterleavedImpl_<T, true> <<<grid, block, 0, stream >>>(mag, angle, xy, scale);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void polarToCartInterleavedImpl(const GpuMat& magAngle, GpuMat& xy, bool angleInDegrees, cudaStream_t& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, 2>::type T2;
|
||||
|
||||
const dim3 block(32, 8);
|
||||
const dim3 grid(divUp(magAngle.cols, block.x), divUp(magAngle.rows, block.y));
|
||||
|
||||
const T scale = angleInDegrees ? static_cast<T>(CV_PI / 180.0) : static_cast<T>(1.0);
|
||||
|
||||
polarToCartInterleavedImpl_<T> <<<grid, block, 0, stream >>>(magAngle, xy, scale);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::polarToCart(InputArray _mag, InputArray _angle, OutputArray _x, OutputArray _y, bool angleInDegrees, Stream& _stream)
|
||||
{
|
||||
typedef void(*func_t)(const GpuMat& mag, const GpuMat& angle, GpuMat& x, GpuMat& y, bool angleInDegrees, cudaStream_t& stream);
|
||||
static const func_t funcs[7] = { 0, 0, 0, 0, 0, polarToCartImpl<float>, polarToCartImpl<double> };
|
||||
|
||||
GpuMat mag = getInputMat(_mag, _stream);
|
||||
GpuMat angle = getInputMat(_angle, _stream);
|
||||
|
||||
CV_Assert(angle.depth() == CV_32F || angle.depth() == CV_64F);
|
||||
CV_Assert( mag.empty() || (mag.type() == angle.type() && mag.size() == angle.size()) );
|
||||
|
||||
GpuMat x = getOutputMat(_x, angle.size(), CV_MAKETYPE(angle.depth(), 1), _stream);
|
||||
GpuMat y = getOutputMat(_y, angle.size(), CV_MAKETYPE(angle.depth(), 1), _stream);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
funcs[angle.depth()](mag, angle, x, y, angleInDegrees, stream);
|
||||
CV_CUDEV_SAFE_CALL( cudaGetLastError() );
|
||||
|
||||
syncOutput(x, _x, _stream);
|
||||
syncOutput(y, _y, _stream);
|
||||
|
||||
if (stream == 0)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
void cv::cuda::polarToCart(InputArray _mag, InputArray _angle, OutputArray _xy, bool angleInDegrees, Stream& _stream)
|
||||
{
|
||||
typedef void(*func_t)(const GpuMat& mag, const GpuMat& angle, GpuMat& xy, bool angleInDegrees, cudaStream_t& stream);
|
||||
static const func_t funcs[7] = { 0, 0, 0, 0, 0, polarToCartDstInterleavedImpl<float>, polarToCartDstInterleavedImpl<double> };
|
||||
|
||||
GpuMat mag = getInputMat(_mag, _stream);
|
||||
GpuMat angle = getInputMat(_angle, _stream);
|
||||
|
||||
CV_Assert(angle.depth() == CV_32F || angle.depth() == CV_64F);
|
||||
CV_Assert( mag.empty() || (mag.type() == angle.type() && mag.size() == angle.size()) );
|
||||
|
||||
GpuMat xy = getOutputMat(_xy, angle.size(), CV_MAKETYPE(angle.depth(), 2), _stream);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
funcs[angle.depth()](mag, angle, xy, angleInDegrees, stream);
|
||||
CV_CUDEV_SAFE_CALL( cudaGetLastError() );
|
||||
|
||||
syncOutput(xy, _xy, _stream);
|
||||
|
||||
if (stream == 0)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
void cv::cuda::polarToCart(InputArray _magAngle, OutputArray _xy, bool angleInDegrees, Stream& _stream)
|
||||
{
|
||||
typedef void(*func_t)(const GpuMat& magAngle, GpuMat& xy, bool angleInDegrees, cudaStream_t& stream);
|
||||
static const func_t funcs[7] = { 0, 0, 0, 0, 0, polarToCartInterleavedImpl<float>, polarToCartInterleavedImpl<double> };
|
||||
|
||||
GpuMat magAngle = getInputMat(_magAngle, _stream);
|
||||
|
||||
CV_Assert(magAngle.type() == CV_32FC2 || magAngle.type() == CV_64FC2);
|
||||
|
||||
GpuMat xy = getOutputMat(_xy, magAngle.size(), magAngle.type(), _stream);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
funcs[magAngle.depth()](magAngle, xy, angleInDegrees, stream);
|
||||
CV_CUDEV_SAFE_CALL( cudaGetLastError() );
|
||||
|
||||
syncOutput(xy, _xy, _stream);
|
||||
|
||||
if (stream == 0)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,301 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename S, typename D>
|
||||
void reduceToRowImpl(const GpuMat& _src, GpuMat& _dst, int reduceOp, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<D>& dst = (GpuMat_<D>&) _dst;
|
||||
|
||||
switch (reduceOp)
|
||||
{
|
||||
case cv::REDUCE_SUM:
|
||||
gridReduceToRow< Sum<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_AVG:
|
||||
gridReduceToRow< Avg<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_MIN:
|
||||
gridReduceToRow< Min<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_MAX:
|
||||
gridReduceToRow< Max<S> >(src, dst, stream);
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T, typename S, typename D>
|
||||
void reduceToColumnImpl_(const GpuMat& _src, GpuMat& _dst, int reduceOp, Stream& stream)
|
||||
{
|
||||
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
|
||||
GpuMat_<D>& dst = (GpuMat_<D>&) _dst;
|
||||
|
||||
switch (reduceOp)
|
||||
{
|
||||
case cv::REDUCE_SUM:
|
||||
gridReduceToColumn< Sum<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_AVG:
|
||||
gridReduceToColumn< Avg<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_MIN:
|
||||
gridReduceToColumn< Min<S> >(src, dst, stream);
|
||||
break;
|
||||
|
||||
case cv::REDUCE_MAX:
|
||||
gridReduceToColumn< Max<S> >(src, dst, stream);
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T, typename S, typename D>
|
||||
void reduceToColumnImpl(const GpuMat& src, GpuMat& dst, int reduceOp, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat& dst, int reduceOp, Stream& stream);
|
||||
static const func_t funcs[4] =
|
||||
{
|
||||
reduceToColumnImpl_<T, S, D>,
|
||||
reduceToColumnImpl_<typename MakeVec<T, 2>::type, typename MakeVec<S, 2>::type, typename MakeVec<D, 2>::type>,
|
||||
reduceToColumnImpl_<typename MakeVec<T, 3>::type, typename MakeVec<S, 3>::type, typename MakeVec<D, 3>::type>,
|
||||
reduceToColumnImpl_<typename MakeVec<T, 4>::type, typename MakeVec<S, 4>::type, typename MakeVec<D, 4>::type>
|
||||
};
|
||||
|
||||
funcs[src.channels() - 1](src, dst, reduceOp, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::reduce(InputArray _src, OutputArray _dst, int dim, int reduceOp, int dtype, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.channels() <= 4 );
|
||||
CV_Assert( dim == 0 || dim == 1 );
|
||||
CV_Assert( reduceOp == REDUCE_SUM || reduceOp == REDUCE_AVG || reduceOp == REDUCE_MAX || reduceOp == REDUCE_MIN );
|
||||
|
||||
if (dtype < 0)
|
||||
dtype = src.depth();
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, dim == 0 ? 1 : src.rows, dim == 0 ? src.cols : 1, CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()), stream);
|
||||
|
||||
if (dim == 0)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, GpuMat& _dst, int reduceOp, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX][CV_DEPTH_MAX] =
|
||||
{
|
||||
{
|
||||
reduceToRowImpl<uchar, int, uchar>,
|
||||
0 /*reduceToRowImpl<uchar, int, schar>*/,
|
||||
0 /*reduceToRowImpl<uchar, int, ushort>*/,
|
||||
0 /*reduceToRowImpl<uchar, int, short>*/,
|
||||
reduceToRowImpl<uchar, int, int>,
|
||||
reduceToRowImpl<uchar, float, float>,
|
||||
reduceToRowImpl<uchar, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<schar, int, uchar>*/,
|
||||
0 /*reduceToRowImpl<schar, int, schar>*/,
|
||||
0 /*reduceToRowImpl<schar, int, ushort>*/,
|
||||
0 /*reduceToRowImpl<schar, int, short>*/,
|
||||
0 /*reduceToRowImpl<schar, int, int>*/,
|
||||
0 /*reduceToRowImpl<schar, float, float>*/,
|
||||
0 /*reduceToRowImpl<schar, double, double>*/
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<ushort, int, uchar>*/,
|
||||
0 /*reduceToRowImpl<ushort, int, schar>*/,
|
||||
reduceToRowImpl<ushort, int, ushort>,
|
||||
0 /*reduceToRowImpl<ushort, int, short>*/,
|
||||
reduceToRowImpl<ushort, int, int>,
|
||||
reduceToRowImpl<ushort, float, float>,
|
||||
reduceToRowImpl<ushort, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<short, int, uchar>*/,
|
||||
0 /*reduceToRowImpl<short, int, schar>*/,
|
||||
0 /*reduceToRowImpl<short, int, ushort>*/,
|
||||
reduceToRowImpl<short, int, short>,
|
||||
reduceToRowImpl<short, int, int>,
|
||||
reduceToRowImpl<short, float, float>,
|
||||
reduceToRowImpl<short, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<int, int, uchar>*/,
|
||||
0 /*reduceToRowImpl<int, int, schar>*/,
|
||||
0 /*reduceToRowImpl<int, int, ushort>*/,
|
||||
0 /*reduceToRowImpl<int, int, short>*/,
|
||||
reduceToRowImpl<int, int, int>,
|
||||
reduceToRowImpl<int, float, float>,
|
||||
reduceToRowImpl<int, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<float, float, uchar>*/,
|
||||
0 /*reduceToRowImpl<float, float, schar>*/,
|
||||
0 /*reduceToRowImpl<float, float, ushort>*/,
|
||||
0 /*reduceToRowImpl<float, float, short>*/,
|
||||
0 /*reduceToRowImpl<float, float, int>*/,
|
||||
reduceToRowImpl<float, float, float>,
|
||||
reduceToRowImpl<float, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToRowImpl<double, double, uchar>*/,
|
||||
0 /*reduceToRowImpl<double, double, schar>*/,
|
||||
0 /*reduceToRowImpl<double, double, ushort>*/,
|
||||
0 /*reduceToRowImpl<double, double, short>*/,
|
||||
0 /*reduceToRowImpl<double, double, int>*/,
|
||||
0 /*reduceToRowImpl<double, double, float>*/,
|
||||
reduceToRowImpl<double, double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const func_t func = funcs[src.depth()][dst.depth()];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of input and output array formats");
|
||||
|
||||
GpuMat dst_cont = dst.reshape(1);
|
||||
func(src.reshape(1), dst_cont, reduceOp, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, GpuMat& _dst, int reduceOp, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX][CV_DEPTH_MAX] =
|
||||
{
|
||||
{
|
||||
reduceToColumnImpl<uchar, int, uchar>,
|
||||
0 /*reduceToColumnImpl<uchar, int, schar>*/,
|
||||
0 /*reduceToColumnImpl<uchar, int, ushort>*/,
|
||||
0 /*reduceToColumnImpl<uchar, int, short>*/,
|
||||
reduceToColumnImpl<uchar, int, int>,
|
||||
reduceToColumnImpl<uchar, float, float>,
|
||||
reduceToColumnImpl<uchar, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<schar, int, uchar>*/,
|
||||
0 /*reduceToColumnImpl<schar, int, schar>*/,
|
||||
0 /*reduceToColumnImpl<schar, int, ushort>*/,
|
||||
0 /*reduceToColumnImpl<schar, int, short>*/,
|
||||
0 /*reduceToColumnImpl<schar, int, int>*/,
|
||||
0 /*reduceToColumnImpl<schar, float, float>*/,
|
||||
0 /*reduceToColumnImpl<schar, double, double>*/
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<ushort, int, uchar>*/,
|
||||
0 /*reduceToColumnImpl<ushort, int, schar>*/,
|
||||
reduceToColumnImpl<ushort, int, ushort>,
|
||||
0 /*reduceToColumnImpl<ushort, int, short>*/,
|
||||
reduceToColumnImpl<ushort, int, int>,
|
||||
reduceToColumnImpl<ushort, float, float>,
|
||||
reduceToColumnImpl<ushort, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<short, int, uchar>*/,
|
||||
0 /*reduceToColumnImpl<short, int, schar>*/,
|
||||
0 /*reduceToColumnImpl<short, int, ushort>*/,
|
||||
reduceToColumnImpl<short, int, short>,
|
||||
reduceToColumnImpl<short, int, int>,
|
||||
reduceToColumnImpl<short, float, float>,
|
||||
reduceToColumnImpl<short, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<int, int, uchar>*/,
|
||||
0 /*reduceToColumnImpl<int, int, schar>*/,
|
||||
0 /*reduceToColumnImpl<int, int, ushort>*/,
|
||||
0 /*reduceToColumnImpl<int, int, short>*/,
|
||||
reduceToColumnImpl<int, int, int>,
|
||||
reduceToColumnImpl<int, float, float>,
|
||||
reduceToColumnImpl<int, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<float, float, uchar>*/,
|
||||
0 /*reduceToColumnImpl<float, float, schar>*/,
|
||||
0 /*reduceToColumnImpl<float, float, ushort>*/,
|
||||
0 /*reduceToColumnImpl<float, float, short>*/,
|
||||
0 /*reduceToColumnImpl<float, float, int>*/,
|
||||
reduceToColumnImpl<float, float, float>,
|
||||
reduceToColumnImpl<float, double, double>
|
||||
},
|
||||
{
|
||||
0 /*reduceToColumnImpl<double, double, uchar>*/,
|
||||
0 /*reduceToColumnImpl<double, double, schar>*/,
|
||||
0 /*reduceToColumnImpl<double, double, ushort>*/,
|
||||
0 /*reduceToColumnImpl<double, double, short>*/,
|
||||
0 /*reduceToColumnImpl<double, double, int>*/,
|
||||
0 /*reduceToColumnImpl<double, double, float>*/,
|
||||
reduceToColumnImpl<double, double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const func_t func = funcs[src.depth()][dst.depth()];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of input and output array formats");
|
||||
|
||||
func(src, dst, reduceOp, stream);
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,253 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/// merge
|
||||
|
||||
namespace
|
||||
{
|
||||
template <int cn, typename T> struct MergeFunc;
|
||||
|
||||
template <typename T> struct MergeFunc<2, T>
|
||||
{
|
||||
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const std::array<GlobPtrSz<T>, 2> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1])};
|
||||
gridMerge(d_src,
|
||||
globPtr<typename MakeVec<T, 2>::type>(dst),
|
||||
stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> struct MergeFunc<3, T>
|
||||
{
|
||||
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const std::array<GlobPtrSz<T>, 3> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])};
|
||||
gridMerge(d_src,
|
||||
globPtr<typename MakeVec<T, 3>::type>(dst),
|
||||
stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> struct MergeFunc<4, T>
|
||||
{
|
||||
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const std::array<GlobPtrSz<T>, 4 > d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])};
|
||||
gridMerge(d_src,
|
||||
globPtr<typename MakeVec<T, 4>::type>(dst),
|
||||
stream);
|
||||
}
|
||||
};
|
||||
|
||||
void mergeImpl(const GpuMat* src, size_t n, cv::OutputArray _dst, Stream& stream)
|
||||
{
|
||||
CV_Assert( src != 0 );
|
||||
CV_Assert( n > 0 && n <= 4 );
|
||||
|
||||
const int depth = src[0].depth();
|
||||
const cv::Size size = src[0].size();
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
{
|
||||
CV_Assert( src[i].size() == size );
|
||||
CV_Assert( src[i].depth() == depth );
|
||||
CV_Assert( src[i].channels() == 1 );
|
||||
}
|
||||
|
||||
if (n == 1)
|
||||
{
|
||||
src[0].copyTo(_dst, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat* src, GpuMat& dst, Stream& stream);
|
||||
static const func_t funcs[3][5] =
|
||||
{
|
||||
{MergeFunc<2, uchar>::call, MergeFunc<2, ushort>::call, MergeFunc<2, int>::call, 0, MergeFunc<2, double>::call},
|
||||
{MergeFunc<3, uchar>::call, MergeFunc<3, ushort>::call, MergeFunc<3, int>::call, 0, MergeFunc<3, double>::call},
|
||||
{MergeFunc<4, uchar>::call, MergeFunc<4, ushort>::call, MergeFunc<4, int>::call, 0, MergeFunc<4, double>::call}
|
||||
};
|
||||
|
||||
const int channels = static_cast<int>(n);
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, size, CV_MAKE_TYPE(depth, channels), stream);
|
||||
|
||||
const func_t func = funcs[channels - 2][CV_ELEM_SIZE(depth) / 2];
|
||||
|
||||
if (func == 0)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported channel count or data type");
|
||||
|
||||
func(src, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::merge(const GpuMat* src, size_t n, OutputArray dst, Stream& stream)
|
||||
{
|
||||
mergeImpl(src, n, dst, stream);
|
||||
}
|
||||
|
||||
|
||||
void cv::cuda::merge(const std::vector<GpuMat>& src, OutputArray dst, Stream& stream)
|
||||
{
|
||||
mergeImpl(&src[0], src.size(), dst, stream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/// split
|
||||
|
||||
namespace
|
||||
{
|
||||
template <int cn, typename T> struct SplitFunc;
|
||||
|
||||
template <typename T> struct SplitFunc<2, T>
|
||||
{
|
||||
static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
|
||||
{
|
||||
GlobPtrSz<T> dstarr[2] =
|
||||
{
|
||||
globPtr<T>(dst[0]), globPtr<T>(dst[1])
|
||||
};
|
||||
|
||||
gridSplit(globPtr<typename MakeVec<T, 2>::type>(src), dstarr, stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> struct SplitFunc<3, T>
|
||||
{
|
||||
static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
|
||||
{
|
||||
GlobPtrSz<T> dstarr[3] =
|
||||
{
|
||||
globPtr<T>(dst[0]), globPtr<T>(dst[1]), globPtr<T>(dst[2])
|
||||
};
|
||||
|
||||
gridSplit(globPtr<typename MakeVec<T, 3>::type>(src), dstarr, stream);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> struct SplitFunc<4, T>
|
||||
{
|
||||
static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
|
||||
{
|
||||
GlobPtrSz<T> dstarr[4] =
|
||||
{
|
||||
globPtr<T>(dst[0]), globPtr<T>(dst[1]), globPtr<T>(dst[2]), globPtr<T>(dst[3])
|
||||
};
|
||||
|
||||
gridSplit(globPtr<typename MakeVec<T, 4>::type>(src), dstarr, stream);
|
||||
}
|
||||
};
|
||||
|
||||
void splitImpl(const GpuMat& src, GpuMat* dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat* dst, Stream& stream);
|
||||
static const func_t funcs[3][5] =
|
||||
{
|
||||
{SplitFunc<2, uchar>::call, SplitFunc<2, ushort>::call, SplitFunc<2, int>::call, 0, SplitFunc<2, double>::call},
|
||||
{SplitFunc<3, uchar>::call, SplitFunc<3, ushort>::call, SplitFunc<3, int>::call, 0, SplitFunc<3, double>::call},
|
||||
{SplitFunc<4, uchar>::call, SplitFunc<4, ushort>::call, SplitFunc<4, int>::call, 0, SplitFunc<4, double>::call}
|
||||
};
|
||||
|
||||
CV_Assert( dst != 0 );
|
||||
|
||||
const int depth = src.depth();
|
||||
const int channels = src.channels();
|
||||
|
||||
CV_Assert( channels <= 4 );
|
||||
|
||||
if (channels == 0)
|
||||
return;
|
||||
|
||||
if (channels == 1)
|
||||
{
|
||||
src.copyTo(dst[0], stream);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < channels; ++i)
|
||||
dst[i].create(src.size(), depth);
|
||||
|
||||
const func_t func = funcs[channels - 2][CV_ELEM_SIZE(depth) / 2];
|
||||
|
||||
if (func == 0)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported channel count or data type");
|
||||
|
||||
func(src, dst, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::split(InputArray _src, GpuMat* dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
splitImpl(src, dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::split(InputArray _src, std::vector<GpuMat>& dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
dst.resize(src.channels());
|
||||
if (src.channels() > 0)
|
||||
splitImpl(src, &dst[0], stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,225 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename D> struct SubOp1 : binary_function<T, T, D>
|
||||
{
|
||||
__device__ __forceinline__ D operator ()(T a, T b) const
|
||||
{
|
||||
return saturate_cast<D>(a - b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename D>
|
||||
void subMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
if (mask.data)
|
||||
gridTransformBinary(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), SubOp1<T, D>(), globPtr<uchar>(mask), stream);
|
||||
else
|
||||
gridTransformBinary(globPtr<T>(src1), globPtr<T>(src2), globPtr<D>(dst), SubOp1<T, D>(), stream);
|
||||
}
|
||||
|
||||
struct SubOp2 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vsub2(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void subMat_v2(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 1;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, SubOp2(), stream);
|
||||
}
|
||||
|
||||
struct SubOp4 : binary_function<uint, uint, uint>
|
||||
{
|
||||
__device__ __forceinline__ uint operator ()(uint a, uint b) const
|
||||
{
|
||||
return vsub4(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
void subMat_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
|
||||
{
|
||||
const int vcols = src1.cols >> 2;
|
||||
|
||||
GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
|
||||
GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
|
||||
|
||||
gridTransformBinary(src1_, src2_, dst_, SubOp4(), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][7] =
|
||||
{
|
||||
{
|
||||
subMat_v1<uchar, uchar>,
|
||||
subMat_v1<uchar, schar>,
|
||||
subMat_v1<uchar, ushort>,
|
||||
subMat_v1<uchar, short>,
|
||||
subMat_v1<uchar, int>,
|
||||
subMat_v1<uchar, float>,
|
||||
subMat_v1<uchar, double>
|
||||
},
|
||||
{
|
||||
subMat_v1<schar, uchar>,
|
||||
subMat_v1<schar, schar>,
|
||||
subMat_v1<schar, ushort>,
|
||||
subMat_v1<schar, short>,
|
||||
subMat_v1<schar, int>,
|
||||
subMat_v1<schar, float>,
|
||||
subMat_v1<schar, double>
|
||||
},
|
||||
{
|
||||
0 /*subMat_v1<ushort, uchar>*/,
|
||||
0 /*subMat_v1<ushort, schar>*/,
|
||||
subMat_v1<ushort, ushort>,
|
||||
subMat_v1<ushort, short>,
|
||||
subMat_v1<ushort, int>,
|
||||
subMat_v1<ushort, float>,
|
||||
subMat_v1<ushort, double>
|
||||
},
|
||||
{
|
||||
0 /*subMat_v1<short, uchar>*/,
|
||||
0 /*subMat_v1<short, schar>*/,
|
||||
subMat_v1<short, ushort>,
|
||||
subMat_v1<short, short>,
|
||||
subMat_v1<short, int>,
|
||||
subMat_v1<short, float>,
|
||||
subMat_v1<short, double>
|
||||
},
|
||||
{
|
||||
0 /*subMat_v1<int, uchar>*/,
|
||||
0 /*subMat_v1<int, schar>*/,
|
||||
0 /*subMat_v1<int, ushort>*/,
|
||||
0 /*subMat_v1<int, short>*/,
|
||||
subMat_v1<int, int>,
|
||||
subMat_v1<int, float>,
|
||||
subMat_v1<int, double>
|
||||
},
|
||||
{
|
||||
0 /*subMat_v1<float, uchar>*/,
|
||||
0 /*subMat_v1<float, schar>*/,
|
||||
0 /*subMat_v1<float, ushort>*/,
|
||||
0 /*subMat_v1<float, short>*/,
|
||||
0 /*subMat_v1<float, int>*/,
|
||||
subMat_v1<float, float>,
|
||||
subMat_v1<float, double>
|
||||
},
|
||||
{
|
||||
0 /*subMat_v1<double, uchar>*/,
|
||||
0 /*subMat_v1<double, schar>*/,
|
||||
0 /*subMat_v1<double, ushort>*/,
|
||||
0 /*subMat_v1<double, short>*/,
|
||||
0 /*subMat_v1<double, int>*/,
|
||||
0 /*subMat_v1<double, float>*/,
|
||||
subMat_v1<double, double>
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src1.depth();
|
||||
const int ddepth = dst.depth();
|
||||
|
||||
CV_Assert( sdepth <= CV_64F && ddepth <= CV_64F );
|
||||
|
||||
GpuMat src1_ = src1.reshape(1);
|
||||
GpuMat src2_ = src2.reshape(1);
|
||||
GpuMat dst_ = dst.reshape(1);
|
||||
|
||||
if (mask.empty() && (sdepth == CV_8U || sdepth == CV_16U) && ddepth == sdepth)
|
||||
{
|
||||
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
|
||||
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
|
||||
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
|
||||
|
||||
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
|
||||
|
||||
if (isAllAligned)
|
||||
{
|
||||
if (sdepth == CV_8U && (src1_.cols & 3) == 0)
|
||||
{
|
||||
subMat_v4(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
else if (sdepth == CV_16U && (src1_.cols & 1) == 0)
|
||||
{
|
||||
subMat_v2(src1_, src2_, dst_, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src1_, src2_, dst_, mask, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,206 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/cuda/cuda_compat.hpp"
|
||||
|
||||
using namespace cv::cudev;
|
||||
|
||||
void subScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int);
|
||||
|
||||
namespace
|
||||
{
|
||||
using cv::cuda::device::compat::double4Compat;
|
||||
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct SubScalarOp : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(saturate_cast<ScalarType>(a) - val);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarType, typename DstType> struct SubScalarOpInv : unary_function<SrcType, DstType>
|
||||
{
|
||||
ScalarType val;
|
||||
|
||||
__device__ __forceinline__ DstType operator ()(SrcType a) const
|
||||
{
|
||||
return saturate_cast<DstType>(val - saturate_cast<ScalarType>(a));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SrcType, typename ScalarDepth, typename DstType>
|
||||
void subScalarImpl(const GpuMat& src, cv::Scalar value, bool inv, GpuMat& dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<ScalarDepth, VecTraits<SrcType>::cn>::type ScalarType;
|
||||
|
||||
cv::Scalar_<ScalarDepth> value_ = value;
|
||||
|
||||
if (inv)
|
||||
{
|
||||
SubScalarOpInv<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
if (mask.data)
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, globPtr<uchar>(mask), stream);
|
||||
else
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
SubScalarOp<SrcType, ScalarType, DstType> op;
|
||||
op.val = VecTraits<ScalarType>::make(value_.val);
|
||||
|
||||
if (mask.data)
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, globPtr<uchar>(mask), stream);
|
||||
else
|
||||
gridTransformUnary_< TransformPolicy<ScalarDepth> >(globPtr<SrcType>(src), globPtr<DstType>(dst), op, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void subScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][7][4] =
|
||||
{
|
||||
{
|
||||
{subScalarImpl<uchar, float, uchar>, subScalarImpl<uchar2, float, uchar2>, subScalarImpl<uchar3, float, uchar3>, subScalarImpl<uchar4, float, uchar4>},
|
||||
{subScalarImpl<uchar, float, schar>, subScalarImpl<uchar2, float, char2>, subScalarImpl<uchar3, float, char3>, subScalarImpl<uchar4, float, char4>},
|
||||
{subScalarImpl<uchar, float, ushort>, subScalarImpl<uchar2, float, ushort2>, subScalarImpl<uchar3, float, ushort3>, subScalarImpl<uchar4, float, ushort4>},
|
||||
{subScalarImpl<uchar, float, short>, subScalarImpl<uchar2, float, short2>, subScalarImpl<uchar3, float, short3>, subScalarImpl<uchar4, float, short4>},
|
||||
{subScalarImpl<uchar, float, int>, subScalarImpl<uchar2, float, int2>, subScalarImpl<uchar3, float, int3>, subScalarImpl<uchar4, float, int4>},
|
||||
{subScalarImpl<uchar, float, float>, subScalarImpl<uchar2, float, float2>, subScalarImpl<uchar3, float, float3>, subScalarImpl<uchar4, float, float4>},
|
||||
{subScalarImpl<uchar, double, double>, subScalarImpl<uchar2, double, double2>, subScalarImpl<uchar3, double, double3>, subScalarImpl<uchar4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{subScalarImpl<schar, float, uchar>, subScalarImpl<char2, float, uchar2>, subScalarImpl<char3, float, uchar3>, subScalarImpl<char4, float, uchar4>},
|
||||
{subScalarImpl<schar, float, schar>, subScalarImpl<char2, float, char2>, subScalarImpl<char3, float, char3>, subScalarImpl<char4, float, char4>},
|
||||
{subScalarImpl<schar, float, ushort>, subScalarImpl<char2, float, ushort2>, subScalarImpl<char3, float, ushort3>, subScalarImpl<char4, float, ushort4>},
|
||||
{subScalarImpl<schar, float, short>, subScalarImpl<char2, float, short2>, subScalarImpl<char3, float, short3>, subScalarImpl<char4, float, short4>},
|
||||
{subScalarImpl<schar, float, int>, subScalarImpl<char2, float, int2>, subScalarImpl<char3, float, int3>, subScalarImpl<char4, float, int4>},
|
||||
{subScalarImpl<schar, float, float>, subScalarImpl<char2, float, float2>, subScalarImpl<char3, float, float3>, subScalarImpl<char4, float, float4>},
|
||||
{subScalarImpl<schar, double, double>, subScalarImpl<char2, double, double2>, subScalarImpl<char3, double, double3>, subScalarImpl<char4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*subScalarImpl<ushort, float, uchar>*/, 0 /*subScalarImpl<ushort2, float, uchar2>*/, 0 /*subScalarImpl<ushort3, float, uchar3>*/, 0 /*subScalarImpl<ushort4, float, uchar4>*/},
|
||||
{0 /*subScalarImpl<ushort, float, schar>*/, 0 /*subScalarImpl<ushort2, float, char2>*/, 0 /*subScalarImpl<ushort3, float, char3>*/, 0 /*subScalarImpl<ushort4, float, char4>*/},
|
||||
{subScalarImpl<ushort, float, ushort>, subScalarImpl<ushort2, float, ushort2>, subScalarImpl<ushort3, float, ushort3>, subScalarImpl<ushort4, float, ushort4>},
|
||||
{subScalarImpl<ushort, float, short>, subScalarImpl<ushort2, float, short2>, subScalarImpl<ushort3, float, short3>, subScalarImpl<ushort4, float, short4>},
|
||||
{subScalarImpl<ushort, float, int>, subScalarImpl<ushort2, float, int2>, subScalarImpl<ushort3, float, int3>, subScalarImpl<ushort4, float, int4>},
|
||||
{subScalarImpl<ushort, float, float>, subScalarImpl<ushort2, float, float2>, subScalarImpl<ushort3, float, float3>, subScalarImpl<ushort4, float, float4>},
|
||||
{subScalarImpl<ushort, double, double>, subScalarImpl<ushort2, double, double2>, subScalarImpl<ushort3, double, double3>, subScalarImpl<ushort4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*subScalarImpl<short, float, uchar>*/, 0 /*subScalarImpl<short2, float, uchar2>*/, 0 /*subScalarImpl<short3, float, uchar3>*/, 0 /*subScalarImpl<short4, float, uchar4>*/},
|
||||
{0 /*subScalarImpl<short, float, schar>*/, 0 /*subScalarImpl<short2, float, char2>*/, 0 /*subScalarImpl<short3, float, char3>*/, 0 /*subScalarImpl<short4, float, char4>*/},
|
||||
{subScalarImpl<short, float, ushort>, subScalarImpl<short2, float, ushort2>, subScalarImpl<short3, float, ushort3>, subScalarImpl<short4, float, ushort4>},
|
||||
{subScalarImpl<short, float, short>, subScalarImpl<short2, float, short2>, subScalarImpl<short3, float, short3>, subScalarImpl<short4, float, short4>},
|
||||
{subScalarImpl<short, float, int>, subScalarImpl<short2, float, int2>, subScalarImpl<short3, float, int3>, subScalarImpl<short4, float, int4>},
|
||||
{subScalarImpl<short, float, float>, subScalarImpl<short2, float, float2>, subScalarImpl<short3, float, float3>, subScalarImpl<short4, float, float4>},
|
||||
{subScalarImpl<short, double, double>, subScalarImpl<short2, double, double2>, subScalarImpl<short3, double, double3>, subScalarImpl<short4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*subScalarImpl<int, float, uchar>*/, 0 /*subScalarImpl<int2, float, uchar2>*/, 0 /*subScalarImpl<int3, float, uchar3>*/, 0 /*subScalarImpl<int4, float, uchar4>*/},
|
||||
{0 /*subScalarImpl<int, float, schar>*/, 0 /*subScalarImpl<int2, float, char2>*/, 0 /*subScalarImpl<int3, float, char3>*/, 0 /*subScalarImpl<int4, float, char4>*/},
|
||||
{0 /*subScalarImpl<int, float, ushort>*/, 0 /*subScalarImpl<int2, float, ushort2>*/, 0 /*subScalarImpl<int3, float, ushort3>*/, 0 /*subScalarImpl<int4, float, ushort4>*/},
|
||||
{0 /*subScalarImpl<int, float, short>*/, 0 /*subScalarImpl<int2, float, short2>*/, 0 /*subScalarImpl<int3, float, short3>*/, 0 /*subScalarImpl<int4, float, short4>*/},
|
||||
{subScalarImpl<int, float, int>, subScalarImpl<int2, float, int2>, subScalarImpl<int3, float, int3>, subScalarImpl<int4, float, int4>},
|
||||
{subScalarImpl<int, float, float>, subScalarImpl<int2, float, float2>, subScalarImpl<int3, float, float3>, subScalarImpl<int4, float, float4>},
|
||||
{subScalarImpl<int, double, double>, subScalarImpl<int2, double, double2>, subScalarImpl<int3, double, double3>, subScalarImpl<int4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*subScalarImpl<float, float, uchar>*/, 0 /*subScalarImpl<float2, float, uchar2>*/, 0 /*subScalarImpl<float3, float, uchar3>*/, 0 /*subScalarImpl<float4, float, uchar4>*/},
|
||||
{0 /*subScalarImpl<float, float, schar>*/, 0 /*subScalarImpl<float2, float, char2>*/, 0 /*subScalarImpl<float3, float, char3>*/, 0 /*subScalarImpl<float4, float, char4>*/},
|
||||
{0 /*subScalarImpl<float, float, ushort>*/, 0 /*subScalarImpl<float2, float, ushort2>*/, 0 /*subScalarImpl<float3, float, ushort3>*/, 0 /*subScalarImpl<float4, float, ushort4>*/},
|
||||
{0 /*subScalarImpl<float, float, short>*/, 0 /*subScalarImpl<float2, float, short2>*/, 0 /*subScalarImpl<float3, float, short3>*/, 0 /*subScalarImpl<float4, float, short4>*/},
|
||||
{0 /*subScalarImpl<float, float, int>*/, 0 /*subScalarImpl<float2, float, int2>*/, 0 /*subScalarImpl<float3, float, int3>*/, 0 /*subScalarImpl<float4, float, int4>*/},
|
||||
{subScalarImpl<float, float, float>, subScalarImpl<float2, float, float2>, subScalarImpl<float3, float, float3>, subScalarImpl<float4, float, float4>},
|
||||
{subScalarImpl<float, double, double>, subScalarImpl<float2, double, double2>, subScalarImpl<float3, double, double3>, subScalarImpl<float4, double, double4Compat>}
|
||||
},
|
||||
{
|
||||
{0 /*subScalarImpl<double, double, uchar>*/, 0 /*subScalarImpl<double2, double, uchar2>*/, 0 /*subScalarImpl<double3, double, uchar3>*/, 0 /*subScalarImpl<double4, double, uchar4>*/},
|
||||
{0 /*subScalarImpl<double, double, schar>*/, 0 /*subScalarImpl<double2, double, char2>*/, 0 /*subScalarImpl<double3, double, char3>*/, 0 /*subScalarImpl<double4, double, char4>*/},
|
||||
{0 /*subScalarImpl<double, double, ushort>*/, 0 /*subScalarImpl<double2, double, ushort2>*/, 0 /*subScalarImpl<double3, double, ushort3>*/, 0 /*subScalarImpl<double4, double, ushort4>*/},
|
||||
{0 /*subScalarImpl<double, double, short>*/, 0 /*subScalarImpl<double2, double, short2>*/, 0 /*subScalarImpl<double3, double, short3>*/, 0 /*subScalarImpl<double4, double, short4>*/},
|
||||
{0 /*subScalarImpl<double, double, int>*/, 0 /*subScalarImpl<double2, double, int2>*/, 0 /*subScalarImpl<double3, double, int3>*/, 0 /*subScalarImpl<double4, double, int4>*/},
|
||||
{0 /*subScalarImpl<double, double, float>*/, 0 /*subScalarImpl<double2, double, float2>*/, 0 /*subScalarImpl<double3, double, float3>*/, 0 /*subScalarImpl<double4, double, float4>*/},
|
||||
{subScalarImpl<double, double, double>, subScalarImpl<double2, double, double2>, subScalarImpl<double3, double, double3>, subScalarImpl<double4Compat, double, double4Compat>}
|
||||
}
|
||||
};
|
||||
|
||||
const int sdepth = src.depth();
|
||||
const int ddepth = dst.depth();
|
||||
const int cn = src.channels();
|
||||
|
||||
CV_DbgAssert( sdepth <= CV_64F && ddepth <= CV_64F && cn <= 4 );
|
||||
|
||||
const func_t func = funcs[sdepth][ddepth][cn - 1];
|
||||
|
||||
if (!func)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
|
||||
|
||||
func(src, val, inv, dst, mask, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,242 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T, typename R, int cn>
|
||||
void sumImpl(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, cn>::type src_type;
|
||||
typedef typename MakeVec<R, cn>::type res_type;
|
||||
|
||||
const GpuMat_<src_type>& src = (const GpuMat_<src_type>&) _src;
|
||||
GpuMat_<res_type>& dst = (GpuMat_<res_type>&) _dst;
|
||||
|
||||
if (mask.empty())
|
||||
gridCalcSum(src, dst, stream);
|
||||
else
|
||||
gridCalcSum(src, dst, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
|
||||
template <typename T, typename R, int cn>
|
||||
void sumAbsImpl(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, cn>::type src_type;
|
||||
typedef typename MakeVec<R, cn>::type res_type;
|
||||
|
||||
const GpuMat_<src_type>& src = (const GpuMat_<src_type>&) _src;
|
||||
GpuMat_<res_type>& dst = (GpuMat_<res_type>&) _dst;
|
||||
|
||||
if (mask.empty())
|
||||
gridCalcSum(abs_(cvt_<res_type>(src)), dst, stream);
|
||||
else
|
||||
gridCalcSum(abs_(cvt_<res_type>(src)), dst, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
|
||||
template <typename T, typename R, int cn>
|
||||
void sumSqrImpl(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream)
|
||||
{
|
||||
typedef typename MakeVec<T, cn>::type src_type;
|
||||
typedef typename MakeVec<R, cn>::type res_type;
|
||||
|
||||
const GpuMat_<src_type>& src = (const GpuMat_<src_type>&) _src;
|
||||
GpuMat_<res_type>& dst = (GpuMat_<res_type>&) _dst;
|
||||
|
||||
if (mask.empty())
|
||||
gridCalcSum(sqr_(cvt_<res_type>(src)), dst, stream);
|
||||
else
|
||||
gridCalcSum(sqr_(cvt_<res_type>(src)), dst, globPtr<uchar>(mask), stream);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::calcSum(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][4] =
|
||||
{
|
||||
{sumImpl<uchar , double, 1>, sumImpl<uchar , double, 2>, sumImpl<uchar , double, 3>, sumImpl<uchar , double, 4>},
|
||||
{sumImpl<schar , double, 1>, sumImpl<schar , double, 2>, sumImpl<schar , double, 3>, sumImpl<schar , double, 4>},
|
||||
{sumImpl<ushort, double, 1>, sumImpl<ushort, double, 2>, sumImpl<ushort, double, 3>, sumImpl<ushort, double, 4>},
|
||||
{sumImpl<short , double, 1>, sumImpl<short , double, 2>, sumImpl<short , double, 3>, sumImpl<short , double, 4>},
|
||||
{sumImpl<int , double, 1>, sumImpl<int , double, 2>, sumImpl<int , double, 3>, sumImpl<int , double, 4>},
|
||||
{sumImpl<float , double, 1>, sumImpl<float , double, 2>, sumImpl<float , double, 3>, sumImpl<float , double, 4>},
|
||||
{sumImpl<double, double, 1>, sumImpl<double, double, 2>, sumImpl<double, double, 3>, sumImpl<double, double, 4>}
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == src.size()) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int channels = src.channels();
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC(channels), stream);
|
||||
|
||||
const func_t func = funcs[src_depth][channels - 1];
|
||||
func(src, dst, mask, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
cv::Scalar cv::cuda::sum(InputArray _src, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
calcSum(_src, dst, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
cv::Scalar val;
|
||||
dst.createMatHeader().convertTo(cv::Mat(dst.size(), CV_64FC(dst.channels()), val.val), CV_64F);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
void cv::cuda::calcAbsSum(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][4] =
|
||||
{
|
||||
{sumAbsImpl<uchar , double, 1>, sumAbsImpl<uchar , double, 2>, sumAbsImpl<uchar , double, 3>, sumAbsImpl<uchar , double, 4>},
|
||||
{sumAbsImpl<schar , double, 1>, sumAbsImpl<schar , double, 2>, sumAbsImpl<schar , double, 3>, sumAbsImpl<schar , double, 4>},
|
||||
{sumAbsImpl<ushort, double, 1>, sumAbsImpl<ushort, double, 2>, sumAbsImpl<ushort, double, 3>, sumAbsImpl<ushort, double, 4>},
|
||||
{sumAbsImpl<short , double, 1>, sumAbsImpl<short , double, 2>, sumAbsImpl<short , double, 3>, sumAbsImpl<short , double, 4>},
|
||||
{sumAbsImpl<int , double, 1>, sumAbsImpl<int , double, 2>, sumAbsImpl<int , double, 3>, sumAbsImpl<int , double, 4>},
|
||||
{sumAbsImpl<float , double, 1>, sumAbsImpl<float , double, 2>, sumAbsImpl<float , double, 3>, sumAbsImpl<float , double, 4>},
|
||||
{sumAbsImpl<double, double, 1>, sumAbsImpl<double, double, 2>, sumAbsImpl<double, double, 3>, sumAbsImpl<double, double, 4>}
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == src.size()) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int channels = src.channels();
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC(channels), stream);
|
||||
|
||||
const func_t func = funcs[src_depth][channels - 1];
|
||||
func(src, dst, mask, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
cv::Scalar cv::cuda::absSum(InputArray _src, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
calcAbsSum(_src, dst, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
cv::Scalar val;
|
||||
dst.createMatHeader().convertTo(cv::Mat(dst.size(), CV_64FC(dst.channels()), val.val), CV_64F);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
void cv::cuda::calcSqrSum(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& _src, GpuMat& _dst, const GpuMat& mask, Stream& stream);
|
||||
static const func_t funcs[7][4] =
|
||||
{
|
||||
{sumSqrImpl<uchar , double, 1>, sumSqrImpl<uchar , double, 2>, sumSqrImpl<uchar , double, 3>, sumSqrImpl<uchar , double, 4>},
|
||||
{sumSqrImpl<schar , double, 1>, sumSqrImpl<schar , double, 2>, sumSqrImpl<schar , double, 3>, sumSqrImpl<schar , double, 4>},
|
||||
{sumSqrImpl<ushort, double, 1>, sumSqrImpl<ushort, double, 2>, sumSqrImpl<ushort, double, 3>, sumSqrImpl<ushort, double, 4>},
|
||||
{sumSqrImpl<short , double, 1>, sumSqrImpl<short , double, 2>, sumSqrImpl<short , double, 3>, sumSqrImpl<short , double, 4>},
|
||||
{sumSqrImpl<int , double, 1>, sumSqrImpl<int , double, 2>, sumSqrImpl<int , double, 3>, sumSqrImpl<int , double, 4>},
|
||||
{sumSqrImpl<float , double, 1>, sumSqrImpl<float , double, 2>, sumSqrImpl<float , double, 3>, sumSqrImpl<float , double, 4>},
|
||||
{sumSqrImpl<double, double, 1>, sumSqrImpl<double, double, 2>, sumSqrImpl<double, double, 3>, sumSqrImpl<double, double, 4>}
|
||||
};
|
||||
|
||||
const GpuMat src = getInputMat(_src, stream);
|
||||
const GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == src.size()) );
|
||||
|
||||
const int src_depth = src.depth();
|
||||
const int channels = src.channels();
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC(channels), stream);
|
||||
|
||||
const func_t func = funcs[src_depth][channels - 1];
|
||||
func(src, dst, mask, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
cv::Scalar cv::cuda::sqrSum(InputArray _src, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
calcSqrSum(_src, dst, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
cv::Scalar val;
|
||||
dst.createMatHeader().convertTo(cv::Mat(dst.size(), CV_64FC(dst.channels()), val.val), CV_64F);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,409 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
|
||||
{
|
||||
};
|
||||
template <> struct TransformPolicy<double> : DefaultTransformPolicy
|
||||
{
|
||||
enum {
|
||||
shift = 1
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void thresholdImpl(const GpuMat& src, GpuMat& dst, double thresh, double maxVal, int type, Stream& stream)
|
||||
{
|
||||
const T thresh_ = static_cast<T>(thresh);
|
||||
const T maxVal_ = static_cast<T>(maxVal);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case 0:
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), thresh_binary_func(thresh_, maxVal_), stream);
|
||||
break;
|
||||
case 1:
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), thresh_binary_inv_func(thresh_, maxVal_), stream);
|
||||
break;
|
||||
case 2:
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), thresh_trunc_func(thresh_), stream);
|
||||
break;
|
||||
case 3:
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), thresh_to_zero_func(thresh_), stream);
|
||||
break;
|
||||
case 4:
|
||||
gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), thresh_to_zero_inv_func(thresh_), stream);
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
__global__ void otsu_sums(uint *histogram, uint *threshold_sums, unsigned long long *sums)
|
||||
{
|
||||
const uint n_bins = 256;
|
||||
|
||||
__shared__ uint shared_memory_ts[n_bins];
|
||||
__shared__ unsigned long long shared_memory_s[n_bins];
|
||||
|
||||
int bin_idx = threadIdx.x;
|
||||
int threshold = blockIdx.x;
|
||||
|
||||
uint threshold_sum_above = 0;
|
||||
unsigned long long sum_above = 0;
|
||||
|
||||
if (bin_idx > threshold)
|
||||
{
|
||||
uint value = histogram[bin_idx];
|
||||
threshold_sum_above = value;
|
||||
sum_above = value * bin_idx;
|
||||
}
|
||||
|
||||
blockReduce<n_bins>(shared_memory_ts, threshold_sum_above, bin_idx, plus<uint>());
|
||||
blockReduce<n_bins>(shared_memory_s, sum_above, bin_idx, plus<unsigned long long>());
|
||||
|
||||
if (bin_idx == 0)
|
||||
{
|
||||
threshold_sums[threshold] = threshold_sum_above;
|
||||
sums[threshold] = sum_above;
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void
|
||||
otsu_variance(float2 *variance, uint *histogram, uint *threshold_sums, unsigned long long *sums, uint n_samples)
|
||||
{
|
||||
const uint n_bins = 256;
|
||||
|
||||
__shared__ signed long long shared_memory_a[n_bins];
|
||||
__shared__ signed long long shared_memory_b[n_bins];
|
||||
|
||||
int bin_idx = threadIdx.x;
|
||||
int threshold = blockIdx.x;
|
||||
|
||||
uint n_samples_above = threshold_sums[threshold];
|
||||
uint n_samples_below = n_samples - n_samples_above;
|
||||
|
||||
unsigned long long total_sum = sums[0];
|
||||
unsigned long long sum_above = sums[threshold];
|
||||
unsigned long long sum_below = total_sum - sum_above;
|
||||
|
||||
float threshold_variance_above_f32 = 0;
|
||||
float threshold_variance_below_f32 = 0;
|
||||
if (bin_idx > threshold)
|
||||
{
|
||||
if (n_samples_above > 0)
|
||||
{
|
||||
float mean = (float) sum_above / n_samples_above;
|
||||
float sigma = bin_idx - mean;
|
||||
threshold_variance_above_f32 = sigma * sigma;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n_samples_below > 0)
|
||||
{
|
||||
float mean = (float) sum_below / n_samples_below;
|
||||
float sigma = bin_idx - mean;
|
||||
threshold_variance_below_f32 = sigma * sigma;
|
||||
}
|
||||
}
|
||||
|
||||
uint bin_count = histogram[bin_idx];
|
||||
signed long long threshold_variance_above_i64 = (signed long long)(threshold_variance_above_f32 * bin_count);
|
||||
signed long long threshold_variance_below_i64 = (signed long long)(threshold_variance_below_f32 * bin_count);
|
||||
blockReduce<n_bins>(shared_memory_a, threshold_variance_above_i64, bin_idx, plus<signed long long>());
|
||||
blockReduce<n_bins>(shared_memory_b, threshold_variance_below_i64, bin_idx, plus<signed long long>());
|
||||
|
||||
if (bin_idx == 0)
|
||||
{
|
||||
variance[threshold] = make_float2(threshold_variance_above_i64, threshold_variance_below_i64);
|
||||
}
|
||||
}
|
||||
|
||||
template <uint n_thresholds>
|
||||
__device__ bool has_lowest_score(
|
||||
uint threshold, float original_score, float score, uint *shared_memory
|
||||
) {
|
||||
// It may happen that multiple threads have the same minimum score. In that case, we want to find the thread with
|
||||
// the lowest threshold. This is done by calling '__syncthreads_count' to count how many threads have a score
|
||||
// that matches to the minimum score found. Since this is rare, we will optimize towards the common case where only
|
||||
// one thread has the minimum score. If multiple threads have the same minimum score, we will find the minimum
|
||||
// threshold that satifies the condition
|
||||
bool has_match = original_score == score;
|
||||
uint matches = __syncthreads_count(has_match);
|
||||
|
||||
if(matches > 1) {
|
||||
// If this thread has a match, we use it; otherwise we give it a value that is larger than the maximum
|
||||
// threshold, so it will never get picked
|
||||
uint min_threshold = has_match ? threshold : n_thresholds;
|
||||
|
||||
blockReduce<n_thresholds>(shared_memory, min_threshold, threshold, minimum<uint>());
|
||||
|
||||
return min_threshold == threshold;
|
||||
} else {
|
||||
return has_match;
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void
|
||||
otsu_score(uint *otsu_threshold, uint *threshold_sums, float2 *variance, uint n_samples)
|
||||
{
|
||||
const uint n_thresholds = 256;
|
||||
|
||||
__shared__ float shared_memory[n_thresholds];
|
||||
|
||||
int threshold = threadIdx.x;
|
||||
|
||||
uint n_samples_above = threshold_sums[threshold];
|
||||
uint n_samples_below = n_samples - n_samples_above;
|
||||
|
||||
float threshold_mean_above = (float)n_samples_above / n_samples;
|
||||
float threshold_mean_below = (float)n_samples_below / n_samples;
|
||||
|
||||
float2 variances = variance[threshold];
|
||||
float variance_above = n_samples_above > 0 ? variances.x / n_samples_above : 0.0f;
|
||||
float variance_below = n_samples_below > 0 ? variances.y / n_samples_below : 0.0f;
|
||||
|
||||
float above = threshold_mean_above * variance_above;
|
||||
float below = threshold_mean_below * variance_below;
|
||||
float score = above + below;
|
||||
|
||||
float original_score = score;
|
||||
|
||||
blockReduce<n_thresholds>(shared_memory, score, threshold, minimum<float>());
|
||||
|
||||
if (threshold == 0)
|
||||
{
|
||||
shared_memory[0] = score;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
score = shared_memory[0];
|
||||
|
||||
// We found the minimum score, but in some cases multiple threads can have the same score, so we need to find the
|
||||
// lowest threshold
|
||||
if (has_lowest_score<n_thresholds>(threshold, original_score, score, (uint *) shared_memory))
|
||||
{
|
||||
*otsu_threshold = threshold;
|
||||
}
|
||||
}
|
||||
|
||||
void compute_otsu(uint *histogram, uint *otsu_threshold, uint n_samples, Stream &stream)
|
||||
{
|
||||
const uint n_bins = 256;
|
||||
const uint n_thresholds = 256;
|
||||
|
||||
cudaStream_t cuda_stream = StreamAccessor::getStream(stream);
|
||||
|
||||
dim3 block_all(n_bins);
|
||||
dim3 grid_all(n_thresholds);
|
||||
dim3 block_score(n_thresholds);
|
||||
dim3 grid_score(1);
|
||||
|
||||
BufferPool pool(stream);
|
||||
GpuMat gpu_threshold_sums(1, n_bins, CV_32SC1, pool.getAllocator());
|
||||
GpuMat gpu_sums(1, n_bins, CV_64FC1, pool.getAllocator());
|
||||
GpuMat gpu_variances(1, n_bins, CV_32FC2, pool.getAllocator());
|
||||
|
||||
otsu_sums<<<grid_all, block_all, 0, cuda_stream>>>(
|
||||
histogram, gpu_threshold_sums.ptr<uint>(), gpu_sums.ptr<unsigned long long>());
|
||||
otsu_variance<<<grid_all, block_all, 0, cuda_stream>>>(
|
||||
gpu_variances.ptr<float2>(), histogram, gpu_threshold_sums.ptr<uint>(), gpu_sums.ptr<unsigned long long>(), n_samples);
|
||||
otsu_score<<<grid_score, block_score, 0, cuda_stream>>>(
|
||||
otsu_threshold, gpu_threshold_sums.ptr<uint>(), gpu_variances.ptr<float2>(), n_samples);
|
||||
}
|
||||
|
||||
// TODO: Replace this with cv::cuda::calcHist
|
||||
template <uint n_bins>
|
||||
__global__ void histogram_kernel(
|
||||
uint *histogram, const uint8_t *image, uint width,
|
||||
uint height, uint pitch)
|
||||
{
|
||||
__shared__ uint local_histogram[n_bins];
|
||||
|
||||
uint x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
uint y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
uint tid = threadIdx.y * blockDim.x + threadIdx.x;
|
||||
|
||||
if (tid < n_bins)
|
||||
{
|
||||
local_histogram[tid] = 0;
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
if (x < width && y < height)
|
||||
{
|
||||
uint8_t value = image[y * pitch + x];
|
||||
atomicInc(&local_histogram[value], 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
if (tid < n_bins)
|
||||
{
|
||||
cv::cudev::atomicAdd(&histogram[tid], local_histogram[tid]);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Replace this with cv::cuda::calcHist
|
||||
void calcHist(
|
||||
const GpuMat src, GpuMat histogram, Stream stream)
|
||||
{
|
||||
const uint n_bins = 256;
|
||||
|
||||
cudaStream_t cuda_stream = StreamAccessor::getStream(stream);
|
||||
|
||||
dim3 block(128, 4, 1);
|
||||
dim3 grid = dim3(divUp(src.cols, block.x), divUp(src.rows, block.y), 1);
|
||||
CV_CUDEV_SAFE_CALL(cudaMemsetAsync(histogram.ptr<uint>(), 0, n_bins * sizeof(uint), cuda_stream));
|
||||
histogram_kernel<n_bins>
|
||||
<<<grid, block, 0, cuda_stream>>>(
|
||||
histogram.ptr<uint>(), src.ptr<uint8_t>(), (uint) src.cols, (uint) src.rows, (uint) src.step);
|
||||
}
|
||||
|
||||
double cv::cuda::threshold(InputArray _src, OutputArray _dst, double thresh, double maxVal, int type, Stream &stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
const int depth = src.depth();
|
||||
|
||||
const int THRESH_OTSU = 8;
|
||||
if ((type & THRESH_OTSU) == THRESH_OTSU)
|
||||
{
|
||||
CV_Assert(depth == CV_8U);
|
||||
CV_Assert(src.channels() == 1);
|
||||
|
||||
BufferPool pool(stream);
|
||||
|
||||
// Find the threshold using Otsu and then run the normal thresholding algorithm
|
||||
GpuMat gpu_histogram(256, 1, CV_32SC1, pool.getAllocator());
|
||||
calcHist(src, gpu_histogram, stream);
|
||||
|
||||
GpuMat gpu_otsu_threshold(1, 1, CV_32SC1, pool.getAllocator());
|
||||
compute_otsu(gpu_histogram.ptr<uint>(), gpu_otsu_threshold.ptr<uint>(), src.rows * src.cols, stream);
|
||||
|
||||
cv::Mat mat_otsu_threshold;
|
||||
gpu_otsu_threshold.download(mat_otsu_threshold, stream);
|
||||
stream.waitForCompletion();
|
||||
|
||||
// Overwrite the threshold value with the Otsu value and remove the Otsu flag from the type
|
||||
type = type & ~THRESH_OTSU;
|
||||
thresh = (double) mat_otsu_threshold.at<int>(0);
|
||||
}
|
||||
|
||||
CV_Assert( depth <= CV_64F );
|
||||
CV_Assert( type <= 4 /*THRESH_TOZERO_INV*/ );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
src = src.reshape(1);
|
||||
dst = dst.reshape(1);
|
||||
|
||||
if (depth == CV_32F && type == 2 /*THRESH_TRUNC*/)
|
||||
{
|
||||
NppStreamHandler h(StreamAccessor::getStream(stream));
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = src.cols;
|
||||
sz.height = src.rows;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(nppiThreshold_32f_C1R_Ctx(src.ptr<Npp32f>(), static_cast<int>(src.step),
|
||||
dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz, static_cast<Npp32f>(thresh), NPP_CMP_GREATER, h));
|
||||
#else
|
||||
nppSafeCall( nppiThreshold_32f_C1R(src.ptr<Npp32f>(), static_cast<int>(src.step),
|
||||
dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz, static_cast<Npp32f>(thresh), NPP_CMP_GREATER) );
|
||||
#endif
|
||||
|
||||
if (!stream)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, GpuMat& dst, double thresh, double maxVal, int type, Stream& stream);
|
||||
static const func_t funcs[CV_DEPTH_MAX] =
|
||||
{
|
||||
thresholdImpl<uchar>,
|
||||
thresholdImpl<schar>,
|
||||
thresholdImpl<ushort>,
|
||||
thresholdImpl<short>,
|
||||
thresholdImpl<int>,
|
||||
thresholdImpl<float>,
|
||||
thresholdImpl<double>
|
||||
};
|
||||
|
||||
if (depth != CV_32F && depth != CV_64F)
|
||||
{
|
||||
thresh = cvFloor(thresh);
|
||||
maxVal = cvRound(maxVal);
|
||||
}
|
||||
|
||||
auto f = funcs[depth];
|
||||
CV_Assert(f);
|
||||
|
||||
f(src, dst, thresh, maxVal, type, stream);
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
|
||||
return thresh;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,100 @@
|
||||
/*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 "opencv2/opencv_modules.hpp"
|
||||
|
||||
#ifndef HAVE_OPENCV_CUDEV
|
||||
|
||||
#error "opencv_cudev is required"
|
||||
|
||||
#else
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/cudev.hpp"
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
using namespace cv::cudev;
|
||||
|
||||
void cv::cuda::transpose(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
const size_t elemSize = src.elemSize();
|
||||
|
||||
CV_Assert( elemSize == 1 || elemSize == 4 || elemSize == 8 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.cols, src.rows, src.type(), stream);
|
||||
|
||||
if (elemSize == 1)
|
||||
{
|
||||
NppStreamHandler h(StreamAccessor::getStream(stream));
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = src.cols;
|
||||
sz.height = src.rows;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(nppiTranspose_8u_C1R_Ctx(src.ptr<Npp8u>(), static_cast<int>(src.step),
|
||||
dst.ptr<Npp8u>(), static_cast<int>(dst.step), sz, h));
|
||||
#else
|
||||
nppSafeCall( nppiTranspose_8u_C1R(src.ptr<Npp8u>(), static_cast<int>(src.step),
|
||||
dst.ptr<Npp8u>(), static_cast<int>(dst.step), sz) );
|
||||
#endif
|
||||
|
||||
if (!stream)
|
||||
CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
|
||||
}
|
||||
else if (elemSize == 4)
|
||||
{
|
||||
gridTranspose(globPtr<int>(src), globPtr<int>(dst), stream);
|
||||
}
|
||||
else // if (elemSize == 8)
|
||||
{
|
||||
gridTranspose(globPtr<double>(src), globPtr<double>(dst), stream);
|
||||
}
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,561 @@
|
||||
/*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"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
void cv::cuda::add(InputArray, InputArray, OutputArray, InputArray, int, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::subtract(InputArray, InputArray, OutputArray, InputArray, int, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::multiply(InputArray, InputArray, OutputArray, double, int, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::divide(InputArray, InputArray, OutputArray, double, int, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::absdiff(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::abs(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::sqr(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::sqrt(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::exp(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::log(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::pow(InputArray, double, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::compare(InputArray, InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::bitwise_not(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::bitwise_or(InputArray, InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::bitwise_and(InputArray, InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::bitwise_xor(InputArray, InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::rshift(InputArray, Scalar_<int>, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::lshift(InputArray, Scalar_<int>, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::min(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::max(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::addWeighted(InputArray, double, InputArray, double, double, OutputArray, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
double cv::cuda::threshold(InputArray, OutputArray, double, double, int, Stream&) {throw_no_cuda(); return 0.0;}
|
||||
|
||||
void cv::cuda::inRange(InputArray, const Scalar&, const Scalar&, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::magnitude(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::magnitude(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::magnitudeSqr(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::magnitudeSqr(InputArray, InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::phase(InputArray, InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::phase(InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::cartToPolar(InputArray, InputArray, OutputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::cartToPolar(InputArray, OutputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::cartToPolar(InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::polarToCart(InputArray, InputArray, OutputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::polarToCart(InputArray, InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::polarToCart(InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
|
||||
|
||||
#else
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// arithm_op
|
||||
|
||||
namespace
|
||||
{
|
||||
typedef void (*mat_mat_func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int op);
|
||||
typedef void (*mat_scalar_func_t)(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int op);
|
||||
|
||||
void arithm_op(InputArray _src1, InputArray _src2, OutputArray _dst, InputArray _mask, double scale, int dtype, Stream& stream,
|
||||
mat_mat_func_t mat_mat_func, mat_scalar_func_t mat_scalar_func, int op = 0)
|
||||
{
|
||||
const int kind1 = _src1.kind();
|
||||
const int kind2 = _src2.kind();
|
||||
|
||||
const bool isScalar1 = (kind1 == _InputArray::MATX);
|
||||
const bool isScalar2 = (kind2 == _InputArray::MATX);
|
||||
CV_Assert( !isScalar1 || !isScalar2 );
|
||||
|
||||
GpuMat src1;
|
||||
if (!isScalar1)
|
||||
src1 = getInputMat(_src1, stream);
|
||||
|
||||
GpuMat src2;
|
||||
if (!isScalar2)
|
||||
src2 = getInputMat(_src2, stream);
|
||||
|
||||
Mat scalar;
|
||||
if (isScalar1)
|
||||
scalar = _src1.getMat();
|
||||
else if (isScalar2)
|
||||
scalar = _src2.getMat();
|
||||
|
||||
Scalar val;
|
||||
if (!scalar.empty())
|
||||
{
|
||||
CV_Assert( scalar.total() <= 4 );
|
||||
scalar.convertTo(Mat_<double>(scalar.rows, scalar.cols, &val[0]), CV_64F);
|
||||
}
|
||||
|
||||
GpuMat mask = getInputMat(_mask, stream);
|
||||
|
||||
const int sdepth = src1.empty() ? src2.depth() : src1.depth();
|
||||
const int cn = src1.empty() ? src2.channels() : src1.channels();
|
||||
const Size size = src1.empty() ? src2.size() : src1.size();
|
||||
|
||||
if (dtype < 0)
|
||||
dtype = sdepth;
|
||||
|
||||
const int ddepth = CV_MAT_DEPTH(dtype);
|
||||
|
||||
CV_Assert( sdepth <= CV_64F && ddepth <= CV_64F );
|
||||
CV_Assert( !scalar.empty() || (src2.type() == src1.type() && src2.size() == src1.size()) );
|
||||
CV_Assert( mask.empty() || (cn == 1 && mask.size() == size && mask.type() == CV_8UC1) );
|
||||
|
||||
if (sdepth == CV_64F || ddepth == CV_64F)
|
||||
{
|
||||
if (!deviceSupports(NATIVE_DOUBLE))
|
||||
CV_Error(Error::StsUnsupportedFormat, "The device doesn't support double");
|
||||
}
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, size, CV_MAKE_TYPE(ddepth, cn), stream);
|
||||
|
||||
if (isScalar1)
|
||||
mat_scalar_func(src2, val, true, dst, mask, scale, stream, op);
|
||||
else if (isScalar2)
|
||||
mat_scalar_func(src1, val, false, dst, mask, scale, stream, op);
|
||||
else
|
||||
mat_mat_func(src1, src2, dst, mask, scale, stream, op);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// add
|
||||
|
||||
void addMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int);
|
||||
|
||||
void addScalar(const GpuMat& src, Scalar val, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int);
|
||||
|
||||
void cv::cuda::add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, int dtype, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, mask, 1.0, dtype, stream, addMat, addScalar);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// subtract
|
||||
|
||||
void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int);
|
||||
|
||||
void subScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int);
|
||||
|
||||
void cv::cuda::subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, int dtype, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, mask, 1.0, dtype, stream, subMat, subScalar);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// multiply
|
||||
|
||||
void mulMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int);
|
||||
void mulMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
void mulMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
|
||||
void mulScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int);
|
||||
|
||||
void cv::cuda::multiply(InputArray _src1, InputArray _src2, OutputArray _dst, double scale, int dtype, Stream& stream)
|
||||
{
|
||||
if (_src1.type() == CV_8UC4 && _src2.type() == CV_32FC1)
|
||||
{
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), src1.type(), stream);
|
||||
|
||||
mulMat_8uc4_32f(src1, src2, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
else if (_src1.type() == CV_16SC4 && _src2.type() == CV_32FC1)
|
||||
{
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), src1.type(), stream);
|
||||
|
||||
mulMat_16sc4_32f(src1, src2, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
arithm_op(_src1, _src2, _dst, GpuMat(), scale, dtype, stream, mulMat, mulScalar);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// divide
|
||||
|
||||
void divMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double scale, Stream& stream, int);
|
||||
void divMat_8uc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
void divMat_16sc4_32f(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
|
||||
|
||||
void divScalar(const GpuMat& src, cv::Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double scale, Stream& stream, int);
|
||||
|
||||
void cv::cuda::divide(InputArray _src1, InputArray _src2, OutputArray _dst, double scale, int dtype, Stream& stream)
|
||||
{
|
||||
if (_src1.type() == CV_8UC4 && _src2.type() == CV_32FC1)
|
||||
{
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), src1.type(), stream);
|
||||
|
||||
divMat_8uc4_32f(src1, src2, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
else if (_src1.type() == CV_16SC4 && _src2.type() == CV_32FC1)
|
||||
{
|
||||
GpuMat src1 = getInputMat(_src1, stream);
|
||||
GpuMat src2 = getInputMat(_src2, stream);
|
||||
|
||||
CV_Assert( src1.size() == src2.size() );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src1.size(), src1.type(), stream);
|
||||
|
||||
divMat_16sc4_32f(src1, src2, dst, stream);
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
arithm_op(_src1, _src2, _dst, GpuMat(), scale, dtype, stream, divMat, divScalar);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// absdiff
|
||||
|
||||
void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int);
|
||||
|
||||
void absDiffScalar(const GpuMat& src, cv::Scalar val, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int);
|
||||
|
||||
void cv::cuda::absdiff(InputArray src1, InputArray src2, OutputArray dst, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, noArray(), 1.0, -1, stream, absDiffMat, absDiffScalar);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// compare
|
||||
|
||||
void cmpMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop);
|
||||
|
||||
void cmpScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat&, double, Stream& stream, int cmpop);
|
||||
|
||||
void cv::cuda::compare(InputArray src1, InputArray src2, OutputArray dst, int cmpop, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, noArray(), 1.0, CV_8U, stream, cmpMat, cmpScalar, cmpop);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Binary bitwise logical operations
|
||||
|
||||
namespace
|
||||
{
|
||||
enum
|
||||
{
|
||||
BIT_OP_AND,
|
||||
BIT_OP_OR,
|
||||
BIT_OP_XOR
|
||||
};
|
||||
}
|
||||
|
||||
void bitMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op);
|
||||
|
||||
void bitScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int op);
|
||||
|
||||
void cv::cuda::bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, mask, 1.0, -1, stream, bitMat, bitScalar, BIT_OP_OR);
|
||||
}
|
||||
|
||||
void cv::cuda::bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, mask, 1.0, -1, stream, bitMat, bitScalar, BIT_OP_AND);
|
||||
}
|
||||
|
||||
void cv::cuda::bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, mask, 1.0, -1, stream, bitMat, bitScalar, BIT_OP_XOR);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// shift
|
||||
|
||||
namespace
|
||||
{
|
||||
template <int DEPTH, int cn> struct NppShiftFunc
|
||||
{
|
||||
typedef typename NPPTypeTraits<DEPTH>::npp_type npp_type;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus(*func_t)(const npp_type* pSrc1, int nSrc1Step, const Npp32u* pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus (*func_t)(const npp_type* pSrc1, int nSrc1Step, const Npp32u* pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI);
|
||||
#endif
|
||||
};
|
||||
template <int DEPTH> struct NppShiftFunc<DEPTH, 1>
|
||||
{
|
||||
typedef typename NPPTypeTraits<DEPTH>::npp_type npp_type;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus(*func_t)(const npp_type* pSrc1, int nSrc1Step, const Npp32u pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus (*func_t)(const npp_type* pSrc1, int nSrc1Step, const Npp32u pConstants, npp_type* pDst, int nDstStep, NppiSize oSizeROI);
|
||||
#endif
|
||||
};
|
||||
|
||||
template <int DEPTH, int cn, typename NppShiftFunc<DEPTH, cn>::func_t func> struct NppShift
|
||||
{
|
||||
typedef typename NPPTypeTraits<DEPTH>::npp_type npp_type;
|
||||
|
||||
static void call(const GpuMat& src, Scalar_<Npp32u> sc, GpuMat& dst, cudaStream_t stream)
|
||||
{
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiSize oSizeROI;
|
||||
oSizeROI.width = src.cols;
|
||||
oSizeROI.height = src.rows;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(func(src.ptr<npp_type>(), static_cast<int>(src.step), sc.val, dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI, h));
|
||||
#else
|
||||
nppSafeCall( func(src.ptr<npp_type>(), static_cast<int>(src.step), sc.val, dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
};
|
||||
template <int DEPTH, typename NppShiftFunc<DEPTH, 1>::func_t func> struct NppShift<DEPTH, 1, func>
|
||||
{
|
||||
typedef typename NPPTypeTraits<DEPTH>::npp_type npp_type;
|
||||
|
||||
static void call(const GpuMat& src, Scalar_<Npp32u> sc, GpuMat& dst, cudaStream_t stream)
|
||||
{
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiSize oSizeROI;
|
||||
oSizeROI.width = src.cols;
|
||||
oSizeROI.height = src.rows;
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(func(src.ptr<npp_type>(), static_cast<int>(src.step), sc.val[0], dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI, h));
|
||||
#else
|
||||
nppSafeCall( func(src.ptr<npp_type>(), static_cast<int>(src.step), sc.val[0], dst.ptr<npp_type>(), static_cast<int>(dst.step), oSizeROI) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void cv::cuda::rshift(InputArray _src, Scalar_<int> val, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, Scalar_<Npp32u> sc, GpuMat& dst, cudaStream_t stream);
|
||||
static const func_t funcs[5][4] =
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
|
||||
{NppShift<CV_8U , 1, nppiRShiftC_8u_C1R_Ctx>::call, 0, NppShift<CV_8U , 3, nppiRShiftC_8u_C3R_Ctx>::call, NppShift<CV_8U , 4, nppiRShiftC_8u_C4R_Ctx>::call },
|
||||
{NppShift<CV_8S , 1, nppiRShiftC_8s_C1R_Ctx>::call, 0, NppShift<CV_8S , 3, nppiRShiftC_8s_C3R_Ctx>::call, NppShift<CV_8S , 4, nppiRShiftC_8s_C4R_Ctx>::call },
|
||||
{NppShift<CV_16U, 1, nppiRShiftC_16u_C1R_Ctx>::call, 0, NppShift<CV_16U, 3, nppiRShiftC_16u_C3R_Ctx>::call, NppShift<CV_16U, 4, nppiRShiftC_16u_C4R_Ctx>::call},
|
||||
{NppShift<CV_16S, 1, nppiRShiftC_16s_C1R_Ctx>::call, 0, NppShift<CV_16S, 3, nppiRShiftC_16s_C3R_Ctx>::call, NppShift<CV_16S, 4, nppiRShiftC_16s_C4R_Ctx>::call},
|
||||
{NppShift<CV_32S, 1, nppiRShiftC_32s_C1R_Ctx>::call, 0, NppShift<CV_32S, 3, nppiRShiftC_32s_C3R_Ctx>::call, NppShift<CV_32S, 4, nppiRShiftC_32s_C4R_Ctx>::call},
|
||||
#else
|
||||
{NppShift<CV_8U , 1, nppiRShiftC_8u_C1R >::call, 0, NppShift<CV_8U , 3, nppiRShiftC_8u_C3R >::call, NppShift<CV_8U , 4, nppiRShiftC_8u_C4R>::call },
|
||||
{NppShift<CV_8S , 1, nppiRShiftC_8s_C1R >::call, 0, NppShift<CV_8S , 3, nppiRShiftC_8s_C3R >::call, NppShift<CV_8S , 4, nppiRShiftC_8s_C4R>::call },
|
||||
{NppShift<CV_16U, 1, nppiRShiftC_16u_C1R>::call, 0, NppShift<CV_16U, 3, nppiRShiftC_16u_C3R>::call, NppShift<CV_16U, 4, nppiRShiftC_16u_C4R>::call},
|
||||
{NppShift<CV_16S, 1, nppiRShiftC_16s_C1R>::call, 0, NppShift<CV_16S, 3, nppiRShiftC_16s_C3R>::call, NppShift<CV_16S, 4, nppiRShiftC_16s_C4R>::call},
|
||||
{NppShift<CV_32S, 1, nppiRShiftC_32s_C1R>::call, 0, NppShift<CV_32S, 3, nppiRShiftC_32s_C3R>::call, NppShift<CV_32S, 4, nppiRShiftC_32s_C4R>::call},
|
||||
#endif
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() < CV_32F );
|
||||
CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()][src.channels() - 1](src, val, dst, StreamAccessor::getStream(stream));
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::lshift(InputArray _src, Scalar_<int> val, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
typedef void (*func_t)(const GpuMat& src, Scalar_<Npp32u> sc, GpuMat& dst, cudaStream_t stream);
|
||||
static const func_t funcs[5][4] =
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
{NppShift<CV_8U , 1, nppiLShiftC_8u_C1R_Ctx>::call , 0, NppShift<CV_8U , 3, nppiLShiftC_8u_C3R_Ctx>::call , NppShift<CV_8U , 4, nppiLShiftC_8u_C4R_Ctx>::call },
|
||||
{0 , 0, 0 , 0 },
|
||||
{NppShift<CV_16U, 1, nppiLShiftC_16u_C1R_Ctx>::call, 0, NppShift<CV_16U, 3, nppiLShiftC_16u_C3R_Ctx>::call, NppShift<CV_16U, 4, nppiLShiftC_16u_C4R_Ctx>::call},
|
||||
{0 , 0, 0 , 0 },
|
||||
{NppShift<CV_32S, 1, nppiLShiftC_32s_C1R_Ctx>::call, 0, NppShift<CV_32S, 3, nppiLShiftC_32s_C3R_Ctx>::call, NppShift<CV_32S, 4, nppiLShiftC_32s_C4R_Ctx>::call},
|
||||
#else
|
||||
{NppShift<CV_8U , 1, nppiLShiftC_8u_C1R>::call , 0, NppShift<CV_8U , 3, nppiLShiftC_8u_C3R>::call , NppShift<CV_8U , 4, nppiLShiftC_8u_C4R>::call },
|
||||
{0 , 0, 0 , 0 },
|
||||
{NppShift<CV_16U, 1, nppiLShiftC_16u_C1R>::call, 0, NppShift<CV_16U, 3, nppiLShiftC_16u_C3R>::call, NppShift<CV_16U, 4, nppiLShiftC_16u_C4R>::call},
|
||||
{0 , 0, 0 , 0 },
|
||||
{NppShift<CV_32S, 1, nppiLShiftC_32s_C1R>::call, 0, NppShift<CV_32S, 3, nppiLShiftC_32s_C3R>::call, NppShift<CV_32S, 4, nppiLShiftC_32s_C4R>::call},
|
||||
#endif
|
||||
};
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32S );
|
||||
CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
|
||||
|
||||
funcs[src.depth()][src.channels() - 1](src, val, dst, StreamAccessor::getStream(stream));
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Minimum and maximum operations
|
||||
|
||||
namespace
|
||||
{
|
||||
enum
|
||||
{
|
||||
MIN_OP,
|
||||
MAX_OP
|
||||
};
|
||||
}
|
||||
|
||||
void minMaxMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int op);
|
||||
|
||||
void minMaxScalar(const GpuMat& src, cv::Scalar value, bool, GpuMat& dst, const GpuMat&, double, Stream& stream, int op);
|
||||
|
||||
void cv::cuda::min(InputArray src1, InputArray src2, OutputArray dst, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, noArray(), 1.0, -1, stream, minMaxMat, minMaxScalar, MIN_OP);
|
||||
}
|
||||
|
||||
void cv::cuda::max(InputArray src1, InputArray src2, OutputArray dst, Stream& stream)
|
||||
{
|
||||
arithm_op(src1, src2, dst, noArray(), 1.0, -1, stream, minMaxMat, minMaxScalar, MAX_OP);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// NPP magnitide
|
||||
|
||||
namespace
|
||||
{
|
||||
#if USE_NPP_STREAM_CTX
|
||||
typedef NppStatus(*nppMagnitude_t)(const Npp32fc* pSrc, int nSrcStep, Npp32f* pDst, int nDstStep, NppiSize oSizeROI, NppStreamContext ctx);
|
||||
#else
|
||||
typedef NppStatus (*nppMagnitude_t)(const Npp32fc* pSrc, int nSrcStep, Npp32f* pDst, int nDstStep, NppiSize oSizeROI);
|
||||
#endif
|
||||
|
||||
void npp_magnitude(const GpuMat& src, GpuMat& dst, nppMagnitude_t func, cudaStream_t stream)
|
||||
{
|
||||
CV_Assert(src.type() == CV_32FC2);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = src.cols;
|
||||
sz.height = src.rows;
|
||||
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(func(src.ptr<Npp32fc>(), static_cast<int>(src.step), dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz, h));
|
||||
#else
|
||||
nppSafeCall( func(src.ptr<Npp32fc>(), static_cast<int>(src.step), dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
}
|
||||
|
||||
void cv::cuda::magnitude(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), CV_32FC1, stream);
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
npp_magnitude(src, dst, nppiMagnitude_32fc32f_C1R_Ctx, StreamAccessor::getStream(stream));
|
||||
#else
|
||||
npp_magnitude(src, dst, nppiMagnitude_32fc32f_C1R, StreamAccessor::getStream(stream));
|
||||
#endif
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::magnitudeSqr(InputArray _src, OutputArray _dst, Stream& stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), CV_32FC1, stream);
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
npp_magnitude(src, dst, nppiMagnitudeSqr_32fc32f_C1R_Ctx, StreamAccessor::getStream(stream));
|
||||
#else
|
||||
npp_magnitude(src, dst, nppiMagnitudeSqr_32fc32f_C1R, StreamAccessor::getStream(stream));
|
||||
#endif
|
||||
|
||||
syncOutput(dst, _dst, stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
Ptr<LookUpTable> cv::cuda::createLookUpTable(InputArray) { throw_no_cuda(); return Ptr<LookUpTable>(); }
|
||||
|
||||
#else /* !defined (HAVE_CUDA) || defined (CUDA_DISABLER) */
|
||||
|
||||
// lut.hpp includes cuda_runtime.h and can only be included when we have CUDA
|
||||
#include "lut.hpp"
|
||||
|
||||
Ptr<LookUpTable> cv::cuda::createLookUpTable(InputArray lut)
|
||||
{
|
||||
return makePtr<LookUpTableImpl>(lut);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#ifndef __CUDAARITHM_LUT_HPP__
|
||||
#define __CUDAARITHM_LUT_HPP__
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
namespace cv { namespace cuda {
|
||||
|
||||
class LookUpTableImpl : public LookUpTable
|
||||
{
|
||||
public:
|
||||
LookUpTableImpl(InputArray lut);
|
||||
void transform(InputArray src, OutputArray dst, Stream& stream = Stream::Null()) CV_OVERRIDE;
|
||||
private:
|
||||
GpuMat d_lut;
|
||||
size_t szInBytes = 0;
|
||||
};
|
||||
|
||||
} }
|
||||
|
||||
#endif // __CUDAARITHM_LUT_HPP__
|
||||
@@ -0,0 +1,63 @@
|
||||
/*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*/
|
||||
|
||||
#ifndef __OPENCV_PRECOMP_H__
|
||||
#define __OPENCV_PRECOMP_H__
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "cvconfig.h"
|
||||
|
||||
#include "opencv2/cudaarithm.hpp"
|
||||
#include "opencv2/core/utility.hpp"
|
||||
|
||||
#include "opencv2/core/private.cuda.hpp"
|
||||
|
||||
#ifdef HAVE_CUBLAS
|
||||
# include <cublas.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CUFFT
|
||||
# include <cufft.h>
|
||||
#endif
|
||||
|
||||
#endif /* __OPENCV_PRECOMP_H__ */
|
||||
@@ -0,0 +1,337 @@
|
||||
/*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"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::cuda;
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
double cv::cuda::norm(InputArray, int, InputArray) { throw_no_cuda(); return 0.0; }
|
||||
void cv::cuda::calcNorm(InputArray, OutputArray, int, InputArray, Stream&) { throw_no_cuda(); }
|
||||
double cv::cuda::norm(InputArray, InputArray, int) { throw_no_cuda(); return 0.0; }
|
||||
void cv::cuda::calcNormDiff(InputArray, InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
Scalar cv::cuda::sum(InputArray, InputArray) { throw_no_cuda(); return Scalar(); }
|
||||
void cv::cuda::calcSum(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
Scalar cv::cuda::absSum(InputArray, InputArray) { throw_no_cuda(); return Scalar(); }
|
||||
void cv::cuda::calcAbsSum(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
Scalar cv::cuda::sqrSum(InputArray, InputArray) { throw_no_cuda(); return Scalar(); }
|
||||
void cv::cuda::calcSqrSum(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::minMax(InputArray, double*, double*, InputArray) { throw_no_cuda(); }
|
||||
void cv::cuda::findMinMax(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::minMaxLoc(InputArray, double*, double*, Point*, Point*, InputArray) { throw_no_cuda(); }
|
||||
void cv::cuda::findMinMaxLoc(InputArray, OutputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
int cv::cuda::countNonZero(InputArray) { throw_no_cuda(); return 0; }
|
||||
void cv::cuda::countNonZero(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::reduce(InputArray, OutputArray, int, int, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::meanStdDev(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::meanStdDev(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::meanStdDev(InputArray, Scalar&, Scalar&, InputArray) { throw_no_cuda(); }
|
||||
void cv::cuda::meanStdDev(InputArray, Scalar&, Scalar&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::rectStdDev(InputArray, InputArray, OutputArray, Rect, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::normalize(InputArray, OutputArray, double, double, int, int, InputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::cuda::integral(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
void cv::cuda::sqrIntegral(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
|
||||
|
||||
#else
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// norm
|
||||
|
||||
namespace cv { namespace cuda { namespace device {
|
||||
|
||||
void normL2(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _mask, Stream& stream);
|
||||
|
||||
void findMaxAbs(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _mask, Stream& stream);
|
||||
|
||||
}}}
|
||||
|
||||
void cv::cuda::calcNorm(InputArray _src, OutputArray dst, int normType, InputArray mask, Stream& stream)
|
||||
{
|
||||
CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 );
|
||||
|
||||
GpuMat src = getInputMat(_src, stream);
|
||||
|
||||
GpuMat src_single_channel = src.reshape(1);
|
||||
|
||||
if (normType == NORM_L1)
|
||||
{
|
||||
calcAbsSum(src_single_channel, dst, mask, stream);
|
||||
}
|
||||
else if (normType == NORM_L2)
|
||||
{
|
||||
cv::cuda::device::normL2(src_single_channel, dst, mask, stream);
|
||||
}
|
||||
else // NORM_INF
|
||||
{
|
||||
cv::cuda::device::findMaxAbs(src_single_channel, dst, mask, stream);
|
||||
}
|
||||
}
|
||||
|
||||
double cv::cuda::norm(InputArray _src, int normType, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
calcNorm(_src, dst, normType, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double val;
|
||||
dst.createMatHeader().convertTo(Mat(1, 1, CV_64FC1, &val), CV_64F);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// meanStdDev
|
||||
|
||||
void cv::cuda::meanStdDev(InputArray src, OutputArray dst, Stream& stream)
|
||||
{
|
||||
if (!deviceSupports(FEATURE_SET_COMPUTE_13))
|
||||
CV_Error(cv::Error::StsNotImplemented, "Not sufficient compute capebility");
|
||||
|
||||
const GpuMat gsrc = getInputMat(src, stream);
|
||||
|
||||
#if (CUDA_VERSION <= 4020)
|
||||
CV_Assert( gsrc.type() == CV_8UC1 );
|
||||
#else
|
||||
CV_Assert( (gsrc.type() == CV_8UC1) || (gsrc.type() == CV_32FC1) );
|
||||
#endif
|
||||
|
||||
GpuMat gdst = getOutputMat(dst, 1, 2, CV_64FC1, stream);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = gsrc.cols;
|
||||
sz.height = gsrc.rows;
|
||||
|
||||
#if (NPP_VERSION >= 12205)
|
||||
size_t bufSize;
|
||||
#else
|
||||
int bufSize;
|
||||
#endif
|
||||
|
||||
NppStreamHandler h(StreamAccessor::getStream(stream));
|
||||
|
||||
#if (CUDA_VERSION <= 4020)
|
||||
nppSafeCall( nppiMeanStdDev8uC1RGetBufferHostSize(sz, &bufSize) );
|
||||
#else
|
||||
#if USE_NPP_STREAM_CTX
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall(nppiMeanStdDevGetBufferHostSize_8u_C1R_Ctx(sz, &bufSize, h));
|
||||
else
|
||||
nppSafeCall(nppiMeanStdDevGetBufferHostSize_32f_C1R_Ctx(sz, &bufSize, h));
|
||||
#else
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall( nppiMeanStdDevGetBufferHostSize_8u_C1R(sz, &bufSize) );
|
||||
else
|
||||
nppSafeCall( nppiMeanStdDevGetBufferHostSize_32f_C1R(sz, &bufSize) );
|
||||
#endif
|
||||
#endif
|
||||
|
||||
BufferPool pool(stream);
|
||||
CV_Assert(bufSize <= std::numeric_limits<int>::max());
|
||||
GpuMat buf = pool.getBuffer(1, static_cast<int>(bufSize), gsrc.type());
|
||||
#if USE_NPP_STREAM_CTX
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall(nppiMean_StdDev_8u_C1R_Ctx(gsrc.ptr<Npp8u>(), static_cast<int>(gsrc.step), sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1, h));
|
||||
else
|
||||
nppSafeCall(nppiMean_StdDev_32f_C1R_Ctx(gsrc.ptr<Npp32f>(), static_cast<int>(gsrc.step), sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1, h));
|
||||
#else
|
||||
if(gsrc.type() == CV_8UC1)
|
||||
nppSafeCall( nppiMean_StdDev_8u_C1R(gsrc.ptr<Npp8u>(), static_cast<int>(gsrc.step), sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1) );
|
||||
else
|
||||
nppSafeCall( nppiMean_StdDev_32f_C1R(gsrc.ptr<Npp32f>(), static_cast<int>(gsrc.step), sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1) );
|
||||
#endif
|
||||
|
||||
syncOutput(gdst, dst, stream);
|
||||
}
|
||||
|
||||
void cv::cuda::meanStdDev(InputArray src, Scalar& mean, Scalar& stddev)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
meanStdDev(src, dst, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double vals[2];
|
||||
dst.createMatHeader().copyTo(Mat(1, 2, CV_64FC1, &vals[0]));
|
||||
|
||||
mean = Scalar(vals[0]);
|
||||
stddev = Scalar(vals[1]);
|
||||
}
|
||||
|
||||
void cv::cuda::meanStdDev(InputArray _src, Scalar& mean, Scalar& stddev, InputArray _mask)
|
||||
{
|
||||
Stream& stream = Stream::Null();
|
||||
|
||||
HostMem dst;
|
||||
meanStdDev(_src, dst, _mask, stream);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
double vals[2];
|
||||
dst.createMatHeader().copyTo(Mat(1, 2, CV_64FC1, &vals[0]));
|
||||
|
||||
mean = Scalar(vals[0]);
|
||||
stddev = Scalar(vals[1]);
|
||||
}
|
||||
|
||||
void cv::cuda::meanStdDev(InputArray src, OutputArray dst, InputArray mask, Stream& stream)
|
||||
{
|
||||
if (!deviceSupports(FEATURE_SET_COMPUTE_13))
|
||||
CV_Error(cv::Error::StsNotImplemented, "Not sufficient compute capebility");
|
||||
|
||||
const GpuMat gsrc = getInputMat(src, stream);
|
||||
const GpuMat gmask = getInputMat(mask, stream);
|
||||
|
||||
#if (CUDA_VERSION <= 4020)
|
||||
CV_Assert( gsrc.type() == CV_8UC1 );
|
||||
#else
|
||||
CV_Assert( (gsrc.type() == CV_8UC1) || (gsrc.type() == CV_32FC1) );
|
||||
#endif
|
||||
|
||||
GpuMat gdst = getOutputMat(dst, 1, 2, CV_64FC1, stream);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = gsrc.cols;
|
||||
sz.height = gsrc.rows;
|
||||
|
||||
#if (NPP_VERSION >= 12205)
|
||||
size_t bufSize;
|
||||
#else
|
||||
int bufSize;
|
||||
#endif
|
||||
|
||||
NppStreamHandler h(StreamAccessor::getStream(stream));
|
||||
|
||||
#if (CUDA_VERSION <= 4020)
|
||||
nppSafeCall( nppiMeanStdDev8uC1MRGetBufferHostSize(sz, &bufSize) );
|
||||
#else
|
||||
#if USE_NPP_STREAM_CTX
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall(nppiMeanStdDevGetBufferHostSize_8u_C1MR_Ctx(sz, &bufSize, h));
|
||||
else
|
||||
nppSafeCall(nppiMeanStdDevGetBufferHostSize_32f_C1MR_Ctx(sz, &bufSize, h));
|
||||
#else
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall( nppiMeanStdDevGetBufferHostSize_8u_C1MR(sz, &bufSize) );
|
||||
else
|
||||
nppSafeCall( nppiMeanStdDevGetBufferHostSize_32f_C1MR(sz, &bufSize) );
|
||||
#endif
|
||||
#endif
|
||||
|
||||
BufferPool pool(stream);
|
||||
CV_Assert(bufSize <= std::numeric_limits<int>::max());
|
||||
GpuMat buf = pool.getBuffer(1, static_cast<int>(bufSize), gsrc.type());
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
if (gsrc.type() == CV_8UC1)
|
||||
nppSafeCall(nppiMean_StdDev_8u_C1MR_Ctx(gsrc.ptr<Npp8u>(), static_cast<int>(gsrc.step), gmask.ptr<Npp8u>(), static_cast<int>(gmask.step),
|
||||
sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1, h));
|
||||
else
|
||||
nppSafeCall(nppiMean_StdDev_32f_C1MR_Ctx(gsrc.ptr<Npp32f>(), static_cast<int>(gsrc.step), gmask.ptr<Npp8u>(), static_cast<int>(gmask.step),
|
||||
sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1, h));
|
||||
#else
|
||||
if(gsrc.type() == CV_8UC1)
|
||||
nppSafeCall( nppiMean_StdDev_8u_C1MR(gsrc.ptr<Npp8u>(), static_cast<int>(gsrc.step), gmask.ptr<Npp8u>(), static_cast<int>(gmask.step),
|
||||
sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1) );
|
||||
else
|
||||
nppSafeCall( nppiMean_StdDev_32f_C1MR(gsrc.ptr<Npp32f>(), static_cast<int>(gsrc.step), gmask.ptr<Npp8u>(), static_cast<int>(gmask.step),
|
||||
sz, buf.ptr<Npp8u>(), gdst.ptr<Npp64f>(), gdst.ptr<Npp64f>() + 1) );
|
||||
#endif
|
||||
|
||||
syncOutput(gdst, dst, stream);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// rectStdDev
|
||||
|
||||
void cv::cuda::rectStdDev(InputArray _src, InputArray _sqr, OutputArray _dst, Rect rect, Stream& _stream)
|
||||
{
|
||||
GpuMat src = getInputMat(_src, _stream);
|
||||
GpuMat sqr = getInputMat(_sqr, _stream);
|
||||
|
||||
CV_Assert( src.type() == CV_32SC1 && sqr.type() == CV_64FC1 );
|
||||
|
||||
GpuMat dst = getOutputMat(_dst, src.size(), CV_32FC1, _stream);
|
||||
|
||||
NppiSize sz;
|
||||
sz.width = src.cols;
|
||||
sz.height = src.rows;
|
||||
|
||||
NppiRect nppRect;
|
||||
nppRect.height = rect.height;
|
||||
nppRect.width = rect.width;
|
||||
nppRect.x = rect.x;
|
||||
nppRect.y = rect.y;
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(_stream);
|
||||
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
#if USE_NPP_STREAM_CTX
|
||||
nppSafeCall(nppiRectStdDev_32s32f_C1R_Ctx(src.ptr<Npp32s>(), static_cast<int>(src.step), sqr.ptr<Npp64f>(), static_cast<int>(sqr.step),
|
||||
dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz, nppRect, h));
|
||||
#else
|
||||
nppSafeCall( nppiRectStdDev_32s32f_C1R(src.ptr<Npp32s>(), static_cast<int>(src.step), sqr.ptr<Npp64f>(), static_cast<int>(sqr.step),
|
||||
dst.ptr<Npp32f>(), static_cast<int>(dst.step), sz, nppRect) );
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
|
||||
syncOutput(dst, _dst, _stream);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user