--- /dev/null
- error = gpu::sum(diff, norm_buf)[0];
+/*M///////////////////////////////////////////////////////////////////////////////////////
+//
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
+//
+// By downloading, copying, installing or using the software you agree to this license.
+// If you do not agree to this license, do not download, install,
+// copy or use the software.
+//
+//
+// License Agreement
+// For Open Source Computer Vision Library
+//
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
+// Third party copyrights are property of their respective owners.
+//
+// Redistribution and use in source and binary forms, with or without modification,
+// are permitted provided that the following conditions are met:
+//
+// * Redistribution's of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// * Redistribution's in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// * The name of the copyright holders may not be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// This software is provided by the copyright holders and contributors "as is" and
+// any express or implied warranties, including, but not limited to, the implied
+// warranties of merchantability and fitness for a particular purpose are disclaimed.
+// In no event shall the Intel Corporation or contributors be liable for any direct,
+// indirect, incidental, special, exemplary, or consequential damages
+// (including, but not limited to, procurement of substitute goods or services;
+// loss of use, data, or profits; or business interruption) however caused
+// and on any theory of liability, whether in contract, strict liability,
+// or tort (including negligence or otherwise) arising in any way out of
+// the use of this software, even if advised of the possibility of such damage.
+//
+//M*/
+
+#include "precomp.hpp"
+
+#if !defined HAVE_CUDA || defined(CUDA_DISABLER)
+
+cv::gpu::OpticalFlowDual_TVL1_GPU::OpticalFlowDual_TVL1_GPU() { throw_no_cuda(); }
+void cv::gpu::OpticalFlowDual_TVL1_GPU::operator ()(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&) { throw_no_cuda(); }
+void cv::gpu::OpticalFlowDual_TVL1_GPU::collectGarbage() {}
+void cv::gpu::OpticalFlowDual_TVL1_GPU::procOneScale(const GpuMat&, const GpuMat&, GpuMat&, GpuMat&) { throw_no_cuda(); }
+
+#else
+
+using namespace cv;
+using namespace cv::gpu;
+
+cv::gpu::OpticalFlowDual_TVL1_GPU::OpticalFlowDual_TVL1_GPU()
+{
+ tau = 0.25;
+ lambda = 0.15;
+ theta = 0.3;
+ nscales = 5;
+ warps = 5;
+ epsilon = 0.01;
+ iterations = 300;
+ scaleStep = 0.8;
+ useInitialFlow = false;
+}
+
+void cv::gpu::OpticalFlowDual_TVL1_GPU::operator ()(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy)
+{
+ CV_Assert( I0.type() == CV_8UC1 || I0.type() == CV_32FC1 );
+ CV_Assert( I0.size() == I1.size() );
+ CV_Assert( I0.type() == I1.type() );
+ CV_Assert( !useInitialFlow || (flowx.size() == I0.size() && flowx.type() == CV_32FC1 && flowy.size() == flowx.size() && flowy.type() == flowx.type()) );
+ CV_Assert( nscales > 0 );
+
+ // allocate memory for the pyramid structure
+ I0s.resize(nscales);
+ I1s.resize(nscales);
+ u1s.resize(nscales);
+ u2s.resize(nscales);
+
+ I0.convertTo(I0s[0], CV_32F, I0.depth() == CV_8U ? 1.0 : 255.0);
+ I1.convertTo(I1s[0], CV_32F, I1.depth() == CV_8U ? 1.0 : 255.0);
+
+ if (!useInitialFlow)
+ {
+ flowx.create(I0.size(), CV_32FC1);
+ flowy.create(I0.size(), CV_32FC1);
+ }
+
+ u1s[0] = flowx;
+ u2s[0] = flowy;
+
+ I1x_buf.create(I0.size(), CV_32FC1);
+ I1y_buf.create(I0.size(), CV_32FC1);
+
+ I1w_buf.create(I0.size(), CV_32FC1);
+ I1wx_buf.create(I0.size(), CV_32FC1);
+ I1wy_buf.create(I0.size(), CV_32FC1);
+
+ grad_buf.create(I0.size(), CV_32FC1);
+ rho_c_buf.create(I0.size(), CV_32FC1);
+
+ p11_buf.create(I0.size(), CV_32FC1);
+ p12_buf.create(I0.size(), CV_32FC1);
+ p21_buf.create(I0.size(), CV_32FC1);
+ p22_buf.create(I0.size(), CV_32FC1);
+
+ diff_buf.create(I0.size(), CV_32FC1);
+
+ // create the scales
+ for (int s = 1; s < nscales; ++s)
+ {
+ gpu::resize(I0s[s-1], I0s[s], Size(), scaleStep, scaleStep);
+ gpu::resize(I1s[s-1], I1s[s], Size(), scaleStep, scaleStep);
+
+ if (I0s[s].cols < 16 || I0s[s].rows < 16)
+ {
+ nscales = s;
+ break;
+ }
+
+ if (useInitialFlow)
+ {
+ gpu::resize(u1s[s-1], u1s[s], Size(), scaleStep, scaleStep);
+ gpu::resize(u2s[s-1], u2s[s], Size(), scaleStep, scaleStep);
+
+ gpu::multiply(u1s[s], Scalar::all(scaleStep), u1s[s]);
+ gpu::multiply(u2s[s], Scalar::all(scaleStep), u2s[s]);
+ }
+ else
+ {
+ u1s[s].create(I0s[s].size(), CV_32FC1);
+ u2s[s].create(I0s[s].size(), CV_32FC1);
+ }
+ }
+
+ if (!useInitialFlow)
+ {
+ u1s[nscales-1].setTo(Scalar::all(0));
+ u2s[nscales-1].setTo(Scalar::all(0));
+ }
+
+ // pyramidal structure for computing the optical flow
+ for (int s = nscales - 1; s >= 0; --s)
+ {
+ // compute the optical flow at the current scale
+ procOneScale(I0s[s], I1s[s], u1s[s], u2s[s]);
+
+ // if this was the last scale, finish now
+ if (s == 0)
+ break;
+
+ // otherwise, upsample the optical flow
+
+ // zoom the optical flow for the next finer scale
+ gpu::resize(u1s[s], u1s[s - 1], I0s[s - 1].size());
+ gpu::resize(u2s[s], u2s[s - 1], I0s[s - 1].size());
+
+ // scale the optical flow with the appropriate zoom factor
+ gpu::multiply(u1s[s - 1], Scalar::all(1/scaleStep), u1s[s - 1]);
+ gpu::multiply(u2s[s - 1], Scalar::all(1/scaleStep), u2s[s - 1]);
+ }
+}
+
+namespace tvl1flow
+{
+ void centeredGradient(PtrStepSzf src, PtrStepSzf dx, PtrStepSzf dy);
+ void warpBackward(PtrStepSzf I0, PtrStepSzf I1, PtrStepSzf I1x, PtrStepSzf I1y, PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf I1w, PtrStepSzf I1wx, PtrStepSzf I1wy, PtrStepSzf grad, PtrStepSzf rho);
+ void estimateU(PtrStepSzf I1wx, PtrStepSzf I1wy,
+ PtrStepSzf grad, PtrStepSzf rho_c,
+ PtrStepSzf p11, PtrStepSzf p12, PtrStepSzf p21, PtrStepSzf p22,
+ PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf error,
+ float l_t, float theta);
+ void estimateDualVariables(PtrStepSzf u1, PtrStepSzf u2, PtrStepSzf p11, PtrStepSzf p12, PtrStepSzf p21, PtrStepSzf p22, float taut);
+}
+
+void cv::gpu::OpticalFlowDual_TVL1_GPU::procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2)
+{
+ using namespace tvl1flow;
+
+ const double scaledEpsilon = epsilon * epsilon * I0.size().area();
+
+ CV_DbgAssert( I1.size() == I0.size() );
+ CV_DbgAssert( I1.type() == I0.type() );
+ CV_DbgAssert( u1.size() == I0.size() );
+ CV_DbgAssert( u2.size() == u1.size() );
+
+ GpuMat I1x = I1x_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat I1y = I1y_buf(Rect(0, 0, I0.cols, I0.rows));
+ centeredGradient(I1, I1x, I1y);
+
+ GpuMat I1w = I1w_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat I1wx = I1wx_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat I1wy = I1wy_buf(Rect(0, 0, I0.cols, I0.rows));
+
+ GpuMat grad = grad_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat rho_c = rho_c_buf(Rect(0, 0, I0.cols, I0.rows));
+
+ GpuMat p11 = p11_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat p12 = p12_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat p21 = p21_buf(Rect(0, 0, I0.cols, I0.rows));
+ GpuMat p22 = p22_buf(Rect(0, 0, I0.cols, I0.rows));
+ p11.setTo(Scalar::all(0));
+ p12.setTo(Scalar::all(0));
+ p21.setTo(Scalar::all(0));
+ p22.setTo(Scalar::all(0));
+
+ GpuMat diff = diff_buf(Rect(0, 0, I0.cols, I0.rows));
+
+ const float l_t = static_cast<float>(lambda * theta);
+ const float taut = static_cast<float>(tau / theta);
+
+ for (int warpings = 0; warpings < warps; ++warpings)
+ {
+ warpBackward(I0, I1, I1x, I1y, u1, u2, I1w, I1wx, I1wy, grad, rho_c);
+
+ double error = std::numeric_limits<double>::max();
+ for (int n = 0; error > scaledEpsilon && n < iterations; ++n)
+ {
+ estimateU(I1wx, I1wy, grad, rho_c, p11, p12, p21, p22, u1, u2, diff, l_t, static_cast<float>(theta));
+
++ if (epsilon > 0)
++ error = gpu::sum(diff, norm_buf)[0];
+
+ estimateDualVariables(u1, u2, p11, p12, p21, p22, taut);
+ }
+ }
+}
+
+void cv::gpu::OpticalFlowDual_TVL1_GPU::collectGarbage()
+{
+ I0s.clear();
+ I1s.clear();
+ u1s.clear();
+ u2s.clear();
+
+ I1x_buf.release();
+ I1y_buf.release();
+
+ I1w_buf.release();
+ I1wx_buf.release();
+ I1wy_buf.release();
+
+ grad_buf.release();
+ rho_c_buf.release();
+
+ p11_buf.release();
+ p12_buf.release();
+ p21_buf.release();
+ p22_buf.release();
+
+ diff_buf.release();
+ norm_buf.release();
+}
+
+#endif // !defined HAVE_CUDA || defined(CUDA_DISABLER)
--- /dev/null
- #include "opencv2/core/cuda/scan.hpp"
+/*M///////////////////////////////////////////////////////////////////////////////////////
+//
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
+//
+// By downloading, copying, installing or using the software you agree to this license.
+// If you do not agree to this license, do not download, install,
+// copy or use the software.
+//
+//
+// License Agreement
+// For Open Source Computer Vision Library
+//
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
+// Third party copyrights are property of their respective owners.
+//
+// Redistribution and use in source and binary forms, with or without modification,
+// are permitted provided that the following conditions are met:
+//
+// * Redistribution's of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// * Redistribution's in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// * The name of the copyright holders may not be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// This software is provided by the copyright holders and contributors "as is" and
+// any express or implied warranties, including, but not limited to, the implied
+// warranties of merchantability and fitness for a particular purpose are disclaimed.
+// In no event shall the Intel Corporation or contributors be liable for any direct,
+// indirect, incidental, special, exemplary, or consequential damages
+// (including, but not limited to, procurement of substitute goods or services;
+// loss of use, data, or profits; or business interruption) however caused
+// and on any theory of liability, whether in contract, strict liability,
+// or tort (including negligence or otherwise) arising in any way out of
+// the use of this software, even if advised of the possibility of such damage.
+//
+//M*/
+
+#if !defined CUDA_DISABLER
+
+#include <cfloat>
+#include "opencv2/core/cuda/common.hpp"
+#include "opencv2/core/cuda/border_interpolate.hpp"
+#include "opencv2/core/cuda/vec_traits.hpp"
+#include "opencv2/core/cuda/vec_math.hpp"
+#include "opencv2/core/cuda/saturate_cast.hpp"
+#include "opencv2/core/cuda/filters.hpp"
- namespace imgproc
+
+namespace cv { namespace gpu { namespace cudev
+{
- template <typename Ptr2D, typename T> __global__ void resize(const Ptr2D src, float fx, float fy, PtrStepSz<T> dst)
++ // kernels
++
++ template <typename T> __global__ void resize_nearest(const PtrStep<T> src, PtrStepSz<T> dst, const float fy, const float fx)
+ {
- const int x = blockDim.x * blockIdx.x + threadIdx.x;
- const int y = blockDim.y * blockIdx.y + threadIdx.y;
++ const int dst_x = blockDim.x * blockIdx.x + threadIdx.x;
++ const int dst_y = blockDim.y * blockIdx.y + threadIdx.y;
++
++ if (dst_x < dst.cols && dst_y < dst.rows)
+ {
- if (x < dst.cols && y < dst.rows)
- {
- const float xcoo = x * fx;
- const float ycoo = y * fy;
++ const float src_x = dst_x * fx;
++ const float src_y = dst_y * fy;
+
- dst(y, x) = saturate_cast<T>(src(ycoo, xcoo));
- }
++ dst(dst_y, dst_x) = src(__float2int_rz(src_y), __float2int_rz(src_x));
++ }
++ }
+
- template <typename Ptr2D, typename T> __global__ void resize_area(const Ptr2D src, float fx, float fy, PtrStepSz<T> dst)
++ template <typename T> __global__ void resize_linear(const PtrStepSz<T> src, PtrStepSz<T> dst, const float fy, const float fx)
++ {
++ typedef typename TypeVec<float, VecTraits<T>::cn>::vec_type work_type;
++
++ const int dst_x = blockDim.x * blockIdx.x + threadIdx.x;
++ const int dst_y = blockDim.y * blockIdx.y + threadIdx.y;
++
++ if (dst_x < dst.cols && dst_y < dst.rows)
++ {
++ const float src_x = dst_x * fx;
++ const float src_y = dst_y * fy;
++
++ work_type out = VecTraits<work_type>::all(0);
++
++ const int x1 = __float2int_rd(src_x);
++ const int y1 = __float2int_rd(src_y);
++ const int x2 = x1 + 1;
++ const int y2 = y1 + 1;
++ const int x2_read = ::min(x2, src.cols - 1);
++ const int y2_read = ::min(y2, src.rows - 1);
++
++ T src_reg = src(y1, x1);
++ out = out + src_reg * ((x2 - src_x) * (y2 - src_y));
++
++ src_reg = src(y1, x2_read);
++ out = out + src_reg * ((src_x - x1) * (y2 - src_y));
++
++ src_reg = src(y2_read, x1);
++ out = out + src_reg * ((x2 - src_x) * (src_y - y1));
++
++ src_reg = src(y2_read, x2_read);
++ out = out + src_reg * ((src_x - x1) * (src_y - y1));
++
++ dst(dst_y, dst_x) = saturate_cast<T>(out);
+ }
++ }
++
++ template <class Ptr2D, typename T> __global__ void resize(const Ptr2D src, PtrStepSz<T> dst, const float fy, const float fx)
++ {
++ const int dst_x = blockDim.x * blockIdx.x + threadIdx.x;
++ const int dst_y = blockDim.y * blockIdx.y + threadIdx.y;
+
- const int x = blockDim.x * blockIdx.x + threadIdx.x;
- const int y = blockDim.y * blockIdx.y + threadIdx.y;
++ if (dst_x < dst.cols && dst_y < dst.rows)
+ {
- if (x < dst.cols && y < dst.rows)
- {
- dst(y, x) = saturate_cast<T>(src(y, x));
- }
++ const float src_x = dst_x * fx;
++ const float src_y = dst_y * fy;
+
- template <template <typename> class Filter, typename T> struct ResizeDispatcherStream
++ dst(dst_y, dst_x) = src(src_y, src_x);
+ }
++ }
+
- static void call(PtrStepSz<T> src, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream)
- {
- dim3 block(32, 8);
- dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++ template <typename Ptr2D, typename T> __global__ void resize_area(const Ptr2D src, PtrStepSz<T> dst)
++ {
++ const int x = blockDim.x * blockIdx.x + threadIdx.x;
++ const int y = blockDim.y * blockIdx.y + threadIdx.y;
++
++ if (x < dst.cols && y < dst.rows)
+ {
- BrdReplicate<T> brd(src.rows, src.cols);
- BorderReader< PtrStep<T>, BrdReplicate<T> > brdSrc(src, brd);
- Filter< BorderReader< PtrStep<T>, BrdReplicate<T> > > filteredSrc(brdSrc, fx, fy);
++ dst(y, x) = src(y, x);
++ }
++ }
+
- resize<<<grid, block, 0, stream>>>(filteredSrc, fx, fy, dst);
- cudaSafeCall( cudaGetLastError() );
- }
++ // textures
+
- template <typename T> struct ResizeDispatcherStream<AreaFilter, T>
++ template <typename T> struct TextureAccessor;
++
++ #define OPENCV_GPU_IMPLEMENT_RESIZE_TEX(type) \
++ texture<type, cudaTextureType2D, cudaReadModeElementType> tex_resize_##type (0, cudaFilterModePoint, cudaAddressModeClamp); \
++ template <> struct TextureAccessor<type> \
++ { \
++ typedef type elem_type; \
++ typedef int index_type; \
++ int xoff; \
++ int yoff; \
++ __device__ __forceinline__ elem_type operator ()(index_type y, index_type x) const \
++ { \
++ return tex2D(tex_resize_##type, x + xoff, y + yoff); \
++ } \
++ __host__ static void bind(const PtrStepSz<type>& mat) \
++ { \
++ bindTexture(&tex_resize_##type, mat); \
++ } \
+ };
+
- static void call(PtrStepSz<T> src, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream)
- {
- dim3 block(32, 8);
- dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(uchar)
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(uchar4)
++
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(ushort)
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(ushort4)
++
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(short)
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(short4)
++
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(float)
++ OPENCV_GPU_IMPLEMENT_RESIZE_TEX(float4)
++
++ #undef OPENCV_GPU_IMPLEMENT_RESIZE_TEX
++
++ template <typename T>
++ TextureAccessor<T> texAccessor(const PtrStepSz<T>& mat, int yoff, int xoff)
++ {
++ TextureAccessor<T>::bind(mat);
++
++ TextureAccessor<T> t;
++ t.xoff = xoff;
++ t.yoff = yoff;
++
++ return t;
++ }
++
++ // callers for nearest interpolation
++
++ template <typename T>
++ void call_resize_nearest_glob(const PtrStepSz<T>& src, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ resize_nearest<<<grid, block, 0, stream>>>(src, dst, fy, fx);
++ cudaSafeCall( cudaGetLastError() );
++
++ if (stream == 0)
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
++
++ template <typename T>
++ void call_resize_nearest_tex(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ resize<<<grid, block>>>(texAccessor(srcWhole, yoff, xoff), dst, fy, fx);
++ cudaSafeCall( cudaGetLastError() );
++
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
++
++ // callers for linear interpolation
++
++ template <typename T>
++ void call_resize_linear_glob(const PtrStepSz<T>& src, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ resize_linear<<<grid, block>>>(src, dst, fy, fx);
++ cudaSafeCall( cudaGetLastError() );
++
++ if (stream == 0)
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
++
++ template <typename T>
++ void call_resize_linear_tex(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ if (srcWhole.data == src.data)
+ {
- BrdConstant<T> brd(src.rows, src.cols);
- BorderReader< PtrStep<T>, BrdConstant<T> > brdSrc(src, brd);
- AreaFilter< BorderReader< PtrStep<T>, BrdConstant<T> > > filteredSrc(brdSrc, fx, fy);
- resize_area<<<grid, block, 0, stream>>>(filteredSrc, fx, fy, dst);
- cudaSafeCall( cudaGetLastError() );
- if (stream == 0)
- cudaSafeCall( cudaDeviceSynchronize() );
- }
- };
++ TextureAccessor<T> texSrc = texAccessor(src, 0, 0);
++ LinearFilter< TextureAccessor<T> > filteredSrc(texSrc);
+
- template <typename T> struct ResizeDispatcherStream<IntegerAreaFilter, T>
++ resize<<<grid, block>>>(filteredSrc, dst, fy, fx);
++ }
++ else
++ {
++ TextureAccessor<T> texSrc = texAccessor(srcWhole, yoff, xoff);
++
++ BrdReplicate<T> brd(src.rows, src.cols);
++ BorderReader<TextureAccessor<T>, BrdReplicate<T> > brdSrc(texSrc, brd);
++ LinearFilter< BorderReader<TextureAccessor<T>, BrdReplicate<T> > > filteredSrc(brdSrc);
++
++ resize<<<grid, block>>>(filteredSrc, dst, fy, fx);
++ }
++
++ cudaSafeCall( cudaGetLastError() );
++
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
++
++ // callers for cubic interpolation
++
++ template <typename T>
++ void call_resize_cubic_glob(const PtrStepSz<T>& src, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ BrdReplicate<T> brd(src.rows, src.cols);
++ BorderReader< PtrStep<T>, BrdReplicate<T> > brdSrc(src, brd);
++ CubicFilter< BorderReader< PtrStep<T>, BrdReplicate<T> > > filteredSrc(brdSrc);
++
++ resize<<<grid, block, 0, stream>>>(filteredSrc, dst, fy, fx);
++ cudaSafeCall( cudaGetLastError() );
++
++ if (stream == 0)
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
+
- static void call(PtrStepSz<T> src, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream)
- {
- dim3 block(32, 8);
- dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
- BrdConstant<T> brd(src.rows, src.cols);
- BorderReader< PtrStep<T>, BrdConstant<T> > brdSrc(src, brd);
- IntegerAreaFilter< BorderReader< PtrStep<T>, BrdConstant<T> > > filteredSrc(brdSrc, fx, fy);
- resize_area<<<grid, block, 0, stream>>>(filteredSrc, fx, fy, dst);
- cudaSafeCall( cudaGetLastError() );
- if (stream == 0)
- cudaSafeCall( cudaDeviceSynchronize() );
- }
- };
++ template <typename T>
++ void call_resize_cubic_tex(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx)
++ {
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++
++ if (srcWhole.data == src.data)
+ {
- template <template <typename> class Filter, typename T> struct ResizeDispatcherNonStream
++ TextureAccessor<T> texSrc = texAccessor(src, 0, 0);
++ CubicFilter< TextureAccessor<T> > filteredSrc(texSrc);
+
- static void call(PtrStepSz<T> src, PtrStepSz<T> srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSz<T> dst)
- {
- (void)srcWhole;
- (void)xoff;
- (void)yoff;
++ resize<<<grid, block>>>(filteredSrc, dst, fy, fx);
++ }
++ else
+ {
- dim3 block(32, 8);
- dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
++ TextureAccessor<T> texSrc = texAccessor(srcWhole, yoff, xoff);
+
- BrdReplicate<T> brd(src.rows, src.cols);
- BorderReader< PtrStep<T>, BrdReplicate<T> > brdSrc(src, brd);
- Filter< BorderReader< PtrStep<T>, BrdReplicate<T> > > filteredSrc(brdSrc);
++ BrdReplicate<T> brd(src.rows, src.cols);
++ BorderReader<TextureAccessor<T>, BrdReplicate<T> > brdSrc(texSrc, brd);
++ CubicFilter< BorderReader<TextureAccessor<T>, BrdReplicate<T> > > filteredSrc(brdSrc);
+
- resize<<<grid, block>>>(filteredSrc, fx, fy, dst);
- cudaSafeCall( cudaGetLastError() );
++ resize<<<grid, block>>>(filteredSrc, dst, fy, fx);
++ }
+
- cudaSafeCall( cudaDeviceSynchronize() );
- }
- };
++ cudaSafeCall( cudaGetLastError() );
+
- #define OPENCV_GPU_IMPLEMENT_RESIZE_TEX(type) \
- texture< type , cudaTextureType2D> tex_resize_ ## type (0, cudaFilterModePoint, cudaAddressModeClamp); \
- struct tex_resize_ ## type ## _reader \
- { \
- typedef type elem_type; \
- typedef int index_type; \
- const int xoff; \
- const int yoff; \
- __host__ tex_resize_ ## type ## _reader(int xoff_, int yoff_) : xoff(xoff_), yoff(yoff_) {} \
- __device__ __forceinline__ elem_type operator ()(index_type y, index_type x) const \
- { \
- return tex2D(tex_resize_ ## type, x + xoff, y + yoff); \
- } \
- }; \
- template <template <typename> class Filter> struct ResizeDispatcherNonStream<Filter, type > \
- { \
- static void call(PtrStepSz< type > src, PtrStepSz< type > srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSz< type > dst) \
- { \
- dim3 block(32, 8); \
- dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y)); \
- bindTexture(&tex_resize_ ## type, srcWhole); \
- tex_resize_ ## type ## _reader texSrc(xoff, yoff); \
- if (srcWhole.cols == src.cols && srcWhole.rows == src.rows) \
- { \
- Filter<tex_resize_ ## type ## _reader> filteredSrc(texSrc); \
- resize<<<grid, block>>>(filteredSrc, fx, fy, dst); \
- } \
- else \
- { \
- BrdReplicate< type > brd(src.rows, src.cols); \
- BorderReader<tex_resize_ ## type ## _reader, BrdReplicate< type > > brdSrc(texSrc, brd); \
- Filter< BorderReader<tex_resize_ ## type ## _reader, BrdReplicate< type > > > filteredSrc(brdSrc); \
- resize<<<grid, block>>>(filteredSrc, fx, fy, dst); \
- } \
- cudaSafeCall( cudaGetLastError() ); \
- cudaSafeCall( cudaDeviceSynchronize() ); \
- } \
- };
-
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(uchar)
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(uchar4)
-
- //OPENCV_GPU_IMPLEMENT_RESIZE_TEX(schar)
- //OPENCV_GPU_IMPLEMENT_RESIZE_TEX(char4)
-
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(ushort)
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(ushort4)
-
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(short)
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(short4)
-
- //OPENCV_GPU_IMPLEMENT_RESIZE_TEX(int)
- //OPENCV_GPU_IMPLEMENT_RESIZE_TEX(int4)
-
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(float)
- OPENCV_GPU_IMPLEMENT_RESIZE_TEX(float4)
-
- #undef OPENCV_GPU_IMPLEMENT_RESIZE_TEX
-
- template <template <typename> class Filter, typename T> struct ResizeDispatcher
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
+
- static void call(PtrStepSz<T> src, PtrStepSz<T> srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream)
++ // ResizeNearestDispatcher
++
++ template <typename T> struct ResizeNearestDispatcher
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
+ {
- if (stream == 0)
- ResizeDispatcherNonStream<Filter, T>::call(src, srcWhole, xoff, yoff, fx, fy, dst);
++ call_resize_nearest_glob(src, dst, fy, fx, stream);
++ }
++ };
++
++ template <typename T> struct SelectImplForNearest
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ if (stream)
++ call_resize_nearest_glob(src, dst, fy, fx, stream);
++ else
+ {
- ResizeDispatcherStream<Filter, T>::call(src, fx, fy, dst, stream);
++ if (fx > 1 || fy > 1)
++ call_resize_nearest_glob(src, dst, fy, fx, 0);
+ else
- };
++ call_resize_nearest_tex(src, srcWhole, yoff, xoff, dst, fy, fx);
+ }
- template <typename T> struct ResizeDispatcher<AreaFilter, T>
++ }
++ };
++
++ template <> struct ResizeNearestDispatcher<uchar> : SelectImplForNearest<uchar> {};
++ template <> struct ResizeNearestDispatcher<uchar4> : SelectImplForNearest<uchar4> {};
++
++ template <> struct ResizeNearestDispatcher<ushort> : SelectImplForNearest<ushort> {};
++ template <> struct ResizeNearestDispatcher<ushort4> : SelectImplForNearest<ushort4> {};
++
++ template <> struct ResizeNearestDispatcher<short> : SelectImplForNearest<short> {};
++ template <> struct ResizeNearestDispatcher<short4> : SelectImplForNearest<short4> {};
+
- static void call(PtrStepSz<T> src, PtrStepSz<T> srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream)
++ template <> struct ResizeNearestDispatcher<float> : SelectImplForNearest<float> {};
++ template <> struct ResizeNearestDispatcher<float4> : SelectImplForNearest<float4> {};
++
++ // ResizeLinearDispatcher
++
++ template <typename T> struct ResizeLinearDispatcher
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
+ {
- (void)srcWhole;
- (void)xoff;
- (void)yoff;
- int iscale_x = (int)round(fx);
- int iscale_y = (int)round(fy);
-
- if( std::abs(fx - iscale_x) < FLT_MIN && std::abs(fy - iscale_y) < FLT_MIN)
- ResizeDispatcherStream<IntegerAreaFilter, T>::call(src, fx, fy, dst, stream);
++ call_resize_linear_glob(src, dst, fy, fx, stream);
++ }
++ };
++
++ template <typename T> struct SelectImplForLinear
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ if (stream)
++ call_resize_linear_glob(src, dst, fy, fx, stream);
++ else
+ {
- ResizeDispatcherStream<AreaFilter, T>::call(src, fx, fy, dst, stream);
++ if (fx > 1 || fy > 1)
++ call_resize_linear_glob(src, dst, fy, fx, 0);
+ else
- };
++ call_resize_linear_tex(src, srcWhole, yoff, xoff, dst, fy, fx);
+ }
- template <typename T> void resize_gpu(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy,
- PtrStepSzb dst, int interpolation, cudaStream_t stream)
++ }
++ };
+
- typedef void (*caller_t)(PtrStepSz<T> src, PtrStepSz<T> srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSz<T> dst, cudaStream_t stream);
++ template <> struct ResizeLinearDispatcher<uchar> : SelectImplForLinear<uchar> {};
++ template <> struct ResizeLinearDispatcher<uchar4> : SelectImplForLinear<uchar4> {};
++
++ template <> struct ResizeLinearDispatcher<ushort> : SelectImplForLinear<ushort> {};
++ template <> struct ResizeLinearDispatcher<ushort4> : SelectImplForLinear<ushort4> {};
++
++ template <> struct ResizeLinearDispatcher<short> : SelectImplForLinear<short> {};
++ template <> struct ResizeLinearDispatcher<short4> : SelectImplForLinear<short4> {};
++
++ template <> struct ResizeLinearDispatcher<float> : SelectImplForLinear<float> {};
++ template <> struct ResizeLinearDispatcher<float4> : SelectImplForLinear<float4> {};
++
++ // ResizeCubicDispatcher
++
++ template <typename T> struct ResizeCubicDispatcher
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
+ {
- static const caller_t callers[4] =
- {
- ResizeDispatcher<PointFilter, T>::call,
- ResizeDispatcher<LinearFilter, T>::call,
- ResizeDispatcher<CubicFilter, T>::call,
- ResizeDispatcher<AreaFilter, T>::call
- };
- // chenge to linear if area interpolation upscaling
- if (interpolation == 3 && (fx <= 1.f || fy <= 1.f))
- interpolation = 1;
-
- callers[interpolation](static_cast< PtrStepSz<T> >(src), static_cast< PtrStepSz<T> >(srcWhole), xoff, yoff, fx, fy,
- static_cast< PtrStepSz<T> >(dst), stream);
++ call_resize_cubic_glob(src, dst, fy, fx, stream);
++ }
++ };
+
- template void resize_gpu<uchar >(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<uchar2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<uchar3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<uchar4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ template <typename T> struct SelectImplForCubic
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ if (stream)
++ call_resize_cubic_glob(src, dst, fy, fx, stream);
++ else
++ call_resize_cubic_tex(src, srcWhole, yoff, xoff, dst, fy, fx);
+ }
++ };
+
- //template void resize_gpu<schar>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<char2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<char3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<char4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ template <> struct ResizeCubicDispatcher<uchar> : SelectImplForCubic<uchar> {};
++ template <> struct ResizeCubicDispatcher<uchar4> : SelectImplForCubic<uchar4> {};
+
- template void resize_gpu<ushort >(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<ushort2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<ushort3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<ushort4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ template <> struct ResizeCubicDispatcher<ushort> : SelectImplForCubic<ushort> {};
++ template <> struct ResizeCubicDispatcher<ushort4> : SelectImplForCubic<ushort4> {};
+
- template void resize_gpu<short >(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<short2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<short3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<short4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ template <> struct ResizeCubicDispatcher<short> : SelectImplForCubic<short> {};
++ template <> struct ResizeCubicDispatcher<short4> : SelectImplForCubic<short4> {};
+
- //template void resize_gpu<int >(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<int2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<int3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<int4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ template <> struct ResizeCubicDispatcher<float> : SelectImplForCubic<float> {};
++ template <> struct ResizeCubicDispatcher<float4> : SelectImplForCubic<float4> {};
+
- template void resize_gpu<float >(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- //template void resize_gpu<float2>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<float3>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
- template void resize_gpu<float4>(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
++ // ResizeAreaDispatcher
+
- template<typename T> struct scan_traits{};
++ template <typename T> struct ResizeAreaDispatcher
++ {
++ static void call(const PtrStepSz<T>& src, const PtrStepSz<T>&, int, int, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream)
++ {
++ const int iscale_x = (int) round(fx);
++ const int iscale_y = (int) round(fy);
+
- template<> struct scan_traits<uchar>
++ const dim3 block(32, 8);
++ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
+
- typedef float scan_line_type;
++ if (std::abs(fx - iscale_x) < FLT_MIN && std::abs(fy - iscale_y) < FLT_MIN)
++ {
++ BrdConstant<T> brd(src.rows, src.cols);
++ BorderReader< PtrStep<T>, BrdConstant<T> > brdSrc(src, brd);
++ IntegerAreaFilter< BorderReader< PtrStep<T>, BrdConstant<T> > > filteredSrc(brdSrc, fx, fy);
++
++ resize_area<<<grid, block, 0, stream>>>(filteredSrc, dst);
++ }
++ else
++ {
++ BrdConstant<T> brd(src.rows, src.cols);
++ BorderReader< PtrStep<T>, BrdConstant<T> > brdSrc(src, brd);
++ AreaFilter< BorderReader< PtrStep<T>, BrdConstant<T> > > filteredSrc(brdSrc, fx, fy);
++
++ resize_area<<<grid, block, 0, stream>>>(filteredSrc, dst);
++ }
++
++ cudaSafeCall( cudaGetLastError() );
++
++ if (stream == 0)
++ cudaSafeCall( cudaDeviceSynchronize() );
++ }
++ };
++
++ // resize
++
++ template <typename T> void resize(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream)
++ {
++ typedef void (*func_t)(const PtrStepSz<T>& src, const PtrStepSz<T>& srcWhole, int yoff, int xoff, const PtrStepSz<T>& dst, float fy, float fx, cudaStream_t stream);
++ static const func_t funcs[4] =
+ {
- } // namespace imgproc
- }}} // namespace cv { namespace gpu { namespace cudev
++ ResizeNearestDispatcher<T>::call,
++ ResizeLinearDispatcher<T>::call,
++ ResizeCubicDispatcher<T>::call,
++ ResizeAreaDispatcher<T>::call
+ };
+
++ // change to linear if area interpolation upscaling
++ if (interpolation == 3 && (fx <= 1.f || fy <= 1.f))
++ interpolation = 1;
++
++ funcs[interpolation](static_cast< PtrStepSz<T> >(src), static_cast< PtrStepSz<T> >(srcWhole), yoff, xoff, static_cast< PtrStepSz<T> >(dst), fy, fx, stream);
++ }
++
++ template void resize<uchar >(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<uchar3>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<uchar4>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++
++ template void resize<ushort >(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<ushort3>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<ushort4>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++
++ template void resize<short >(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<short3>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<short4>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
+
++ template void resize<float >(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<float3>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ template void resize<float4>(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++}}}
+
+#endif /* CUDA_DISABLER */
--- /dev/null
- namespace imgproc
- {
- template <typename T>
- void resize_gpu(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy,
- PtrStepSzb dst, int interpolation, cudaStream_t stream);
- }
+/*M///////////////////////////////////////////////////////////////////////////////////////
+//
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
+//
+// By downloading, copying, installing or using the software you agree to this license.
+// If you do not agree to this license, do not download, install,
+// copy or use the software.
+//
+//
+// License Agreement
+// For Open Source Computer Vision Library
+//
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
+// Third party copyrights are property of their respective owners.
+//
+// Redistribution and use in source and binary forms, with or without modification,
+// are permitted provided that the following conditions are met:
+//
+// * Redistribution's of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// * Redistribution's in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// * The name of the copyright holders may not be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// This software is provided by the copyright holders and contributors "as is" and
+// any express or implied warranties, including, but not limited to, the implied
+// warranties of merchantability and fitness for a particular purpose are disclaimed.
+// In no event shall the Intel Corporation or contributors be liable for any direct,
+// indirect, incidental, special, exemplary, or consequential damages
+// (including, but not limited to, procurement of substitute goods or services;
+// loss of use, data, or profits; or business interruption) however caused
+// and on any theory of liability, whether in contract, strict liability,
+// or tort (including negligence or otherwise) arising in any way out of
+// the use of this software, even if advised of the possibility of such damage.
+//
+//M*/
+
+#include "precomp.hpp"
+
+#if !defined HAVE_CUDA || defined(CUDA_DISABLER)
+
+void cv::gpu::resize(InputArray, OutputArray, Size, double, double, int, Stream&) { throw_no_cuda(); }
+
+#else // HAVE_CUDA
+
+namespace cv { namespace gpu { namespace cudev
+{
- void cv::gpu::resize(InputArray _src, OutputArray _dst, Size dsize, double fx, double fy, int interpolation, Stream& _stream)
++ template <typename T>
++ void resize(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
+}}}
+
- src.copyTo(dst, _stream);
++void cv::gpu::resize(InputArray _src, OutputArray _dst, Size dsize, double fx, double fy, int interpolation, Stream& stream)
+{
+ GpuMat src = _src.getGpuMat();
+
++ typedef void (*func_t)(const PtrStepSzb& src, const PtrStepSzb& srcWhole, int yoff, int xoff, const PtrStepSzb& dst, float fy, float fx, int interpolation, cudaStream_t stream);
++ static const func_t funcs[6][4] =
++ {
++ {cudev::resize<uchar> , 0 /*cudev::resize<uchar2>*/ , cudev::resize<uchar3> , cudev::resize<uchar4> },
++ {0 /*cudev::resize<schar>*/, 0 /*cudev::resize<char2>*/ , 0 /*cudev::resize<char3>*/, 0 /*cudev::resize<char4>*/},
++ {cudev::resize<ushort> , 0 /*cudev::resize<ushort2>*/, cudev::resize<ushort3> , cudev::resize<ushort4> },
++ {cudev::resize<short> , 0 /*cudev::resize<short2>*/ , cudev::resize<short3> , cudev::resize<short4> },
++ {0 /*cudev::resize<int>*/ , 0 /*cudev::resize<int2>*/ , 0 /*cudev::resize<int3>*/ , 0 /*cudev::resize<int4>*/ },
++ {cudev::resize<float> , 0 /*cudev::resize<float2>*/ , cudev::resize<float3> , cudev::resize<float4> }
++ };
++
+ CV_Assert( src.depth() <= CV_32F && src.channels() <= 4 );
+ CV_Assert( interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC || interpolation == INTER_AREA );
+ CV_Assert( !(dsize == Size()) || (fx > 0 && fy > 0) );
+
+ if (dsize == Size())
+ {
+ dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
+ }
+ else
+ {
+ fx = static_cast<double>(dsize.width) / src.cols;
+ fy = static_cast<double>(dsize.height) / src.rows;
+ }
+
+ _dst.create(dsize, src.type());
+ GpuMat dst = _dst.getGpuMat();
+
+ if (dsize == src.size())
+ {
- cudaStream_t stream = StreamAccessor::getStream(_stream);
++ src.copyTo(dst, stream);
+ return;
+ }
+
- bool useNpp = (src.type() == CV_8UC1 || src.type() == CV_8UC4);
- useNpp = useNpp && (interpolation == INTER_NEAREST || interpolation == INTER_LINEAR);
-
- if (useNpp)
- {
- typedef NppStatus (*func_t)(const Npp8u * pSrc, NppiSize oSrcSize, int nSrcStep, NppiRect oSrcROI, Npp8u * pDst, int nDstStep, NppiSize dstROISize,
- double xFactor, double yFactor, int eInterpolation);
-
- const func_t funcs[4] = { nppiResize_8u_C1R, 0, 0, nppiResize_8u_C4R };
-
- static const int npp_inter[] = {NPPI_INTER_NN, NPPI_INTER_LINEAR, NPPI_INTER_CUBIC, 0, NPPI_INTER_LANCZOS};
-
- NppiSize srcsz;
- srcsz.width = wholeSize.width;
- srcsz.height = wholeSize.height;
-
- NppiRect srcrect;
- srcrect.x = ofs.x;
- srcrect.y = ofs.y;
- srcrect.width = src.cols;
- srcrect.height = src.rows;
-
- NppiSize dstsz;
- dstsz.width = dst.cols;
- dstsz.height = dst.rows;
-
- NppStreamHandler h(stream);
-
- nppSafeCall( funcs[src.channels() - 1](src.datastart, srcsz, static_cast<int>(src.step), srcrect,
- dst.ptr<Npp8u>(), static_cast<int>(dst.step), dstsz, fx, fy, npp_inter[interpolation]) );
-
- if (stream == 0)
- cudaSafeCall( cudaDeviceSynchronize() );
- }
- else
- {
- using namespace ::cv::gpu::cudev::imgproc;
-
- typedef void (*func_t)(PtrStepSzb src, PtrStepSzb srcWhole, int xoff, int yoff, float fx, float fy, PtrStepSzb dst, int interpolation, cudaStream_t stream);
-
- static const func_t funcs[6][4] =
- {
- {resize_gpu<uchar> , 0 /*resize_gpu<uchar2>*/ , resize_gpu<uchar3> , resize_gpu<uchar4> },
- {0 /*resize_gpu<schar>*/, 0 /*resize_gpu<char2>*/ , 0 /*resize_gpu<char3>*/, 0 /*resize_gpu<char4>*/},
- {resize_gpu<ushort> , 0 /*resize_gpu<ushort2>*/, resize_gpu<ushort3> , resize_gpu<ushort4> },
- {resize_gpu<short> , 0 /*resize_gpu<short2>*/ , resize_gpu<short3> , resize_gpu<short4> },
- {0 /*resize_gpu<int>*/ , 0 /*resize_gpu<int2>*/ , 0 /*resize_gpu<int3>*/ , 0 /*resize_gpu<int4>*/ },
- {resize_gpu<float> , 0 /*resize_gpu<float2>*/ , resize_gpu<float3> , resize_gpu<float4> }
- };
-
- const func_t func = funcs[src.depth()][src.channels() - 1];
- CV_Assert(func != 0);
-
- func(src, PtrStepSzb(wholeSize.height, wholeSize.width, src.datastart, src.step), ofs.x, ofs.y,
- static_cast<float>(1.0 / fx), static_cast<float>(1.0 / fy), dst, interpolation, stream);
- }
++ const func_t func = funcs[src.depth()][src.channels() - 1];
++
++ if (!func)
++ CV_Error(Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
+
+ Size wholeSize;
+ Point ofs;
+ src.locateROI(wholeSize, ofs);
++ PtrStepSzb wholeSrc(wholeSize.height, wholeSize.width, src.datastart, src.step);
+
++ func(src, wholeSrc, ofs.y, ofs.x, dst, static_cast<float>(1.0 / fy), static_cast<float>(1.0 / fx), interpolation, StreamAccessor::getStream(stream));
+}
+
+#endif // HAVE_CUDA
--- /dev/null
- testing::Values(MatType(CV_8UC3), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
+/*M///////////////////////////////////////////////////////////////////////////////////////
+//
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
+//
+// By downloading, copying, installing or using the software you agree to this license.
+// If you do not agree to this license, do not download, install,
+// copy or use the software.
+//
+//
+// License Agreement
+// For Open Source Computer Vision Library
+//
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
+// Third party copyrights are property of their respective owners.
+//
+// Redistribution and use in source and binary forms, with or without modification,
+// are permitted provided that the following conditions are met:
+//
+// * Redistribution's of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// * Redistribution's in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// * The name of the copyright holders may not be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// This software is provided by the copyright holders and contributors "as is" and
+// any express or implied warranties, including, but not limited to, the implied
+// warranties of merchantability and fitness for a particular purpose are disclaimed.
+// In no event shall the Intel Corporation or contributors be liable for any direct,
+// indirect, incidental, special, exemplary, or consequential damages
+// (including, but not limited to, procurement of substitute goods or services;
+// loss of use, data, or profits; or business interruption) however caused
+// and on any theory of liability, whether in contract, strict liability,
+// or tort (including negligence or otherwise) arising in any way out of
+// the use of this software, even if advised of the possibility of such damage.
+//
+//M*/
+
+#include "test_precomp.hpp"
+
+#ifdef HAVE_CUDA
+
+using namespace cvtest;
+
+///////////////////////////////////////////////////////////////////
+// Gold implementation
+
+namespace
+{
+ template <typename T, template <typename> class Interpolator>
+ void resizeImpl(const cv::Mat& src, cv::Mat& dst, double fx, double fy)
+ {
+ const int cn = src.channels();
+
+ cv::Size dsize(cv::saturate_cast<int>(src.cols * fx), cv::saturate_cast<int>(src.rows * fy));
+
+ dst.create(dsize, src.type());
+
+ float ifx = static_cast<float>(1.0 / fx);
+ float ify = static_cast<float>(1.0 / fy);
+
+ for (int y = 0; y < dsize.height; ++y)
+ {
+ for (int x = 0; x < dsize.width; ++x)
+ {
+ for (int c = 0; c < cn; ++c)
+ dst.at<T>(y, x * cn + c) = Interpolator<T>::getValue(src, y * ify, x * ifx, c, cv::BORDER_REPLICATE);
+ }
+ }
+ }
+
+ void resizeGold(const cv::Mat& src, cv::Mat& dst, double fx, double fy, int interpolation)
+ {
+ typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst, double fx, double fy);
+
+ static const func_t nearest_funcs[] =
+ {
+ resizeImpl<unsigned char, NearestInterpolator>,
+ resizeImpl<signed char, NearestInterpolator>,
+ resizeImpl<unsigned short, NearestInterpolator>,
+ resizeImpl<short, NearestInterpolator>,
+ resizeImpl<int, NearestInterpolator>,
+ resizeImpl<float, NearestInterpolator>
+ };
+
+
+ static const func_t linear_funcs[] =
+ {
+ resizeImpl<unsigned char, LinearInterpolator>,
+ resizeImpl<signed char, LinearInterpolator>,
+ resizeImpl<unsigned short, LinearInterpolator>,
+ resizeImpl<short, LinearInterpolator>,
+ resizeImpl<int, LinearInterpolator>,
+ resizeImpl<float, LinearInterpolator>
+ };
+
+ static const func_t cubic_funcs[] =
+ {
+ resizeImpl<unsigned char, CubicInterpolator>,
+ resizeImpl<signed char, CubicInterpolator>,
+ resizeImpl<unsigned short, CubicInterpolator>,
+ resizeImpl<short, CubicInterpolator>,
+ resizeImpl<int, CubicInterpolator>,
+ resizeImpl<float, CubicInterpolator>
+ };
+
+ static const func_t* funcs[] = {nearest_funcs, linear_funcs, cubic_funcs};
+
+ funcs[interpolation][src.depth()](src, dst, fx, fy);
+ }
+}
+
+///////////////////////////////////////////////////////////////////
+// Test
+
+PARAM_TEST_CASE(Resize, cv::gpu::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
+{
+ cv::gpu::DeviceInfo devInfo;
+ cv::Size size;
+ double coeff;
+ int interpolation;
+ int type;
+ bool useRoi;
+
+ virtual void SetUp()
+ {
+ devInfo = GET_PARAM(0);
+ size = GET_PARAM(1);
+ type = GET_PARAM(2);
+ coeff = GET_PARAM(3);
+ interpolation = GET_PARAM(4);
+ useRoi = GET_PARAM(5);
+
+ cv::gpu::setDevice(devInfo.deviceID());
+ }
+};
+
+GPU_TEST_P(Resize, Accuracy)
+{
+ cv::Mat src = randomMat(size, type);
+
+ cv::gpu::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
+ cv::gpu::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
+
+ cv::Mat dst_gold;
+ resizeGold(src, dst_gold, coeff, coeff, interpolation);
+
+ EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
+}
+
+INSTANTIATE_TEST_CASE_P(GPU_Warping, Resize, testing::Combine(
+ ALL_DEVICES,
+ DIFFERENT_SIZES,
- testing::Values(MatType(CV_8UC3), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
++ testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
+ testing::Values(0.3, 0.5, 1.5, 2.0),
+ testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)),
+ WHOLE_SUBMAT));
+
+/////////////////
+
+PARAM_TEST_CASE(ResizeSameAsHost, cv::gpu::DeviceInfo, cv::Size, MatType, double, Interpolation, UseRoi)
+{
+ cv::gpu::DeviceInfo devInfo;
+ cv::Size size;
+ double coeff;
+ int interpolation;
+ int type;
+ bool useRoi;
+
+ virtual void SetUp()
+ {
+ devInfo = GET_PARAM(0);
+ size = GET_PARAM(1);
+ type = GET_PARAM(2);
+ coeff = GET_PARAM(3);
+ interpolation = GET_PARAM(4);
+ useRoi = GET_PARAM(5);
+
+ cv::gpu::setDevice(devInfo.deviceID());
+ }
+};
+
+// downscaling only: used for classifiers
+GPU_TEST_P(ResizeSameAsHost, Accuracy)
+{
+ cv::Mat src = randomMat(size, type);
+
+ cv::gpu::GpuMat dst = createMat(cv::Size(cv::saturate_cast<int>(src.cols * coeff), cv::saturate_cast<int>(src.rows * coeff)), type, useRoi);
+ cv::gpu::resize(loadMat(src, useRoi), dst, cv::Size(), coeff, coeff, interpolation);
+
+ cv::Mat dst_gold;
+ cv::resize(src, dst_gold, cv::Size(), coeff, coeff, interpolation);
+
+ EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-2 : 1.0);
+}
+
+INSTANTIATE_TEST_CASE_P(GPU_Warping, ResizeSameAsHost, testing::Combine(
+ ALL_DEVICES,
+ DIFFERENT_SIZES,
- testing::Values(Interpolation(cv::INTER_AREA), Interpolation(cv::INTER_NEAREST)), //, Interpolation(cv::INTER_LINEAR), Interpolation(cv::INTER_CUBIC)
++ testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC3), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
+ testing::Values(0.3, 0.5),
- ///////////////////////////////////////////////////////////////////
- // Test NPP
-
- PARAM_TEST_CASE(ResizeNPP, cv::gpu::DeviceInfo, MatType, double, Interpolation)
- {
- cv::gpu::DeviceInfo devInfo;
- double coeff;
- int interpolation;
- int type;
-
- virtual void SetUp()
- {
- devInfo = GET_PARAM(0);
- type = GET_PARAM(1);
- coeff = GET_PARAM(2);
- interpolation = GET_PARAM(3);
-
- cv::gpu::setDevice(devInfo.deviceID());
- }
- };
-
- GPU_TEST_P(ResizeNPP, Accuracy)
- {
- cv::Mat src = readImageType("stereobp/aloe-L.png", type);
- ASSERT_FALSE(src.empty());
-
- cv::gpu::GpuMat dst;
- cv::gpu::resize(loadMat(src), dst, cv::Size(), coeff, coeff, interpolation);
-
- cv::Mat dst_gold;
- resizeGold(src, dst_gold, coeff, coeff, interpolation);
-
- EXPECT_MAT_SIMILAR(dst_gold, dst, 1e-1);
- }
-
- INSTANTIATE_TEST_CASE_P(GPU_Warping, ResizeNPP, testing::Combine(
- ALL_DEVICES,
- testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),
- testing::Values(0.3, 0.5, 1.5, 2.0),
- testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_LINEAR))));
-
++ testing::Values(Interpolation(cv::INTER_NEAREST), Interpolation(cv::INTER_AREA)),
+ WHOLE_SUBMAT));
+
+#endif // HAVE_CUDA
#include "perf_precomp.hpp"
- static int cvErrorCallback(int /*status*/, const char * /*func_name*/,
- const char *err_msg, const char * /*file_name*/,
- int /*line*/, void * /*userdata*/)
+ const char * impls[] =
{
- TestSystem::instance().printError(err_msg);
- return 0;
- }
-
- int main(int argc, const char *argv[])
+ IMPL_OCL,
+ IMPL_PLAIN,
+ #ifdef HAVE_OPENCV_GPU
+ IMPL_GPU
+ #endif
+ };
+
+ int main(int argc, char ** argv)
{
- const char *keys =
- "{ h help | false | print help message }"
- "{ f filter | | filter for test }"
- "{ w workdir | | set working directory }"
- "{ l list | false | show all tests }"
- "{ d device | 0 | device id }"
- "{ c cpu_ocl | false | use cpu as ocl device}"
- "{ i iters | 10 | iteration count }"
- "{ m warmup | 1 | gpu warm up iteration count}"
- "{ t xtop | 1.1 | xfactor top boundary}"
- "{ b xbottom | 0.9 | xfactor bottom boundary}"
- "{ v verify | false | only run gpu once to verify if problems occur}";
+ const char * keys =
- "{ h | help | false | print help message }"
- "{ t | type | gpu | set device type:cpu or gpu}"
- "{ p | platform | 0 | set platform id }"
- "{ d | device | 0 | set device id }";
++ "{ h help | false | print help message }"
++ "{ t type | gpu | set device type:cpu or gpu}"
++ "{ p platform | 0 | set platform id }"
++ "{ d device | 0 | set device id }";
- redirectError(cvErrorCallback);
CommandLineParser cmd(argc, argv, keys);
- if (cmd.get<bool>("help"))
+ if (cmd.has("help"))
{
- cout << "Avaible options:" << endl;
+ cout << "Available options besides google test option:" << endl;
- cmd.printParams();
+ cmd.printMessage();
return 0;
}
//M*/
#include "perf_precomp.hpp"
+
///////////// StereoMatchBM ////////////////////////
- PERFTEST(StereoMatchBM)
- {
- Mat left_image = imread(abspath("aloeL.jpg"), cv::IMREAD_GRAYSCALE);
- Mat right_image = imread(abspath("aloeR.jpg"), cv::IMREAD_GRAYSCALE);
- Mat disp,dst;
- ocl::oclMat d_left, d_right,d_disp;
- int n_disp= 128;
- int winSize =19;
- SUBTEST << left_image.cols << 'x' << left_image.rows << "; aloeL.jpg ;"<< right_image.cols << 'x' << right_image.rows << "; aloeR.jpg ";
+ PERF_TEST(StereoMatchBMFixture, DISABLED_StereoMatchBM) // TODO doesn't work properly
+ {
+ Mat left_image = imread(getDataPath("gpu/stereobm/aloe-L.png"), cv::IMREAD_GRAYSCALE);
+ Mat right_image = imread(getDataPath("gpu/stereobm/aloe-R.png"), cv::IMREAD_GRAYSCALE);
- Ptr<StereoBM> bm = createStereoBM(n_disp, winSize);
- bm->compute(left_image, right_image, dst);
+ ASSERT_TRUE(!left_image.empty()) << "no input image";
+ ASSERT_TRUE(!right_image.empty()) << "no input image";
+ ASSERT_TRUE(right_image.size() == left_image.size());
+ ASSERT_TRUE(right_image.size() == left_image.size());
- CPU_ON;
- bm->compute(left_image, right_image, dst);
- CPU_OFF;
+ const int n_disp = 128, winSize = 19;
+ Mat disp(left_image.size(), CV_16SC1);
- d_left.upload(left_image);
- d_right.upload(right_image);
+ declare.in(left_image, right_image).out(disp);
- ocl::StereoBM_OCL d_bm(0, n_disp, winSize);
+ if (RUN_OCL_IMPL)
+ {
+ ocl::oclMat oclLeft(left_image), oclRight(right_image),
+ oclDisp(left_image.size(), CV_16SC1);
+ ocl::StereoBM_OCL oclBM(0, n_disp, winSize);
- WARMUP_ON;
- d_bm(d_left, d_right, d_disp);
- WARMUP_OFF;
+ TEST_CYCLE() oclBM(oclLeft, oclRight, oclDisp);
- cv::Mat ocl_mat;
- d_disp.download(ocl_mat);
- ocl_mat.convertTo(ocl_mat, dst.type());
+ oclDisp.download(disp);
- GPU_ON;
- d_bm(d_left, d_right, d_disp);
- GPU_OFF;
+ SANITY_CHECK(disp);
+ }
+ else if (RUN_PLAIN_IMPL)
+ {
- StereoBM bm(0, n_disp, winSize);
++ Ptr<StereoBM> bm = createStereoBM(n_disp, winSize);
- GPU_FULL_ON;
- d_left.upload(left_image);
- d_right.upload(right_image);
- d_bm(d_left, d_right, d_disp);
- d_disp.download(disp);
- GPU_FULL_OFF;
- TEST_CYCLE() bm(left_image, right_image, disp);
++ TEST_CYCLE() bm->compute(left_image, right_image, disp);
- TestSystem::instance().setAccurate(-1, 0.);
+ SANITY_CHECK(disp);
+ }
+ else
+ OCL_PERF_ELSE
}
//M*/
#include "perf_precomp.hpp"
- ///////////// cvtColor////////////////////////
- PERFTEST(cvtColor)
- {
- Mat src, dst, ocl_dst;
- ocl::oclMat d_src, d_dst;
-
- int all_type[] = {CV_8UC4};
- std::string type_name[] = {"CV_8UC4"};
-
- for (int size = Min_Size; size <= Max_Size; size *= Multiple)
- {
- for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
- {
- gen(src, size, size, all_type[j], 0, 256);
- SUBTEST << size << "x" << size << "; " << type_name[j] << " ; CV_RGBA2GRAY";
-
- cvtColor(src, dst, COLOR_RGBA2GRAY, 4);
-
- CPU_ON;
- cvtColor(src, dst, COLOR_RGBA2GRAY, 4);
- CPU_OFF;
+ using namespace perf;
- d_src.upload(src);
+ ///////////// cvtColor////////////////////////
- WARMUP_ON;
- ocl::cvtColor(d_src, d_dst, COLOR_RGBA2GRAY, 4);
- WARMUP_OFF;
+ typedef TestBaseWithParam<Size> cvtColorFixture;
- GPU_ON;
- ocl::cvtColor(d_src, d_dst, COLOR_RGBA2GRAY, 4);
- GPU_OFF;
+ PERF_TEST_P(cvtColorFixture, cvtColor, OCL_TYPICAL_MAT_SIZES)
+ {
+ const Size srcSize = GetParam();
- GPU_FULL_ON;
- d_src.upload(src);
- ocl::cvtColor(d_src, d_dst, COLOR_RGBA2GRAY, 4);
- d_dst.download(ocl_dst);
- GPU_FULL_OFF;
+ Mat src(srcSize, CV_8UC4), dst(srcSize, CV_8UC4);
+ declare.in(src, WARMUP_RNG).out(dst);
- TestSystem::instance().ExceptedMatSimilar(dst, ocl_dst, 1e-5);
- }
+ if (RUN_OCL_IMPL)
+ {
+ ocl::oclMat oclSrc(src), oclDst(src.size(), CV_8UC4);
- TEST_CYCLE() ocl::cvtColor(oclSrc, oclDst, CV_RGBA2GRAY, 4);
++ TEST_CYCLE() ocl::cvtColor(oclSrc, oclDst, COLOR_RGBA2GRAY, 4);
+ oclDst.download(dst);
+ SANITY_CHECK(dst);
}
- TEST_CYCLE() cv::cvtColor(src, dst, CV_RGBA2GRAY, 4);
+ else if (RUN_PLAIN_IMPL)
+ {
++ TEST_CYCLE() cv::cvtColor(src, dst, COLOR_RGBA2GRAY, 4);
-
+ SANITY_CHECK(dst);
+ }
+ else
+ OCL_PERF_ELSE
}
//M*/
#include "perf_precomp.hpp"
- ///////////// Haar ////////////////////////
-
- PERFTEST(Haar)
- {
- Mat img = imread(abspath("basketball1.png"), IMREAD_GRAYSCALE);
++#include "opencv2/objdetect/objdetect_c.h"
+
- if (img.empty())
- {
- throw runtime_error("can't open basketball1.png");
- }
+ using namespace perf;
- CascadeClassifier faceCascadeCPU;
-
- if (!faceCascadeCPU.load(abspath("haarcascade_frontalface_alt.xml")))
- {
- throw runtime_error("can't load haarcascade_frontalface_alt.xml");
- }
+ ///////////// Haar ////////////////////////
-namespace cv
-{
-namespace ocl
-{
-
-struct getRect
-{
- Rect operator()(const CvAvgComp &e) const
- {
- return e.rect;
- }
-};
-
-class CascadeClassifier_GPU : public OclCascadeClassifier
-{
-public:
- void detectMultiScale(oclMat &image,
- CV_OUT std::vector<cv::Rect>& faces,
- double scaleFactor = 1.1,
- int minNeighbors = 3, int flags = 0,
- Size minSize = Size(),
- Size maxSize = Size())
- {
- (void)maxSize;
- MemStorage storage(cvCreateMemStorage(0));
- //CvMat img=image;
- CvSeq *objs = oclHaarDetectObjects(image, storage, scaleFactor, minNeighbors, flags, minSize);
- vector<CvAvgComp> vecAvgComp;
- Seq<CvAvgComp>(objs).copyTo(vecAvgComp);
- faces.resize(vecAvgComp.size());
- std::transform(vecAvgComp.begin(), vecAvgComp.end(), faces.begin(), getRect());
- }
-
-};
-
-}
-}
+ PERF_TEST(HaarFixture, Haar)
+ {
vector<Rect> faces;
- SUBTEST << img.cols << "x" << img.rows << "; scale image";
- CPU_ON;
- faceCascadeCPU.detectMultiScale(img, faces,
- 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
- CPU_OFF;
- Mat img = imread(getDataPath("gpu/haarcascade/basketball1.png"), CV_LOAD_IMAGE_GRAYSCALE);
++ Mat img = imread(getDataPath("gpu/haarcascade/basketball1.png"), IMREAD_GRAYSCALE);
+ ASSERT_TRUE(!img.empty()) << "can't open basketball1.png";
+ declare.in(img);
+ if (RUN_PLAIN_IMPL)
+ {
+ CascadeClassifier faceCascade;
+ ASSERT_TRUE(faceCascade.load(getDataPath("gpu/haarcascade/haarcascade_frontalface_alt.xml")))
+ << "can't load haarcascade_frontalface_alt.xml";
- vector<Rect> oclfaces;
- ocl::OclCascadeClassifier faceCascade;
+ TEST_CYCLE() faceCascade.detectMultiScale(img, faces,
+ 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
- if (!faceCascade.load(abspath("haarcascade_frontalface_alt.xml")))
- {
- throw runtime_error("can't load haarcascade_frontalface_alt.xml");
+ SANITY_CHECK(faces, 4 + 1e-4);
}
- ocl::CascadeClassifier_GPU faceCascade;
+ else if (RUN_OCL_IMPL)
+ {
++ ocl::OclCascadeClassifier faceCascade;
+ ocl::oclMat oclImg(img);
- ocl::oclMat d_img(img);
+ ASSERT_TRUE(faceCascade.load(getDataPath("gpu/haarcascade/haarcascade_frontalface_alt.xml")))
+ << "can't load haarcascade_frontalface_alt.xml";
- WARMUP_ON;
- faceCascade.detectMultiScale(d_img, oclfaces,
- 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
- WARMUP_OFF;
+ TEST_CYCLE() faceCascade.detectMultiScale(oclImg, faces,
+ 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
- if(faces.size() == oclfaces.size())
- TestSystem::instance().setAccurate(1, 0);
+ SANITY_CHECK(faces, 4 + 1e-4);
+ }
else
- TestSystem::instance().setAccurate(0, abs((int)faces.size() - (int)oclfaces.size()));
-
- faces.clear();
-
- GPU_ON;
- faceCascade.detectMultiScale(d_img, oclfaces,
- 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
- GPU_OFF;
-
- GPU_FULL_ON;
- d_img.upload(img);
- faceCascade.detectMultiScale(d_img, oclfaces,
- 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
- GPU_FULL_OFF;
+ OCL_PERF_ELSE
}
//M*/
#include "perf_precomp.hpp"
- /////////// matchTemplate ////////////////////////
- //void InitMatchTemplate()
- //{
- // Mat src; gen(src, 500, 500, CV_32F, 0, 1);
- // Mat templ; gen(templ, 500, 500, CV_32F, 0, 1);
- // ocl::oclMat d_src(src), d_templ(templ), d_dst;
- // ocl::matchTemplate(d_src, d_templ, d_dst, CV_TM_CCORR);
- //}
- PERFTEST(matchTemplate)
- {
- //InitMatchTemplate();
- Mat src, templ, dst, ocl_dst;
- int templ_size = 5;
-
- for (int size = Min_Size; size <= Max_Size; size *= Multiple)
- {
- int all_type[] = {CV_32FC1, CV_32FC4};
- std::string type_name[] = {"CV_32FC1", "CV_32FC4"};
-
- for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
- {
- for(templ_size = 5; templ_size <= 5; templ_size *= 5)
- {
- gen(src, size, size, all_type[j], 0, 1);
-
- SUBTEST << src.cols << 'x' << src.rows << "; " << type_name[j] << "; templ " << templ_size << 'x' << templ_size << "; CCORR";
+ using namespace perf;
+ using std::tr1::tuple;
+ using std::tr1::get;
- gen(templ, templ_size, templ_size, all_type[j], 0, 1);
-
- matchTemplate(src, templ, dst, TM_CCORR);
-
- CPU_ON;
- matchTemplate(src, templ, dst, TM_CCORR);
- CPU_OFF;
-
- ocl::oclMat d_src(src), d_templ(templ), d_dst;
-
- WARMUP_ON;
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR);
- WARMUP_OFF;
+ /////////// matchTemplate ////////////////////////
- GPU_ON;
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR);
- GPU_OFF;
+ typedef Size_MatType CV_TM_CCORRFixture;
- GPU_FULL_ON;
- d_src.upload(src);
- d_templ.upload(templ);
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR);
- d_dst.download(ocl_dst);
- GPU_FULL_OFF;
+ PERF_TEST_P(CV_TM_CCORRFixture, matchTemplate,
+ ::testing::Combine(::testing::Values(OCL_SIZE_1000, OCL_SIZE_2000),
+ OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
+ {
+ const Size_MatType_t params = GetParam();
+ const Size srcSize = get<0>(params), templSize(5, 5);
+ const int type = get<1>(params);
+
+ Mat src(srcSize, type), templ(templSize, type);
+ const Size dstSize(src.cols - templ.cols + 1, src.rows - templ.rows + 1);
+ Mat dst(dstSize, CV_32F);
+ randu(src, 0.0f, 1.0f);
+ randu(templ, 0.0f, 1.0f);
+ declare.time(srcSize == OCL_SIZE_2000 ? 20 : 6).in(src, templ).out(dst);
+
+ if (RUN_OCL_IMPL)
+ {
+ ocl::oclMat oclSrc(src), oclTempl(templ), oclDst(dstSize, CV_32F);
- TestSystem::instance().ExpectedMatNear(dst, ocl_dst, templ.rows * templ.cols * 1e-1);
- }
- }
- TEST_CYCLE() cv::ocl::matchTemplate(oclSrc, oclTempl, oclDst, CV_TM_CCORR);
++ TEST_CYCLE() cv::ocl::matchTemplate(oclSrc, oclTempl, oclDst, TM_CCORR);
- int all_type_8U[] = {CV_8UC1};
- std::string type_name_8U[] = {"CV_8UC1"};
+ oclDst.download(dst);
- for (size_t j = 0; j < sizeof(all_type_8U) / sizeof(int); j++)
- {
- for(templ_size = 5; templ_size <= 5; templ_size *= 5)
- {
- SUBTEST << src.cols << 'x' << src.rows << "; " << type_name_8U[j] << "; templ " << templ_size << 'x' << templ_size << "; CCORR_NORMED";
+ SANITY_CHECK(dst, 1e-4);
+ }
+ else if (RUN_PLAIN_IMPL)
+ {
- TEST_CYCLE() cv::matchTemplate(src, templ, dst, CV_TM_CCORR);
++ TEST_CYCLE() cv::matchTemplate(src, templ, dst, TM_CCORR);
- gen(src, size, size, all_type_8U[j], 0, 255);
+ SANITY_CHECK(dst, 1e-4);
+ }
+ else
+ OCL_PERF_ELSE
+ }
- gen(templ, templ_size, templ_size, all_type_8U[j], 0, 255);
+ typedef TestBaseWithParam<Size> CV_TM_CCORR_NORMEDFixture;
- matchTemplate(src, templ, dst, TM_CCORR_NORMED);
+ PERF_TEST_P(CV_TM_CCORR_NORMEDFixture, matchTemplate, OCL_TYPICAL_MAT_SIZES)
+ {
+ const Size srcSize = GetParam(), templSize(5, 5);
- CPU_ON;
- matchTemplate(src, templ, dst, TM_CCORR_NORMED);
- CPU_OFF;
+ Mat src(srcSize, CV_8UC1), templ(templSize, CV_8UC1), dst;
+ const Size dstSize(src.cols - templ.cols + 1, src.rows - templ.rows + 1);
+ dst.create(dstSize, CV_8UC1);
+ declare.in(src, templ, WARMUP_RNG).out(dst)
+ .time(srcSize == OCL_SIZE_2000 ? 10 : srcSize == OCL_SIZE_4000 ? 23 : 2);
- ocl::oclMat d_src(src);
- ocl::oclMat d_templ(templ), d_dst;
+ if (RUN_OCL_IMPL)
+ {
+ ocl::oclMat oclSrc(src), oclTempl(templ), oclDst(dstSize, CV_8UC1);
- WARMUP_ON;
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR_NORMED);
- WARMUP_OFF;
- TEST_CYCLE() cv::ocl::matchTemplate(oclSrc, oclTempl, oclDst, CV_TM_CCORR_NORMED);
++ TEST_CYCLE() cv::ocl::matchTemplate(oclSrc, oclTempl, oclDst, TM_CCORR_NORMED);
- GPU_ON;
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR_NORMED);
- GPU_OFF;
+ oclDst.download(dst);
- GPU_FULL_ON;
- d_src.upload(src);
- d_templ.upload(templ);
- ocl::matchTemplate(d_src, d_templ, d_dst, TM_CCORR_NORMED);
- d_dst.download(ocl_dst);
- GPU_FULL_OFF;
+ SANITY_CHECK(dst, 2e-2);
+ }
+ else if (RUN_PLAIN_IMPL)
+ {
- TEST_CYCLE() cv::matchTemplate(src, templ, dst, CV_TM_CCORR_NORMED);
++ TEST_CYCLE() cv::matchTemplate(src, templ, dst, TM_CCORR_NORMED);
- TestSystem::instance().ExpectedMatNear(dst, ocl_dst, templ.rows * templ.cols * 1e-1);
- }
- }
+ SANITY_CHECK(dst, 2e-2);
}
+ else
+ OCL_PERF_ELSE
}
#include <cstdio>
#include <vector>
#include <numeric>
-#include "opencv2/core/core.hpp"
-#include "opencv2/imgproc/imgproc.hpp"
-#include "opencv2/highgui/highgui.hpp"
-#include "opencv2/calib3d/calib3d.hpp"
-#include "opencv2/video/video.hpp"
-#include "opencv2/objdetect/objdetect.hpp"
-#include "opencv2/features2d/features2d.hpp"
-#include "opencv2/ocl/ocl.hpp"
-#include "opencv2/ts/ts.hpp"
+
- #include "opencv2/ts/ts_perf.hpp"
- #include "opencv2/ts/ts_gtest.h"
-
- #include "opencv2/core/utility.hpp"
-
- #define Min_Size 1000
- #define Max_Size 4000
- #define Multiple 2
- #define TAB " "
+#include "opencv2/core.hpp"
++#include "opencv2/core/utility.hpp"
+#include "opencv2/imgproc.hpp"
+#include "opencv2/highgui.hpp"
+#include "opencv2/calib3d.hpp"
+#include "opencv2/video.hpp"
+#include "opencv2/objdetect.hpp"
+#include "opencv2/features2d.hpp"
+#include "opencv2/ocl.hpp"
+#include "opencv2/ts.hpp"
using namespace std;
using namespace cv;
return;
}
- CV_Assert((x.type() == y.type() && x.size() == y.size() && x.depth() == CV_32F) || x.depth() == CV_64F);
+ CV_Assert(x.depth() == CV_32F || x.depth() == CV_64F);
y.create(x.size(), x.type());
- string kernelName = "arithm_pow";
+ String kernelName = "arithm_pow";
arithmetic_pow_run(x, p, y, kernelName, &arithm_pow);
}