1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "opencv2/opencv_modules.hpp"
45
#ifndef HAVE_OPENCV_CUDEV
47
#error "opencv_cudev is required"
51
#include "opencv2/cudaarithm.hpp"
52
#include "opencv2/cudev.hpp"
53
#include "opencv2/core/private.cuda.hpp"
56
using namespace cv::cuda;
57
using namespace cv::cudev;
61
void normDiffInf(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
63
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
64
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
65
GpuMat_<int>& dst = (GpuMat_<int>&) _dst;
67
gridFindMaxVal(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
70
void normDiffL1(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
72
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
73
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
74
GpuMat_<int>& dst = (GpuMat_<int>&) _dst;
76
gridCalcSum(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
79
void normDiffL2(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
81
const GpuMat_<uchar>& src1 = (const GpuMat_<uchar>&) _src1;
82
const GpuMat_<uchar>& src2 = (const GpuMat_<uchar>&) _src2;
83
GpuMat_<double>& dst = (GpuMat_<double>&) _dst;
85
BufferPool pool(stream);
86
GpuMat_<double> buf(1, 1, pool.getAllocator());
88
gridCalcSum(sqr_(cvt_<double>(src1) - cvt_<double>(src2)), buf, stream);
89
gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
93
void cv::cuda::calcNormDiff(InputArray _src1, InputArray _src2, OutputArray _dst, int normType, Stream& stream)
95
typedef void (*func_t)(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream);
96
static const func_t funcs[] =
98
0, normDiffInf, normDiffL1, 0, normDiffL2
101
GpuMat src1 = getInputMat(_src1, stream);
102
GpuMat src2 = getInputMat(_src2, stream);
104
CV_Assert( src1.type() == CV_8UC1 );
105
CV_Assert( src1.size() == src2.size() && src1.type() == src2.type() );
106
CV_Assert( normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2 );
108
GpuMat dst = getOutputMat(_dst, 1, 1, normType == NORM_L2 ? CV_64FC1 : CV_32SC1, stream);
110
const func_t func = funcs[normType];
111
func(src1, src2, dst, stream);
113
syncOutput(dst, _dst, stream);
116
double cv::cuda::norm(InputArray _src1, InputArray _src2, int normType)
118
Stream& stream = Stream::Null();
121
calcNormDiff(_src1, _src2, dst, normType, stream);
123
stream.waitForCompletion();
126
dst.createMatHeader().convertTo(Mat(1, 1, CV_64FC1, &val), CV_64F);
131
namespace cv { namespace cuda { namespace device {
133
void normL2(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _mask, Stream& stream);
139
template <typename T, typename R>
140
void normL2Impl(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream)
142
const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
143
GpuMat_<R>& dst = (GpuMat_<R>&) _dst;
145
BufferPool pool(stream);
146
GpuMat_<double> buf(1, 1, pool.getAllocator());
150
gridCalcSum(sqr_(cvt_<double>(src)), buf, stream);
154
gridCalcSum(sqr_(cvt_<double>(src)), buf, globPtr<uchar>(mask), stream);
157
gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
161
void cv::cuda::device::normL2(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
163
typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream);
164
static const func_t funcs[] =
166
normL2Impl<uchar, double>,
167
normL2Impl<schar, double>,
168
normL2Impl<ushort, double>,
169
normL2Impl<short, double>,
170
normL2Impl<int, double>,
171
normL2Impl<float, double>,
172
normL2Impl<double, double>
175
const GpuMat src = getInputMat(_src, stream);
176
const GpuMat mask = getInputMat(_mask, stream);
178
CV_Assert( src.channels() == 1 );
179
CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
181
GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC1, stream);
183
const func_t func = funcs[src.depth()];
184
func(src, mask, dst, stream);
186
syncOutput(dst, _dst, stream);