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_CUDAARITHM)
49
createGoodFeaturesToTrackDetector(int,int,double,double,int,bool,double)50 Ptr<cuda::CornersDetector> cv::cuda::createGoodFeaturesToTrackDetector(int, int, double, double, int, bool, double) { throw_no_cuda(); return Ptr<cuda::CornersDetector>(); }
51
52 #else /* !defined (HAVE_CUDA) */
53
54 namespace cv { namespace cuda { namespace device
55 {
56 namespace gfft
57 {
58 int findCorners_gpu(const cudaTextureObject_t &eigTex_, const int &rows, const int &cols, float threshold, PtrStepSzb mask, float2* corners, int max_count, cudaStream_t stream);
59 void sortCorners_gpu(const cudaTextureObject_t &eigTex_, float2* corners, int count, cudaStream_t stream);
60 }
61 }}}
62
63 namespace
64 {
65 class GoodFeaturesToTrackDetector : public CornersDetector
66 {
67 public:
68 GoodFeaturesToTrackDetector(int srcType, int maxCorners, double qualityLevel, double minDistance,
69 int blockSize, bool useHarrisDetector, double harrisK);
70
71 void detect(InputArray image, OutputArray corners, InputArray mask, Stream& stream);
72
73 private:
74 int maxCorners_;
75 double qualityLevel_;
76 double minDistance_;
77
78 Ptr<cuda::CornernessCriteria> cornerCriteria_;
79
80 GpuMat Dx_;
81 GpuMat Dy_;
82 GpuMat buf_;
83 GpuMat eig_;
84 GpuMat tmpCorners_;
85 };
86
GoodFeaturesToTrackDetector(int srcType,int maxCorners,double qualityLevel,double minDistance,int blockSize,bool useHarrisDetector,double harrisK)87 GoodFeaturesToTrackDetector::GoodFeaturesToTrackDetector(int srcType, int maxCorners, double qualityLevel, double minDistance,
88 int blockSize, bool useHarrisDetector, double harrisK) :
89 maxCorners_(maxCorners), qualityLevel_(qualityLevel), minDistance_(minDistance)
90 {
91 CV_Assert( qualityLevel_ > 0 && minDistance_ >= 0 && maxCorners_ >= 0 );
92
93 cornerCriteria_ = useHarrisDetector ?
94 cuda::createHarrisCorner(srcType, blockSize, 3, harrisK) :
95 cuda::createMinEigenValCorner(srcType, blockSize, 3);
96 }
97
detect(InputArray _image,OutputArray _corners,InputArray _mask,Stream & stream)98 void GoodFeaturesToTrackDetector::detect(InputArray _image, OutputArray _corners, InputArray _mask, Stream& stream)
99 {
100 using namespace cv::cuda::device::gfft;
101
102 GpuMat image = _image.getGpuMat();
103 GpuMat mask = _mask.getGpuMat();
104
105 CV_Assert( mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size()) );
106
107 ensureSizeIsEnough(image.size(), CV_32FC1, eig_);
108 cornerCriteria_->compute(image, eig_, stream);
109
110 double maxVal = 0;
111 cuda::minMax(eig_, 0, &maxVal);
112 cudaStream_t stream_ = StreamAccessor::getStream(stream);
113 ensureSizeIsEnough(1, std::max(1000, static_cast<int>(image.size().area() * 0.05)), CV_32FC2, tmpCorners_);
114
115 //create texture object for findCorners_gpu and sortCorners_gpu
116 cudaTextureDesc texDesc;
117 memset(&texDesc, 0, sizeof(texDesc));
118 texDesc.readMode = cudaReadModeElementType;
119 texDesc.filterMode = cudaFilterModePoint;
120 texDesc.addressMode[0] = cudaAddressModeClamp;
121 texDesc.addressMode[1] = cudaAddressModeClamp;
122 texDesc.addressMode[2] = cudaAddressModeClamp;
123
124 cudaTextureObject_t eigTex_;
125 PtrStepSzf eig = eig_;
126 cv::cuda::device::createTextureObjectPitch2D<float>(&eigTex_, eig, texDesc);
127
128 int total = findCorners_gpu(eigTex_, eig_.rows, eig_.cols, static_cast<float>(maxVal * qualityLevel_), mask, tmpCorners_.ptr<float2>(), tmpCorners_.cols, stream_);
129
130
131 if (total == 0)
132 {
133 _corners.release();
134 return;
135 }
136
137 sortCorners_gpu(eigTex_, tmpCorners_.ptr<float2>(), total, stream_);
138
139 if (minDistance_ < 1)
140 {
141 tmpCorners_.colRange(0, maxCorners_ > 0 ? std::min(maxCorners_, total) : total).copyTo(_corners, stream);
142 }
143 else
144 {
145 std::vector<Point2f> tmp(total);
146 Mat tmpMat(1, total, CV_32FC2, (void*)&tmp[0]);
147 tmpCorners_.colRange(0, total).download(tmpMat, stream);
148 stream.waitForCompletion();
149 std::vector<Point2f> tmp2;
150 tmp2.reserve(total);
151
152 const int cell_size = cvRound(minDistance_);
153 const int grid_width = (image.cols + cell_size - 1) / cell_size;
154 const int grid_height = (image.rows + cell_size - 1) / cell_size;
155
156 std::vector< std::vector<Point2f> > grid(grid_width * grid_height);
157
158 for (int i = 0; i < total; ++i)
159 {
160 Point2f p = tmp[i];
161
162 bool good = true;
163
164 int x_cell = static_cast<int>(p.x / cell_size);
165 int y_cell = static_cast<int>(p.y / cell_size);
166
167 int x1 = x_cell - 1;
168 int y1 = y_cell - 1;
169 int x2 = x_cell + 1;
170 int y2 = y_cell + 1;
171
172 // boundary check
173 x1 = std::max(0, x1);
174 y1 = std::max(0, y1);
175 x2 = std::min(grid_width - 1, x2);
176 y2 = std::min(grid_height - 1, y2);
177
178 for (int yy = y1; yy <= y2; yy++)
179 {
180 for (int xx = x1; xx <= x2; xx++)
181 {
182 std::vector<Point2f>& m = grid[yy * grid_width + xx];
183
184 if (!m.empty())
185 {
186 for(size_t j = 0; j < m.size(); j++)
187 {
188 float dx = p.x - m[j].x;
189 float dy = p.y - m[j].y;
190
191 if (dx * dx + dy * dy < minDistance_ * minDistance_)
192 {
193 good = false;
194 goto break_out;
195 }
196 }
197 }
198 }
199 }
200
201 break_out:
202
203 if(good)
204 {
205 grid[y_cell * grid_width + x_cell].push_back(p);
206
207 tmp2.push_back(p);
208
209 if (maxCorners_ > 0 && tmp2.size() == static_cast<size_t>(maxCorners_))
210 break;
211 }
212 }
213
214 _corners.create(1, static_cast<int>(tmp2.size()), CV_32FC2);
215 GpuMat corners = _corners.getGpuMat();
216
217 corners.upload(Mat(1, static_cast<int>(tmp2.size()), CV_32FC2, &tmp2[0]), stream);
218 }
219 }
220 }
221
createGoodFeaturesToTrackDetector(int srcType,int maxCorners,double qualityLevel,double minDistance,int blockSize,bool useHarrisDetector,double harrisK)222 Ptr<cuda::CornersDetector> cv::cuda::createGoodFeaturesToTrackDetector(int srcType, int maxCorners, double qualityLevel, double minDistance,
223 int blockSize, bool useHarrisDetector, double harrisK)
224 {
225 return Ptr<cuda::CornersDetector>(
226 new GoodFeaturesToTrackDetector(srcType, maxCorners, qualityLevel, minDistance, blockSize, useHarrisDetector, harrisK));
227 }
228
229 #endif /* !defined (HAVE_CUDA) */
230