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 "precomp.hpp"
44
45 using namespace cv;
46 using namespace cv::cuda;
47
48 #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || !defined(HAVE_OPENCV_CUDAFILTERS)
49
createHarrisCorner(int,int,int,double,int)50 Ptr<cuda::CornernessCriteria> cv::cuda::createHarrisCorner(int, int, int, double, int) { throw_no_cuda(); return Ptr<cuda::CornernessCriteria>(); }
createMinEigenValCorner(int,int,int,int)51 Ptr<cuda::CornernessCriteria> cv::cuda::createMinEigenValCorner(int, int, int, int) { throw_no_cuda(); return Ptr<cuda::CornernessCriteria>(); }
52
53 #else /* !defined (HAVE_CUDA) */
54
55 namespace cv { namespace cuda { namespace device
56 {
57 namespace imgproc
58 {
59 void cornerHarris_gpu(int block_size, float k, PtrStepSzf Dx, PtrStepSzf Dy, PtrStepSzf dst, int border_type, cudaStream_t stream);
60 void cornerMinEigenVal_gpu(int block_size, PtrStepSzf Dx, PtrStepSzf Dy, PtrStepSzf dst, int border_type, cudaStream_t stream);
61 }
62 }}}
63
64 namespace
65 {
66 class CornerBase : public CornernessCriteria
67 {
68 protected:
69 CornerBase(int srcType, int blockSize, int ksize, int borderType);
70
71 void extractCovData(const GpuMat& src, Stream& stream);
72
73 int srcType_;
74 int blockSize_;
75 int ksize_;
76 int borderType_;
77 GpuMat Dx_, Dy_;
78
79 private:
80 Ptr<cuda::Filter> filterDx_, filterDy_;
81 };
82
CornerBase(int srcType,int blockSize,int ksize,int borderType)83 CornerBase::CornerBase(int srcType, int blockSize, int ksize, int borderType) :
84 srcType_(srcType), blockSize_(blockSize), ksize_(ksize), borderType_(borderType)
85 {
86 CV_Assert( borderType_ == BORDER_REFLECT101 || borderType_ == BORDER_REPLICATE || borderType_ == BORDER_REFLECT );
87
88 const int sdepth = CV_MAT_DEPTH(srcType_);
89 const int cn = CV_MAT_CN(srcType_);
90
91 CV_Assert( cn == 1 );
92
93 double scale = static_cast<double>(1 << ((ksize_ > 0 ? ksize_ : 3) - 1)) * blockSize_;
94
95 if (ksize_ < 0)
96 scale *= 2.;
97
98 if (sdepth == CV_8U)
99 scale *= 255.;
100
101 scale = 1./scale;
102
103 if (ksize_ > 0)
104 {
105 filterDx_ = cuda::createSobelFilter(srcType, CV_32F, 1, 0, ksize_, scale, borderType_);
106 filterDy_ = cuda::createSobelFilter(srcType, CV_32F, 0, 1, ksize_, scale, borderType_);
107 }
108 else
109 {
110 filterDx_ = cuda::createScharrFilter(srcType, CV_32F, 1, 0, scale, borderType_);
111 filterDy_ = cuda::createScharrFilter(srcType, CV_32F, 0, 1, scale, borderType_);
112 }
113 }
114
extractCovData(const GpuMat & src,Stream & stream)115 void CornerBase::extractCovData(const GpuMat& src, Stream& stream)
116 {
117 CV_Assert( src.type() == srcType_ );
118 filterDx_->apply(src, Dx_, stream);
119 filterDy_->apply(src, Dy_, stream);
120 }
121
122 class Harris : public CornerBase
123 {
124 public:
Harris(int srcType,int blockSize,int ksize,double k,int borderType)125 Harris(int srcType, int blockSize, int ksize, double k, int borderType) :
126 CornerBase(srcType, blockSize, ksize, borderType), k_(static_cast<float>(k))
127 {
128 }
129
130 void compute(InputArray src, OutputArray dst, Stream& stream = Stream::Null());
131
132 private:
133 float k_;
134 };
135
compute(InputArray _src,OutputArray _dst,Stream & stream)136 void Harris::compute(InputArray _src, OutputArray _dst, Stream& stream)
137 {
138 using namespace cv::cuda::device::imgproc;
139
140 GpuMat src = _src.getGpuMat();
141
142 extractCovData(src, stream);
143
144 _dst.create(src.size(), CV_32FC1);
145 GpuMat dst = _dst.getGpuMat();
146
147 cornerHarris_gpu(blockSize_, k_, Dx_, Dy_, dst, borderType_, StreamAccessor::getStream(stream));
148 }
149
150 class MinEigenVal : public CornerBase
151 {
152 public:
MinEigenVal(int srcType,int blockSize,int ksize,int borderType)153 MinEigenVal(int srcType, int blockSize, int ksize, int borderType) :
154 CornerBase(srcType, blockSize, ksize, borderType)
155 {
156 }
157
158 void compute(InputArray src, OutputArray dst, Stream& stream = Stream::Null());
159
160 private:
161 float k_;
162 };
163
compute(InputArray _src,OutputArray _dst,Stream & stream)164 void MinEigenVal::compute(InputArray _src, OutputArray _dst, Stream& stream)
165 {
166 using namespace cv::cuda::device::imgproc;
167
168 GpuMat src = _src.getGpuMat();
169
170 extractCovData(src, stream);
171
172 _dst.create(src.size(), CV_32FC1);
173 GpuMat dst = _dst.getGpuMat();
174
175 cornerMinEigenVal_gpu(blockSize_, Dx_, Dy_, dst, borderType_, StreamAccessor::getStream(stream));
176 }
177 }
178
createHarrisCorner(int srcType,int blockSize,int ksize,double k,int borderType)179 Ptr<cuda::CornernessCriteria> cv::cuda::createHarrisCorner(int srcType, int blockSize, int ksize, double k, int borderType)
180 {
181 return makePtr<Harris>(srcType, blockSize, ksize, k, borderType);
182 }
183
createMinEigenValCorner(int srcType,int blockSize,int ksize,int borderType)184 Ptr<cuda::CornernessCriteria> cv::cuda::createMinEigenValCorner(int srcType, int blockSize, int ksize, int borderType)
185 {
186 return makePtr<MinEigenVal>(srcType, blockSize, ksize, borderType);
187 }
188
189 #endif /* !defined (HAVE_CUDA) */
190