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)
49
createImagePyramid(InputArray,int,Stream &)50 Ptr<ImagePyramid> cv::cuda::createImagePyramid(InputArray, int, Stream&) { throw_no_cuda(); return Ptr<ImagePyramid>(); }
51
52 #else // HAVE_CUDA
53
54 namespace
55 {
56 class ImagePyramidImpl : public ImagePyramid
57 {
58 public:
59 ImagePyramidImpl(InputArray img, int nLayers, Stream& stream);
60
61 void getLayer(OutputArray outImg, Size outRoi, Stream& stream = Stream::Null()) const;
62
63 private:
64 GpuMat layer0_;
65 std::vector<GpuMat> pyramid_;
66 int nLayers_;
67 };
68
ImagePyramidImpl(InputArray _img,int numLayers,Stream & stream)69 ImagePyramidImpl::ImagePyramidImpl(InputArray _img, int numLayers, Stream& stream)
70 {
71 GpuMat img = _img.getGpuMat();
72
73 CV_Assert( img.depth() <= CV_32F && img.channels() <= 4 );
74
75 img.copyTo(layer0_, stream);
76
77 Size szLastLayer = img.size();
78 nLayers_ = 1;
79
80 if (numLayers <= 0)
81 numLayers = 255; // it will cut-off when any of the dimensions goes 1
82
83 pyramid_.resize(numLayers);
84
85 for (int i = 0; i < numLayers - 1; ++i)
86 {
87 Size szCurLayer(szLastLayer.width / 2, szLastLayer.height / 2);
88
89 if (szCurLayer.width == 0 || szCurLayer.height == 0)
90 break;
91
92 ensureSizeIsEnough(szCurLayer, img.type(), pyramid_[i]);
93 nLayers_++;
94
95 const GpuMat& prevLayer = i == 0 ? layer0_ : pyramid_[i - 1];
96
97 cv::cuda::device::pyramid::downsampleX2(prevLayer, pyramid_[i], img.depth(), img.channels(), StreamAccessor::getStream(stream));
98
99 szLastLayer = szCurLayer;
100 }
101 }
102
getLayer(OutputArray _outImg,Size outRoi,Stream & stream) const103 void ImagePyramidImpl::getLayer(OutputArray _outImg, Size outRoi, Stream& stream) const
104 {
105 CV_Assert( outRoi.width <= layer0_.cols && outRoi.height <= layer0_.rows && outRoi.width > 0 && outRoi.height > 0 );
106
107 ensureSizeIsEnough(outRoi, layer0_.type(), _outImg);
108 GpuMat outImg = _outImg.getGpuMat();
109
110 if (outRoi.width == layer0_.cols && outRoi.height == layer0_.rows)
111 {
112 layer0_.copyTo(outImg, stream);
113 return;
114 }
115
116 float lastScale = 1.0f;
117 float curScale;
118 GpuMat lastLayer = layer0_;
119 GpuMat curLayer;
120
121 for (int i = 0; i < nLayers_ - 1; ++i)
122 {
123 curScale = lastScale * 0.5f;
124 curLayer = pyramid_[i];
125
126 if (outRoi.width == curLayer.cols && outRoi.height == curLayer.rows)
127 {
128 curLayer.copyTo(outImg, stream);
129 }
130
131 if (outRoi.width >= curLayer.cols && outRoi.height >= curLayer.rows)
132 break;
133
134 lastScale = curScale;
135 lastLayer = curLayer;
136 }
137
138 cv::cuda::device::pyramid::interpolateFrom1(lastLayer, outImg, outImg.depth(), outImg.channels(), StreamAccessor::getStream(stream));
139 }
140 }
141
createImagePyramid(InputArray img,int nLayers,Stream & stream)142 Ptr<ImagePyramid> cv::cuda::createImagePyramid(InputArray img, int nLayers, Stream& stream)
143 {
144 return Ptr<ImagePyramid>(new ImagePyramidImpl(img, nLayers, stream));
145 }
146
147 #endif
148