Commit 6d6ff268 authored by Vladislav Vinogradov's avatar Vladislav Vinogradov

refactored gpu::ImagePyramid (converted it to abstract interface)

parent 2932bedd
...@@ -100,32 +100,14 @@ CV_EXPORTS void pyrDown(InputArray src, OutputArray dst, Stream& stream = Stream ...@@ -100,32 +100,14 @@ CV_EXPORTS void pyrDown(InputArray src, OutputArray dst, Stream& stream = Stream
//! upsamples the source image and then smoothes it //! upsamples the source image and then smoothes it
CV_EXPORTS void pyrUp(InputArray src, OutputArray dst, Stream& stream = Stream::Null()); CV_EXPORTS void pyrUp(InputArray src, OutputArray dst, Stream& stream = Stream::Null());
class CV_EXPORTS ImagePyramid class CV_EXPORTS ImagePyramid : public Algorithm
{ {
public: public:
inline ImagePyramid() : nLayers_(0) {} virtual void getLayer(OutputArray outImg, Size outRoi, Stream& stream = Stream::Null()) const = 0;
inline ImagePyramid(const GpuMat& img, int nLayers, Stream& stream = Stream::Null())
{
build(img, nLayers, stream);
}
void build(const GpuMat& img, int nLayers, Stream& stream = Stream::Null());
void getLayer(GpuMat& outImg, Size outRoi, Stream& stream = Stream::Null()) const;
inline void release()
{
layer0_.release();
pyramid_.clear();
nLayers_ = 0;
}
private:
GpuMat layer0_;
std::vector<GpuMat> pyramid_;
int nLayers_;
}; };
CV_EXPORTS Ptr<ImagePyramid> createImagePyramid(InputArray img, int nLayers = -1, Stream& stream = Stream::Null());
}} // namespace cv { namespace gpu { }} // namespace cv { namespace gpu {
#endif /* __OPENCV_GPUWARPING_HPP__ */ #endif /* __OPENCV_GPUWARPING_HPP__ */
...@@ -515,45 +515,6 @@ PERF_TEST_P(Sz_Depth_Cn, PyrUp, ...@@ -515,45 +515,6 @@ PERF_TEST_P(Sz_Depth_Cn, PyrUp,
} }
} }
//////////////////////////////////////////////////////////////////////
// ImagePyramidBuild
PERF_TEST_P(Sz_Depth_Cn, ImagePyramidBuild,
Combine(GPU_TYPICAL_MAT_SIZES,
Values(CV_8U, CV_16U, CV_32F),
GPU_CHANNELS_1_3_4))
{
const cv::Size size = GET_PARAM(0);
const int depth = GET_PARAM(1);
const int channels = GET_PARAM(2);
const int type = CV_MAKE_TYPE(depth, channels);
cv::Mat src(size, type);
declare.in(src, WARMUP_RNG);
const int nLayers = 5;
const cv::Size dstSize(size.width / 2 + 10, size.height / 2 + 10);
if (PERF_RUN_GPU())
{
const cv::gpu::GpuMat d_src(src);
cv::gpu::ImagePyramid d_pyr;
TEST_CYCLE() d_pyr.build(d_src, nLayers);
cv::gpu::GpuMat dst;
d_pyr.getLayer(dst, dstSize);
GPU_SANITY_CHECK(dst);
}
else
{
FAIL_NO_CPU();
}
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// ImagePyramidGetLayer // ImagePyramidGetLayer
...@@ -579,9 +540,9 @@ PERF_TEST_P(Sz_Depth_Cn, ImagePyramidGetLayer, ...@@ -579,9 +540,9 @@ PERF_TEST_P(Sz_Depth_Cn, ImagePyramidGetLayer,
const cv::gpu::GpuMat d_src(src); const cv::gpu::GpuMat d_src(src);
cv::gpu::GpuMat dst; cv::gpu::GpuMat dst;
cv::gpu::ImagePyramid d_pyr(d_src, nLayers); cv::Ptr<cv::gpu::ImagePyramid> d_pyr = cv::gpu::createImagePyramid(d_src, nLayers);
TEST_CYCLE() d_pyr.getLayer(dst, dstSize); TEST_CYCLE() d_pyr->getLayer(dst, dstSize);
GPU_SANITY_CHECK(dst); GPU_SANITY_CHECK(dst);
} }
......
...@@ -42,13 +42,15 @@ ...@@ -42,13 +42,15 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace cv;
using namespace cv::gpu;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) #if !defined HAVE_CUDA || defined(CUDA_DISABLER)
void cv::gpu::pyrDown(InputArray, OutputArray, Stream&) { throw_no_cuda(); } void cv::gpu::pyrDown(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
void cv::gpu::pyrUp(InputArray, OutputArray, Stream&) { throw_no_cuda(); } void cv::gpu::pyrUp(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
void cv::gpu::ImagePyramid::build(const GpuMat&, int, Stream&) { throw_no_cuda(); } Ptr<ImagePyramid> cv::gpu::createImagePyramid(InputArray, int, Stream&) { throw_no_cuda(); return Ptr<ImagePyramid>(); }
void cv::gpu::ImagePyramid::getLayer(GpuMat&, Size, Stream&) const { throw_no_cuda(); }
#else // HAVE_CUDA #else // HAVE_CUDA
...@@ -134,84 +136,108 @@ void cv::gpu::pyrUp(InputArray _src, OutputArray _dst, Stream& stream) ...@@ -134,84 +136,108 @@ void cv::gpu::pyrUp(InputArray _src, OutputArray _dst, Stream& stream)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// ImagePyramid // ImagePyramid
void cv::gpu::ImagePyramid::build(const GpuMat& img, int numLayers, Stream& stream) #ifdef HAVE_OPENCV_GPULEGACY
{
#ifndef HAVE_OPENCV_GPULEGACY
(void) img;
(void) numLayers;
(void) stream;
throw_no_cuda();
#else
CV_Assert(img.depth() <= CV_32F && img.channels() <= 4);
layer0_ = img; namespace
Size szLastLayer = img.size(); {
nLayers_ = 1; class ImagePyramidImpl : public ImagePyramid
{
public:
ImagePyramidImpl(InputArray img, int nLayers, Stream& stream);
if (numLayers <= 0) void getLayer(OutputArray outImg, Size outRoi, Stream& stream = Stream::Null()) const;
numLayers = 255; //it will cut-off when any of the dimensions goes 1
pyramid_.resize(numLayers); private:
GpuMat layer0_;
std::vector<GpuMat> pyramid_;
int nLayers_;
};
for (int i = 0; i < numLayers - 1; ++i) ImagePyramidImpl::ImagePyramidImpl(InputArray _img, int numLayers, Stream& stream)
{ {
Size szCurLayer(szLastLayer.width / 2, szLastLayer.height / 2); GpuMat img = _img.getGpuMat();
if (szCurLayer.width == 0 || szCurLayer.height == 0) CV_Assert( img.depth() <= CV_32F && img.channels() <= 4 );
break;
ensureSizeIsEnough(szCurLayer, img.type(), pyramid_[i]); img.copyTo(layer0_, stream);
nLayers_++;
const GpuMat& prevLayer = i == 0 ? layer0_ : pyramid_[i - 1]; Size szLastLayer = img.size();
nLayers_ = 1;
cudev::pyramid::downsampleX2(prevLayer, pyramid_[i], img.depth(), img.channels(), StreamAccessor::getStream(stream)); if (numLayers <= 0)
numLayers = 255; // it will cut-off when any of the dimensions goes 1
szLastLayer = szCurLayer; pyramid_.resize(numLayers);
}
#endif
}
void cv::gpu::ImagePyramid::getLayer(GpuMat& outImg, Size outRoi, Stream& stream) const for (int i = 0; i < numLayers - 1; ++i)
{ {
#ifndef HAVE_OPENCV_GPULEGACY Size szCurLayer(szLastLayer.width / 2, szLastLayer.height / 2);
(void) outImg;
(void) outRoi;
(void) stream;
throw_no_cuda();
#else
CV_Assert(outRoi.width <= layer0_.cols && outRoi.height <= layer0_.rows && outRoi.width > 0 && outRoi.height > 0);
ensureSizeIsEnough(outRoi, layer0_.type(), outImg); if (szCurLayer.width == 0 || szCurLayer.height == 0)
break;
if (outRoi.width == layer0_.cols && outRoi.height == layer0_.rows) ensureSizeIsEnough(szCurLayer, img.type(), pyramid_[i]);
{ nLayers_++;
layer0_.copyTo(outImg, stream);
}
float lastScale = 1.0f; const GpuMat& prevLayer = i == 0 ? layer0_ : pyramid_[i - 1];
float curScale;
GpuMat lastLayer = layer0_;
GpuMat curLayer;
for (int i = 0; i < nLayers_ - 1; ++i) cudev::pyramid::downsampleX2(prevLayer, pyramid_[i], img.depth(), img.channels(), StreamAccessor::getStream(stream));
szLastLayer = szCurLayer;
}
}
void ImagePyramidImpl::getLayer(OutputArray _outImg, Size outRoi, Stream& stream) const
{ {
curScale = lastScale * 0.5f; CV_Assert( outRoi.width <= layer0_.cols && outRoi.height <= layer0_.rows && outRoi.width > 0 && outRoi.height > 0 );
curLayer = pyramid_[i];
ensureSizeIsEnough(outRoi, layer0_.type(), _outImg);
GpuMat outImg = _outImg.getGpuMat();
if (outRoi.width == curLayer.cols && outRoi.height == curLayer.rows) if (outRoi.width == layer0_.cols && outRoi.height == layer0_.rows)
{ {
curLayer.copyTo(outImg, stream); layer0_.copyTo(outImg, stream);
return;
} }
if (outRoi.width >= curLayer.cols && outRoi.height >= curLayer.rows) float lastScale = 1.0f;
break; float curScale;
GpuMat lastLayer = layer0_;
GpuMat curLayer;
for (int i = 0; i < nLayers_ - 1; ++i)
{
curScale = lastScale * 0.5f;
curLayer = pyramid_[i];
if (outRoi.width == curLayer.cols && outRoi.height == curLayer.rows)
{
curLayer.copyTo(outImg, stream);
}
if (outRoi.width >= curLayer.cols && outRoi.height >= curLayer.rows)
break;
lastScale = curScale;
lastLayer = curLayer;
}
lastScale = curScale; cudev::pyramid::interpolateFrom1(lastLayer, outImg, outImg.depth(), outImg.channels(), StreamAccessor::getStream(stream));
lastLayer = curLayer;
} }
}
#endif
cudev::pyramid::interpolateFrom1(lastLayer, outImg, outImg.depth(), outImg.channels(), StreamAccessor::getStream(stream)); Ptr<ImagePyramid> cv::gpu::createImagePyramid(InputArray img, int nLayers, Stream& stream)
{
#ifndef HAVE_OPENCV_GPULEGACY
(void) img;
(void) numLayers;
(void) stream;
throw_no_cuda();
return Ptr<ImagePyramid>();
#else
return new ImagePyramidImpl(img, nLayers, stream);
#endif #endif
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment