~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/cudaarithm/src/cuda/norm.cu

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*M///////////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 
4
//
 
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.
 
8
//
 
9
//
 
10
//                           License Agreement
 
11
//                For Open Source Computer Vision Library
 
12
//
 
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.
 
16
//
 
17
// Redistribution and use in source and binary forms, with or without modification,
 
18
// are permitted provided that the following conditions are met:
 
19
//
 
20
//   * Redistribution's of source code must retain the above copyright notice,
 
21
//     this list of conditions and the following disclaimer.
 
22
//
 
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.
 
26
//
 
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.
 
29
//
 
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.
 
40
//
 
41
//M*/
 
42
 
 
43
#include "opencv2/opencv_modules.hpp"
 
44
 
 
45
#ifndef HAVE_OPENCV_CUDEV
 
46
 
 
47
#error "opencv_cudev is required"
 
48
 
 
49
#else
 
50
 
 
51
#include "opencv2/cudaarithm.hpp"
 
52
#include "opencv2/cudev.hpp"
 
53
#include "opencv2/core/private.cuda.hpp"
 
54
 
 
55
using namespace cv;
 
56
using namespace cv::cuda;
 
57
using namespace cv::cudev;
 
58
 
 
59
namespace
 
60
{
 
61
    void normDiffInf(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
 
62
    {
 
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;
 
66
 
 
67
        gridFindMaxVal(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
 
68
    }
 
69
 
 
70
    void normDiffL1(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
 
71
    {
 
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;
 
75
 
 
76
        gridCalcSum(abs_(cvt_<int>(src1) - cvt_<int>(src2)), dst, stream);
 
77
    }
 
78
 
 
79
    void normDiffL2(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream)
 
80
    {
 
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;
 
84
 
 
85
        BufferPool pool(stream);
 
86
        GpuMat_<double> buf(1, 1, pool.getAllocator());
 
87
 
 
88
        gridCalcSum(sqr_(cvt_<double>(src1) - cvt_<double>(src2)), buf, stream);
 
89
        gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
 
90
    }
 
91
}
 
92
 
 
93
void cv::cuda::calcNormDiff(InputArray _src1, InputArray _src2, OutputArray _dst, int normType, Stream& stream)
 
94
{
 
95
    typedef void (*func_t)(const GpuMat& _src1, const GpuMat& _src2, GpuMat& _dst, Stream& stream);
 
96
    static const func_t funcs[] =
 
97
    {
 
98
        0, normDiffInf, normDiffL1, 0, normDiffL2
 
99
    };
 
100
 
 
101
    GpuMat src1 = getInputMat(_src1, stream);
 
102
    GpuMat src2 = getInputMat(_src2, stream);
 
103
 
 
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 );
 
107
 
 
108
    GpuMat dst = getOutputMat(_dst, 1, 1, normType == NORM_L2 ? CV_64FC1 : CV_32SC1, stream);
 
109
 
 
110
    const func_t func = funcs[normType];
 
111
    func(src1, src2, dst, stream);
 
112
 
 
113
    syncOutput(dst, _dst, stream);
 
114
}
 
115
 
 
116
double cv::cuda::norm(InputArray _src1, InputArray _src2, int normType)
 
117
{
 
118
    Stream& stream = Stream::Null();
 
119
 
 
120
    HostMem dst;
 
121
    calcNormDiff(_src1, _src2, dst, normType, stream);
 
122
 
 
123
    stream.waitForCompletion();
 
124
 
 
125
    double val;
 
126
    dst.createMatHeader().convertTo(Mat(1, 1, CV_64FC1, &val), CV_64F);
 
127
 
 
128
    return val;
 
129
}
 
130
 
 
131
namespace cv { namespace cuda { namespace device {
 
132
 
 
133
void normL2(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _mask, Stream& stream);
 
134
 
 
135
}}}
 
136
 
 
137
namespace
 
138
{
 
139
    template <typename T, typename R>
 
140
    void normL2Impl(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream)
 
141
    {
 
142
        const GpuMat_<T>& src = (const GpuMat_<T>&) _src;
 
143
        GpuMat_<R>& dst = (GpuMat_<R>&) _dst;
 
144
 
 
145
        BufferPool pool(stream);
 
146
        GpuMat_<double> buf(1, 1, pool.getAllocator());
 
147
 
 
148
        if (mask.empty())
 
149
        {
 
150
            gridCalcSum(sqr_(cvt_<double>(src)), buf, stream);
 
151
        }
 
152
        else
 
153
        {
 
154
            gridCalcSum(sqr_(cvt_<double>(src)), buf, globPtr<uchar>(mask), stream);
 
155
        }
 
156
 
 
157
        gridTransformUnary(buf, dst, sqrt_func<double>(), stream);
 
158
    }
 
159
}
 
160
 
 
161
void cv::cuda::device::normL2(InputArray _src, OutputArray _dst, InputArray _mask, Stream& stream)
 
162
{
 
163
    typedef void (*func_t)(const GpuMat& _src, const GpuMat& mask, GpuMat& _dst, Stream& stream);
 
164
    static const func_t funcs[] =
 
165
    {
 
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>
 
173
    };
 
174
 
 
175
    const GpuMat src = getInputMat(_src, stream);
 
176
    const GpuMat mask = getInputMat(_mask, stream);
 
177
 
 
178
    CV_Assert( src.channels() == 1 );
 
179
    CV_Assert( mask.empty() || (mask.size() == src.size() && mask.type() == CV_8U) );
 
180
 
 
181
    GpuMat dst = getOutputMat(_dst, 1, 1, CV_64FC1, stream);
 
182
 
 
183
    const func_t func = funcs[src.depth()];
 
184
    func(src, mask, dst, stream);
 
185
 
 
186
    syncOutput(dst, _dst, stream);
 
187
}
 
188
 
 
189
#endif