.sw[a-z]
.*.swp
tags
+build/
+Thumbs.db
+ *.autosave
--- /dev/null
- 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)
+/*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 cv { namespace gpu { namespace cudev
+{
+ // kernels
+
+ template <typename T> __global__ void resize_nearest(const PtrStep<T> 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;
+
+ if (dst_x < dst.cols && dst_y < dst.rows)
+ {
+ const float src_x = dst_x * fx;
+ const float src_y = dst_y * fy;
+
+ dst(dst_y, dst_x) = src(__float2int_rz(src_y), __float2int_rz(src_x));
+ }
+ }
+
+ 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;
+
+ if (dst_x < dst.cols && dst_y < dst.rows)
+ {
+ const float src_x = dst_x * fx;
+ const float src_y = dst_y * fy;
+
+ dst(dst_y, dst_x) = src(src_y, src_x);
+ }
+ }
+
+ 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)
+ {
+ dst(y, x) = src(y, x);
+ }
+ }
+
+ // textures
+
+ 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); \
+ } \
+ };
+
+ 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>
- 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 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)
+ {
+ TextureAccessor<T> texSrc = texAccessor(src, 0, 0);
+ LinearFilter< TextureAccessor<T> > filteredSrc(texSrc);
+
+ 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() );
+ }
+
+ 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)
+ {
+ TextureAccessor<T> texSrc = texAccessor(src, 0, 0);
+ CubicFilter< TextureAccessor<T> > filteredSrc(texSrc);
+
+ 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);
+ CubicFilter< BorderReader<TextureAccessor<T>, BrdReplicate<T> > > filteredSrc(brdSrc);
+
+ resize<<<grid, block>>>(filteredSrc, dst, fy, fx);
+ }
+
+ cudaSafeCall( cudaGetLastError() );
+
+ cudaSafeCall( cudaDeviceSynchronize() );
+ }
+
+ // 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)
+ {
+ 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
+ {
+ 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 <> 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> {};
+
+ 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)
+ {
+ 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
+ {
+ 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 <> 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)
+ {
+ call_resize_cubic_glob(src, dst, fy, fx, 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 <> struct ResizeCubicDispatcher<uchar> : SelectImplForCubic<uchar> {};
+ template <> struct ResizeCubicDispatcher<uchar4> : SelectImplForCubic<uchar4> {};
+
+ template <> struct ResizeCubicDispatcher<ushort> : SelectImplForCubic<ushort> {};
+ template <> struct ResizeCubicDispatcher<ushort4> : SelectImplForCubic<ushort4> {};
+
+ template <> struct ResizeCubicDispatcher<short> : SelectImplForCubic<short> {};
+ template <> struct ResizeCubicDispatcher<short4> : SelectImplForCubic<short4> {};
+
+ template <> struct ResizeCubicDispatcher<float> : SelectImplForCubic<float> {};
+ template <> struct ResizeCubicDispatcher<float4> : SelectImplForCubic<float4> {};
+
+ // ResizeAreaDispatcher
+
+ 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);
+
+ const dim3 block(32, 8);
+ const dim3 grid(divUp(dst.cols, block.x), divUp(dst.rows, block.y));
+
+ 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] =
+ {
+ 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
+/*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*/
+
+#ifndef __OPENCV_IMGPROC_HPP__
+#define __OPENCV_IMGPROC_HPP__
+
+#include "opencv2/core.hpp"
+
+/*! \namespace cv
+ Namespace where all the C++ OpenCV functionality resides
+ */
+namespace cv
+{
+
+//! type of the kernel
+enum { KERNEL_GENERAL = 0, // the kernel is generic. No any type of symmetry or other properties.
+ KERNEL_SYMMETRICAL = 1, // kernel[i] == kernel[ksize-i-1] , and the anchor is at the center
+ KERNEL_ASYMMETRICAL = 2, // kernel[i] == -kernel[ksize-i-1] , and the anchor is at the center
+ KERNEL_SMOOTH = 4, // all the kernel elements are non-negative and summed to 1
+ KERNEL_INTEGER = 8 // all the kernel coefficients are integer numbers
+ };
+
+//! type of morphological operation
+enum { MORPH_ERODE = 0,
+ MORPH_DILATE = 1,
+ MORPH_OPEN = 2,
+ MORPH_CLOSE = 3,
+ MORPH_GRADIENT = 4,
+ MORPH_TOPHAT = 5,
+ MORPH_BLACKHAT = 6
+ };
+
+//! shape of the structuring element
+enum { MORPH_RECT = 0,
+ MORPH_CROSS = 1,
+ MORPH_ELLIPSE = 2
+ };
+
+//! interpolation algorithm
+enum { INTER_NEAREST = 0, //!< nearest neighbor interpolation
+ INTER_LINEAR = 1, //!< bilinear interpolation
+ INTER_CUBIC = 2, //!< bicubic interpolation
+ INTER_AREA = 3, //!< area-based (or super) interpolation
+ INTER_LANCZOS4 = 4, //!< Lanczos interpolation over 8x8 neighborhood
+
+ INTER_MAX = 7, //!< mask for interpolation codes
+ WARP_INVERSE_MAP = 16
+ };
+
+enum { INTER_BITS = 5,
+ INTER_BITS2 = INTER_BITS * 2,
+ INTER_TAB_SIZE = 1 << INTER_BITS,
+ INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE
+ };
+
+//! Distance types for Distance Transform and M-estimators
+enum { DIST_USER = -1, // User defined distance
+ DIST_L1 = 1, // distance = |x1-x2| + |y1-y2|
+ DIST_L2 = 2, // the simple euclidean distance
+ DIST_C = 3, // distance = max(|x1-x2|,|y1-y2|)
+ DIST_L12 = 4, // L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
+ DIST_FAIR = 5, // distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998
+ DIST_WELSCH = 6, // distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846
+ DIST_HUBER = 7 // distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
+};
+
+//! Mask size for distance transform
+enum { DIST_MASK_3 = 3,
+ DIST_MASK_5 = 5,
+ DIST_MASK_PRECISE = 0
+ };
+
+//! type of the threshold operation
+enum { THRESH_BINARY = 0, // value = value > threshold ? max_value : 0
+ THRESH_BINARY_INV = 1, // value = value > threshold ? 0 : max_value
+ THRESH_TRUNC = 2, // value = value > threshold ? threshold : value
+ THRESH_TOZERO = 3, // value = value > threshold ? value : 0
+ THRESH_TOZERO_INV = 4, // value = value > threshold ? 0 : value
+ THRESH_MASK = 7,
+ THRESH_OTSU = 8 // use Otsu algorithm to choose the optimal threshold value
+ };
+
+//! adaptive threshold algorithm
+enum { ADAPTIVE_THRESH_MEAN_C = 0,
+ ADAPTIVE_THRESH_GAUSSIAN_C = 1
+ };
+
+enum { PROJ_SPHERICAL_ORTHO = 0,
+ PROJ_SPHERICAL_EQRECT = 1
+ };
+
+//! class of the pixel in GrabCut algorithm
+enum { GC_BGD = 0, //!< background
+ GC_FGD = 1, //!< foreground
+ GC_PR_BGD = 2, //!< most probably background
+ GC_PR_FGD = 3 //!< most probably foreground
+ };
+
+//! GrabCut algorithm flags
+enum { GC_INIT_WITH_RECT = 0,
+ GC_INIT_WITH_MASK = 1,
+ GC_EVAL = 2
+};
+
+//! distanceTransform algorithm flags
+enum { DIST_LABEL_CCOMP = 0,
+ DIST_LABEL_PIXEL = 1
+ };
+
+//! floodfill algorithm flags
+enum { FLOODFILL_FIXED_RANGE = 1 << 16,
+ FLOODFILL_MASK_ONLY = 1 << 17
+ };
+
+//! type of the template matching operation
+enum { TM_SQDIFF = 0,
+ TM_SQDIFF_NORMED = 1,
+ TM_CCORR = 2,
+ TM_CCORR_NORMED = 3,
+ TM_CCOEFF = 4,
+ TM_CCOEFF_NORMED = 5
+ };
+
+//! connected components algorithm output formats
+enum { CC_STAT_LEFT = 0,
+ CC_STAT_TOP = 1,
+ CC_STAT_WIDTH = 2,
+ CC_STAT_HEIGHT = 3,
+ CC_STAT_AREA = 4,
+ CC_STAT_MAX = 5
+ };
+
+//! mode of the contour retrieval algorithm
+enum { RETR_EXTERNAL = 0, //!< retrieve only the most external (top-level) contours
+ RETR_LIST = 1, //!< retrieve all the contours without any hierarchical information
+ RETR_CCOMP = 2, //!< retrieve the connected components (that can possibly be nested)
+ RETR_TREE = 3, //!< retrieve all the contours and the whole hierarchy
+ RETR_FLOODFILL = 4
+ };
+
+//! the contour approximation algorithm
+enum { CHAIN_APPROX_NONE = 1,
+ CHAIN_APPROX_SIMPLE = 2,
+ CHAIN_APPROX_TC89_L1 = 3,
+ CHAIN_APPROX_TC89_KCOS = 4
+ };
+
+//! Variants of a Hough transform
+enum { HOUGH_STANDARD = 0,
+ HOUGH_PROBABILISTIC = 1,
+ HOUGH_MULTI_SCALE = 2,
+ HOUGH_GRADIENT = 3
+ };
+
+//! Variants of Line Segment Detector
+enum { LSD_REFINE_NONE = 0,
+ LSD_REFINE_STD = 1,
+ LSD_REFINE_ADV = 2
+ };
+
+//! Histogram comparison methods
+enum { HISTCMP_CORREL = 0,
+ HISTCMP_CHISQR = 1,
+ HISTCMP_INTERSECT = 2,
+ HISTCMP_BHATTACHARYYA = 3,
+ HISTCMP_HELLINGER = HISTCMP_BHATTACHARYYA
+ };
+
+//! the color conversion code
+enum { COLOR_BGR2BGRA = 0,
+ COLOR_RGB2RGBA = COLOR_BGR2BGRA,
+
+ COLOR_BGRA2BGR = 1,
+ COLOR_RGBA2RGB = COLOR_BGRA2BGR,
+
+ COLOR_BGR2RGBA = 2,
+ COLOR_RGB2BGRA = COLOR_BGR2RGBA,
+
+ COLOR_RGBA2BGR = 3,
+ COLOR_BGRA2RGB = COLOR_RGBA2BGR,
+
+ COLOR_BGR2RGB = 4,
+ COLOR_RGB2BGR = COLOR_BGR2RGB,
+
+ COLOR_BGRA2RGBA = 5,
+ COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,
+
+ COLOR_BGR2GRAY = 6,
+ COLOR_RGB2GRAY = 7,
+ COLOR_GRAY2BGR = 8,
+ COLOR_GRAY2RGB = COLOR_GRAY2BGR,
+ COLOR_GRAY2BGRA = 9,
+ COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
+ COLOR_BGRA2GRAY = 10,
+ COLOR_RGBA2GRAY = 11,
+
+ COLOR_BGR2BGR565 = 12,
+ COLOR_RGB2BGR565 = 13,
+ COLOR_BGR5652BGR = 14,
+ COLOR_BGR5652RGB = 15,
+ COLOR_BGRA2BGR565 = 16,
+ COLOR_RGBA2BGR565 = 17,
+ COLOR_BGR5652BGRA = 18,
+ COLOR_BGR5652RGBA = 19,
+
+ COLOR_GRAY2BGR565 = 20,
+ COLOR_BGR5652GRAY = 21,
+
+ COLOR_BGR2BGR555 = 22,
+ COLOR_RGB2BGR555 = 23,
+ COLOR_BGR5552BGR = 24,
+ COLOR_BGR5552RGB = 25,
+ COLOR_BGRA2BGR555 = 26,
+ COLOR_RGBA2BGR555 = 27,
+ COLOR_BGR5552BGRA = 28,
+ COLOR_BGR5552RGBA = 29,
+
+ COLOR_GRAY2BGR555 = 30,
+ COLOR_BGR5552GRAY = 31,
+
+ COLOR_BGR2XYZ = 32,
+ COLOR_RGB2XYZ = 33,
+ COLOR_XYZ2BGR = 34,
+ COLOR_XYZ2RGB = 35,
+
+ COLOR_BGR2YCrCb = 36,
+ COLOR_RGB2YCrCb = 37,
+ COLOR_YCrCb2BGR = 38,
+ COLOR_YCrCb2RGB = 39,
+
+ COLOR_BGR2HSV = 40,
+ COLOR_RGB2HSV = 41,
+
+ COLOR_BGR2Lab = 44,
+ COLOR_RGB2Lab = 45,
+
+ COLOR_BGR2Luv = 50,
+ COLOR_RGB2Luv = 51,
+ COLOR_BGR2HLS = 52,
+ COLOR_RGB2HLS = 53,
+
+ COLOR_HSV2BGR = 54,
+ COLOR_HSV2RGB = 55,
+
+ COLOR_Lab2BGR = 56,
+ COLOR_Lab2RGB = 57,
+ COLOR_Luv2BGR = 58,
+ COLOR_Luv2RGB = 59,
+ COLOR_HLS2BGR = 60,
+ COLOR_HLS2RGB = 61,
+
+ COLOR_BGR2HSV_FULL = 66,
+ COLOR_RGB2HSV_FULL = 67,
+ COLOR_BGR2HLS_FULL = 68,
+ COLOR_RGB2HLS_FULL = 69,
+
+ COLOR_HSV2BGR_FULL = 70,
+ COLOR_HSV2RGB_FULL = 71,
+ COLOR_HLS2BGR_FULL = 72,
+ COLOR_HLS2RGB_FULL = 73,
+
+ COLOR_LBGR2Lab = 74,
+ COLOR_LRGB2Lab = 75,
+ COLOR_LBGR2Luv = 76,
+ COLOR_LRGB2Luv = 77,
+
+ COLOR_Lab2LBGR = 78,
+ COLOR_Lab2LRGB = 79,
+ COLOR_Luv2LBGR = 80,
+ COLOR_Luv2LRGB = 81,
+
+ COLOR_BGR2YUV = 82,
+ COLOR_RGB2YUV = 83,
+ COLOR_YUV2BGR = 84,
+ COLOR_YUV2RGB = 85,
+
+ // YUV 4:2:0 family to RGB
+ COLOR_YUV2RGB_NV12 = 90,
+ COLOR_YUV2BGR_NV12 = 91,
+ COLOR_YUV2RGB_NV21 = 92,
+ COLOR_YUV2BGR_NV21 = 93,
+ COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
+ COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
+
+ COLOR_YUV2RGBA_NV12 = 94,
+ COLOR_YUV2BGRA_NV12 = 95,
+ COLOR_YUV2RGBA_NV21 = 96,
+ COLOR_YUV2BGRA_NV21 = 97,
+ COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
+ COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
+
+ COLOR_YUV2RGB_YV12 = 98,
+ COLOR_YUV2BGR_YV12 = 99,
+ COLOR_YUV2RGB_IYUV = 100,
+ COLOR_YUV2BGR_IYUV = 101,
+ COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
+ COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
+ COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
+ COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
+
+ COLOR_YUV2RGBA_YV12 = 102,
+ COLOR_YUV2BGRA_YV12 = 103,
+ COLOR_YUV2RGBA_IYUV = 104,
+ COLOR_YUV2BGRA_IYUV = 105,
+ COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
+ COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
+ COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
+ COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
+
+ COLOR_YUV2GRAY_420 = 106,
+ COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
+ COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
+ COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
+ COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
+ COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
+ COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
+ COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
+
+ // YUV 4:2:2 family to RGB
+ COLOR_YUV2RGB_UYVY = 107,
+ COLOR_YUV2BGR_UYVY = 108,
+ //COLOR_YUV2RGB_VYUY = 109,
+ //COLOR_YUV2BGR_VYUY = 110,
+ COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
+ COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
+ COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
+ COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
+
+ COLOR_YUV2RGBA_UYVY = 111,
+ COLOR_YUV2BGRA_UYVY = 112,
+ //COLOR_YUV2RGBA_VYUY = 113,
+ //COLOR_YUV2BGRA_VYUY = 114,
+ COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
+ COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
+ COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
+ COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
+
+ COLOR_YUV2RGB_YUY2 = 115,
+ COLOR_YUV2BGR_YUY2 = 116,
+ COLOR_YUV2RGB_YVYU = 117,
+ COLOR_YUV2BGR_YVYU = 118,
+ COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
+ COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
+ COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
+ COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
+
+ COLOR_YUV2RGBA_YUY2 = 119,
+ COLOR_YUV2BGRA_YUY2 = 120,
+ COLOR_YUV2RGBA_YVYU = 121,
+ COLOR_YUV2BGRA_YVYU = 122,
+ COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
+ COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
+ COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
+ COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
+
+ COLOR_YUV2GRAY_UYVY = 123,
+ COLOR_YUV2GRAY_YUY2 = 124,
+ //CV_YUV2GRAY_VYUY = CV_YUV2GRAY_UYVY,
+ COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
+ COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
+ COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
+ COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
+ COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
+
+ // alpha premultiplication
+ COLOR_RGBA2mRGBA = 125,
+ COLOR_mRGBA2RGBA = 126,
+
+ // RGB to YUV 4:2:0 family
+ COLOR_RGB2YUV_I420 = 127,
+ COLOR_BGR2YUV_I420 = 128,
+ COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
+ COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
+
+ COLOR_RGBA2YUV_I420 = 129,
+ COLOR_BGRA2YUV_I420 = 130,
+ COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
+ COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
+ COLOR_RGB2YUV_YV12 = 131,
+ COLOR_BGR2YUV_YV12 = 132,
+ COLOR_RGBA2YUV_YV12 = 133,
+ COLOR_BGRA2YUV_YV12 = 134,
+
+ // Demosaicing
+ COLOR_BayerBG2BGR = 46,
+ COLOR_BayerGB2BGR = 47,
+ COLOR_BayerRG2BGR = 48,
+ COLOR_BayerGR2BGR = 49,
+
+ COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
+ COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
+ COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
+ COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,
+
+ COLOR_BayerBG2GRAY = 86,
+ COLOR_BayerGB2GRAY = 87,
+ COLOR_BayerRG2GRAY = 88,
+ COLOR_BayerGR2GRAY = 89,
+
+ // Demosaicing using Variable Number of Gradients
+ COLOR_BayerBG2BGR_VNG = 62,
+ COLOR_BayerGB2BGR_VNG = 63,
+ COLOR_BayerRG2BGR_VNG = 64,
+ COLOR_BayerGR2BGR_VNG = 65,
+
+ COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
+ COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
+ COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
+ COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,
+
+ // Edge-Aware Demosaicing
+ COLOR_BayerBG2BGR_EA = 135,
+ COLOR_BayerGB2BGR_EA = 136,
+ COLOR_BayerRG2BGR_EA = 137,
+ COLOR_BayerGR2BGR_EA = 138,
+
+ COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
+ COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
+ COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
+ COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,
+
+
+ COLOR_COLORCVT_MAX = 139
+};
+
+
+
+/*!
+ The Base Class for 1D or Row-wise Filters
+
+ This is the base class for linear or non-linear filters that process 1D data.
+ In particular, such filters are used for the "horizontal" filtering parts in separable filters.
+
+ Several functions in OpenCV return Ptr<BaseRowFilter> for the specific types of filters,
+ and those pointers can be used directly or within cv::FilterEngine.
+*/
+class CV_EXPORTS BaseRowFilter
+{
+public:
+ //! the default constructor
+ BaseRowFilter();
+ //! the destructor
+ virtual ~BaseRowFilter();
+ //! the filtering operator. Must be overrided in the derived classes. The horizontal border interpolation is done outside of the class.
+ virtual void operator()(const uchar* src, uchar* dst, int width, int cn) = 0;
+
+ int ksize;
+ int anchor;
+};
+
+
+/*!
+ The Base Class for Column-wise Filters
+
+ This is the base class for linear or non-linear filters that process columns of 2D arrays.
+ Such filters are used for the "vertical" filtering parts in separable filters.
+
+ Several functions in OpenCV return Ptr<BaseColumnFilter> for the specific types of filters,
+ and those pointers can be used directly or within cv::FilterEngine.
+
+ Unlike cv::BaseRowFilter, cv::BaseColumnFilter may have some context information,
+ i.e. box filter keeps the sliding sum of elements. To reset the state BaseColumnFilter::reset()
+ must be called (e.g. the method is called by cv::FilterEngine)
+ */
+class CV_EXPORTS BaseColumnFilter
+{
+public:
+ //! the default constructor
+ BaseColumnFilter();
+ //! the destructor
+ virtual ~BaseColumnFilter();
+ //! the filtering operator. Must be overrided in the derived classes. The vertical border interpolation is done outside of the class.
+ virtual void operator()(const uchar** src, uchar* dst, int dststep, int dstcount, int width) = 0;
+ //! resets the internal buffers, if any
+ virtual void reset();
+
+ int ksize;
+ int anchor;
+};
+
+
+/*!
+ The Base Class for Non-Separable 2D Filters.
+
+ This is the base class for linear or non-linear 2D filters.
+
+ Several functions in OpenCV return Ptr<BaseFilter> for the specific types of filters,
+ and those pointers can be used directly or within cv::FilterEngine.
+
+ Similar to cv::BaseColumnFilter, the class may have some context information,
+ that should be reset using BaseFilter::reset() method before processing the new array.
+*/
+class CV_EXPORTS BaseFilter
+{
+public:
+ //! the default constructor
+ BaseFilter();
+ //! the destructor
+ virtual ~BaseFilter();
+ //! the filtering operator. The horizontal and the vertical border interpolation is done outside of the class.
+ virtual void operator()(const uchar** src, uchar* dst, int dststep, int dstcount, int width, int cn) = 0;
+ //! resets the internal buffers, if any
+ virtual void reset();
+
+ Size ksize;
+ Point anchor;
+};
+
+
+/*!
+ The Main Class for Image Filtering.
+
+ The class can be used to apply an arbitrary filtering operation to an image.
+ It contains all the necessary intermediate buffers, it computes extrapolated values
+ of the "virtual" pixels outside of the image etc.
+ Pointers to the initialized cv::FilterEngine instances
+ are returned by various OpenCV functions, such as cv::createSeparableLinearFilter(),
+ cv::createLinearFilter(), cv::createGaussianFilter(), cv::createDerivFilter(),
+ cv::createBoxFilter() and cv::createMorphologyFilter().
+
+ Using the class you can process large images by parts and build complex pipelines
+ that include filtering as some of the stages. If all you need is to apply some pre-defined
+ filtering operation, you may use cv::filter2D(), cv::erode(), cv::dilate() etc.
+ functions that create FilterEngine internally.
+
+ Here is the example on how to use the class to implement Laplacian operator, which is the sum of
+ second-order derivatives. More complex variant for different types is implemented in cv::Laplacian().
+
+ \code
+ void laplace_f(const Mat& src, Mat& dst)
+ {
+ CV_Assert( src.type() == CV_32F );
+ // make sure the destination array has the proper size and type
+ dst.create(src.size(), src.type());
+
+ // get the derivative and smooth kernels for d2I/dx2.
+ // for d2I/dy2 we could use the same kernels, just swapped
+ Mat kd, ks;
+ getSobelKernels( kd, ks, 2, 0, ksize, false, ktype );
+
+ // let's process 10 source rows at once
+ int DELTA = std::min(10, src.rows);
+ Ptr<FilterEngine> Fxx = createSeparableLinearFilter(src.type(),
+ dst.type(), kd, ks, Point(-1,-1), 0, borderType, borderType, Scalar() );
+ Ptr<FilterEngine> Fyy = createSeparableLinearFilter(src.type(),
+ dst.type(), ks, kd, Point(-1,-1), 0, borderType, borderType, Scalar() );
+
+ int y = Fxx->start(src), dsty = 0, dy = 0;
+ Fyy->start(src);
+ const uchar* sptr = src.data + y*src.step;
+
+ // allocate the buffers for the spatial image derivatives;
+ // the buffers need to have more than DELTA rows, because at the
+ // last iteration the output may take max(kd.rows-1,ks.rows-1)
+ // rows more than the input.
+ Mat Ixx( DELTA + kd.rows - 1, src.cols, dst.type() );
+ Mat Iyy( DELTA + kd.rows - 1, src.cols, dst.type() );
+
+ // inside the loop we always pass DELTA rows to the filter
+ // (note that the "proceed" method takes care of possibe overflow, since
+ // it was given the actual image height in the "start" method)
+ // on output we can get:
+ // * < DELTA rows (the initial buffer accumulation stage)
+ // * = DELTA rows (settled state in the middle)
+ // * > DELTA rows (then the input image is over, but we generate
+ // "virtual" rows using the border mode and filter them)
+ // this variable number of output rows is dy.
+ // dsty is the current output row.
+ // sptr is the pointer to the first input row in the portion to process
+ for( ; dsty < dst.rows; sptr += DELTA*src.step, dsty += dy )
+ {
+ Fxx->proceed( sptr, (int)src.step, DELTA, Ixx.data, (int)Ixx.step );
+ dy = Fyy->proceed( sptr, (int)src.step, DELTA, d2y.data, (int)Iyy.step );
+ if( dy > 0 )
+ {
+ Mat dstripe = dst.rowRange(dsty, dsty + dy);
+ add(Ixx.rowRange(0, dy), Iyy.rowRange(0, dy), dstripe);
+ }
+ }
+ }
+ \endcode
+*/
+class CV_EXPORTS FilterEngine
+{
+public:
+ //! the default constructor
+ FilterEngine();
+ //! the full constructor. Either _filter2D or both _rowFilter and _columnFilter must be non-empty.
+ FilterEngine(const Ptr<BaseFilter>& _filter2D,
+ const Ptr<BaseRowFilter>& _rowFilter,
+ const Ptr<BaseColumnFilter>& _columnFilter,
+ int srcType, int dstType, int bufType,
+ int _rowBorderType = BORDER_REPLICATE,
+ int _columnBorderType = -1,
+ const Scalar& _borderValue = Scalar());
+ //! the destructor
+ virtual ~FilterEngine();
+ //! reinitializes the engine. The previously assigned filters are released.
+ void init(const Ptr<BaseFilter>& _filter2D,
+ const Ptr<BaseRowFilter>& _rowFilter,
+ const Ptr<BaseColumnFilter>& _columnFilter,
+ int srcType, int dstType, int bufType,
+ int _rowBorderType = BORDER_REPLICATE,
+ int _columnBorderType = -1,
+ const Scalar& _borderValue = Scalar());
+ //! starts filtering of the specified ROI of an image of size wholeSize.
+ virtual int start(Size wholeSize, Rect roi, int maxBufRows = -1);
+ //! starts filtering of the specified ROI of the specified image.
+ virtual int start(const Mat& src, const Rect& srcRoi = Rect(0,0,-1,-1),
+ bool isolated = false, int maxBufRows = -1);
+ //! processes the next srcCount rows of the image.
+ virtual int proceed(const uchar* src, int srcStep, int srcCount,
+ uchar* dst, int dstStep);
+ //! applies filter to the specified ROI of the image. if srcRoi=(0,0,-1,-1), the whole image is filtered.
+ virtual void apply( const Mat& src, Mat& dst,
+ const Rect& srcRoi = Rect(0,0,-1,-1),
+ Point dstOfs = Point(0,0),
+ bool isolated = false);
+ //! returns true if the filter is separable
+ bool isSeparable() const { return !filter2D; }
+ //! returns the number
+ int remainingInputRows() const;
+ int remainingOutputRows() const;
+
+ int srcType;
+ int dstType;
+ int bufType;
+ Size ksize;
+ Point anchor;
+ int maxWidth;
+ Size wholeSize;
+ Rect roi;
+ int dx1;
+ int dx2;
+ int rowBorderType;
+ int columnBorderType;
+ std::vector<int> borderTab;
+ int borderElemSize;
+ std::vector<uchar> ringBuf;
+ std::vector<uchar> srcRow;
+ std::vector<uchar> constBorderValue;
+ std::vector<uchar> constBorderRow;
+ int bufStep;
+ int startY;
+ int startY0;
+ int endY;
+ int rowCount;
+ int dstY;
+ std::vector<uchar*> rows;
+
+ Ptr<BaseFilter> filter2D;
+ Ptr<BaseRowFilter> rowFilter;
+ Ptr<BaseColumnFilter> columnFilter;
+};
+
+
+//! finds arbitrary template in the grayscale image using Generalized Hough Transform
+class CV_EXPORTS GeneralizedHough : public Algorithm
+{
+public:
+ //! set template to search
+ virtual void setTemplate(InputArray templ, Point templCenter = Point(-1, -1)) = 0;
+ virtual void setTemplate(InputArray edges, InputArray dx, InputArray dy, Point templCenter = Point(-1, -1)) = 0;
+
+ //! find template on image
+ virtual void detect(InputArray image, OutputArray positions, OutputArray votes = noArray()) = 0;
+ virtual void detect(InputArray edges, InputArray dx, InputArray dy, OutputArray positions, OutputArray votes = noArray()) = 0;
+
+ //! Canny low threshold.
+ virtual void setCannyLowThresh(int cannyLowThresh) = 0;
+ virtual int getCannyLowThresh() const = 0;
+
+ //! Canny high threshold.
+ virtual void setCannyHighThresh(int cannyHighThresh) = 0;
+ virtual int getCannyHighThresh() const = 0;
+
+ //! Minimum distance between the centers of the detected objects.
+ virtual void setMinDist(double minDist) = 0;
+ virtual double getMinDist() const = 0;
+
+ //! Inverse ratio of the accumulator resolution to the image resolution.
+ virtual void setDp(double dp) = 0;
+ virtual double getDp() const = 0;
+
+ //! Maximal size of inner buffers.
+ virtual void setMaxBufferSize(int maxBufferSize) = 0;
+ virtual int getMaxBufferSize() const = 0;
+};
+
+//! Ballard, D.H. (1981). Generalizing the Hough transform to detect arbitrary shapes. Pattern Recognition 13 (2): 111-122.
+//! Detects position only without traslation and rotation
+class CV_EXPORTS GeneralizedHoughBallard : public GeneralizedHough
+{
+public:
+ //! R-Table levels.
+ virtual void setLevels(int levels) = 0;
+ virtual int getLevels() const = 0;
+
+ //! The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.
+ virtual void setVotesThreshold(int votesThreshold) = 0;
+ virtual int getVotesThreshold() const = 0;
+};
+
+//! Guil, N., González-Linares, J.M. and Zapata, E.L. (1999). Bidimensional shape detection using an invariant approach. Pattern Recognition 32 (6): 1025-1038.
+//! Detects position, traslation and rotation
+class CV_EXPORTS GeneralizedHoughGuil : public GeneralizedHough
+{
+public:
+ //! Angle difference in degrees between two points in feature.
+ virtual void setXi(double xi) = 0;
+ virtual double getXi() const = 0;
+
+ //! Feature table levels.
+ virtual void setLevels(int levels) = 0;
+ virtual int getLevels() const = 0;
+
+ //! Maximal difference between angles that treated as equal.
+ virtual void setAngleEpsilon(double angleEpsilon) = 0;
+ virtual double getAngleEpsilon() const = 0;
+
+ //! Minimal rotation angle to detect in degrees.
+ virtual void setMinAngle(double minAngle) = 0;
+ virtual double getMinAngle() const = 0;
+
+ //! Maximal rotation angle to detect in degrees.
+ virtual void setMaxAngle(double maxAngle) = 0;
+ virtual double getMaxAngle() const = 0;
+
+ //! Angle step in degrees.
+ virtual void setAngleStep(double angleStep) = 0;
+ virtual double getAngleStep() const = 0;
+
+ //! Angle votes threshold.
+ virtual void setAngleThresh(int angleThresh) = 0;
+ virtual int getAngleThresh() const = 0;
+
+ //! Minimal scale to detect.
+ virtual void setMinScale(double minScale) = 0;
+ virtual double getMinScale() const = 0;
+
+ //! Maximal scale to detect.
+ virtual void setMaxScale(double maxScale) = 0;
+ virtual double getMaxScale() const = 0;
+
+ //! Scale step.
+ virtual void setScaleStep(double scaleStep) = 0;
+ virtual double getScaleStep() const = 0;
+
+ //! Scale votes threshold.
+ virtual void setScaleThresh(int scaleThresh) = 0;
+ virtual int getScaleThresh() const = 0;
+
+ //! Position votes threshold.
+ virtual void setPosThresh(int posThresh) = 0;
+ virtual int getPosThresh() const = 0;
+};
+
+
+class CV_EXPORTS_W CLAHE : public Algorithm
+{
+public:
+ CV_WRAP virtual void apply(InputArray src, OutputArray dst) = 0;
+
+ CV_WRAP virtual void setClipLimit(double clipLimit) = 0;
+ CV_WRAP virtual double getClipLimit() const = 0;
+
+ CV_WRAP virtual void setTilesGridSize(Size tileGridSize) = 0;
+ CV_WRAP virtual Size getTilesGridSize() const = 0;
+
+ CV_WRAP virtual void collectGarbage() = 0;
+};
+
+
+class CV_EXPORTS_W Subdiv2D
+{
+public:
+ enum { PTLOC_ERROR = -2,
+ PTLOC_OUTSIDE_RECT = -1,
+ PTLOC_INSIDE = 0,
+ PTLOC_VERTEX = 1,
+ PTLOC_ON_EDGE = 2
+ };
+
+ enum { NEXT_AROUND_ORG = 0x00,
+ NEXT_AROUND_DST = 0x22,
+ PREV_AROUND_ORG = 0x11,
+ PREV_AROUND_DST = 0x33,
+ NEXT_AROUND_LEFT = 0x13,
+ NEXT_AROUND_RIGHT = 0x31,
+ PREV_AROUND_LEFT = 0x20,
+ PREV_AROUND_RIGHT = 0x02
+ };
+
+ CV_WRAP Subdiv2D();
+ CV_WRAP Subdiv2D(Rect rect);
+ CV_WRAP void initDelaunay(Rect rect);
+
+ CV_WRAP int insert(Point2f pt);
+ CV_WRAP void insert(const std::vector<Point2f>& ptvec);
+ CV_WRAP int locate(Point2f pt, CV_OUT int& edge, CV_OUT int& vertex);
+
+ CV_WRAP int findNearest(Point2f pt, CV_OUT Point2f* nearestPt = 0);
+ CV_WRAP void getEdgeList(CV_OUT std::vector<Vec4f>& edgeList) const;
+ CV_WRAP void getTriangleList(CV_OUT std::vector<Vec6f>& triangleList) const;
+ CV_WRAP void getVoronoiFacetList(const std::vector<int>& idx, CV_OUT std::vector<std::vector<Point2f> >& facetList,
+ CV_OUT std::vector<Point2f>& facetCenters);
+
+ CV_WRAP Point2f getVertex(int vertex, CV_OUT int* firstEdge = 0) const;
+
+ CV_WRAP int getEdge( int edge, int nextEdgeType ) const;
+ CV_WRAP int nextEdge(int edge) const;
+ CV_WRAP int rotateEdge(int edge, int rotate) const;
+ CV_WRAP int symEdge(int edge) const;
+ CV_WRAP int edgeOrg(int edge, CV_OUT Point2f* orgpt = 0) const;
+ CV_WRAP int edgeDst(int edge, CV_OUT Point2f* dstpt = 0) const;
+
+protected:
+ int newEdge();
+ void deleteEdge(int edge);
+ int newPoint(Point2f pt, bool isvirtual, int firstEdge = 0);
+ void deletePoint(int vtx);
+ void setEdgePoints( int edge, int orgPt, int dstPt );
+ void splice( int edgeA, int edgeB );
+ int connectEdges( int edgeA, int edgeB );
+ void swapEdges( int edge );
+ int isRightOf(Point2f pt, int edge) const;
+ void calcVoronoi();
+ void clearVoronoi();
+ void checkSubdiv() const;
+
+ struct CV_EXPORTS Vertex
+ {
+ Vertex();
+ Vertex(Point2f pt, bool _isvirtual, int _firstEdge=0);
+ bool isvirtual() const;
+ bool isfree() const;
+
+ int firstEdge;
+ int type;
+ Point2f pt;
+ };
+
+ struct CV_EXPORTS QuadEdge
+ {
+ QuadEdge();
+ QuadEdge(int edgeidx);
+ bool isfree() const;
+
+ int next[4];
+ int pt[4];
+ };
+
+ std::vector<Vertex> vtx;
+ std::vector<QuadEdge> qedges;
+ int freeQEdge;
+ int freePoint;
+ bool validGeometry;
+
+ int recentEdge;
+ Point2f topLeft;
+ Point2f bottomRight;
+};
+
+class LineSegmentDetector : public Algorithm
+{
+public:
+/**
+ * Detect lines in the input image with the specified ROI.
+ *
+ * @param _image A grayscale(CV_8UC1) input image.
+ * If only a roi needs to be selected, use
+ * lsd_ptr->detect(image(roi), ..., lines);
+ * lines += Scalar(roi.x, roi.y, roi.x, roi.y);
+ * @param _lines Return: A vector of Vec4i elements specifying the beginning and ending point of a line.
+ * Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end.
+ * Returned lines are strictly oriented depending on the gradient.
+ * @param _roi Return: ROI of the image, where lines are to be found. If specified, the returning
+ * lines coordinates are image wise.
+ * @param width Return: Vector of widths of the regions, where the lines are found. E.g. Width of line.
+ * @param prec Return: Vector of precisions with which the lines are found.
+ * @param nfa Return: Vector containing number of false alarms in the line region, with precision of 10%.
+ * The bigger the value, logarithmically better the detection.
+ * * -1 corresponds to 10 mean false alarms
+ * * 0 corresponds to 1 mean false alarm
+ * * 1 corresponds to 0.1 mean false alarms
+ * This vector will be calculated _only_ when the objects type is REFINE_ADV
+ */
+ virtual void detect(InputArray _image, OutputArray _lines,
+ OutputArray width = noArray(), OutputArray prec = noArray(),
+ OutputArray nfa = noArray()) = 0;
+
+/**
+ * Draw lines on the given canvas.
+ *
+ * @param image The image, where lines will be drawn.
+ * Should have the size of the image, where the lines were found
+ * @param lines The lines that need to be drawn
+ */
+ virtual void drawSegments(InputOutputArray image, InputArray lines) = 0;
+
+/**
+ * Draw both vectors on the image canvas. Uses blue for lines 1 and red for lines 2.
+ *
+ * @param image The image, where lines will be drawn.
+ * Should have the size of the image, where the lines were found
+ * @param lines1 The first lines that need to be drawn. Color - Blue.
+ * @param lines2 The second lines that need to be drawn. Color - Red.
+ * @return The number of mismatching pixels between lines1 and lines2.
+ */
+ virtual int compareSegments(const Size& size, InputArray lines1, InputArray lines2, Mat* image = 0) = 0;
+
+ virtual ~LineSegmentDetector() {};
+};
+
+//! Returns a pointer to a LineSegmentDetector class.
+CV_EXPORTS Ptr<LineSegmentDetector> createLineSegmentDetectorPtr(
+ int _refine = LSD_REFINE_STD, double _scale = 0.8,
+ double _sigma_scale = 0.6, double _quant = 2.0, double _ang_th = 22.5,
+ double _log_eps = 0, double _density_th = 0.7, int _n_bins = 1024);
+
+//! returns type (one of KERNEL_*) of 1D or 2D kernel specified by its coefficients.
+CV_EXPORTS int getKernelType(InputArray kernel, Point anchor);
+
+//! returns the primitive row filter with the specified kernel
+CV_EXPORTS Ptr<BaseRowFilter> getLinearRowFilter(int srcType, int bufType,
+ InputArray kernel, int anchor,
+ int symmetryType);
+
+//! returns the primitive column filter with the specified kernel
+CV_EXPORTS Ptr<BaseColumnFilter> getLinearColumnFilter(int bufType, int dstType,
+ InputArray kernel, int anchor,
+ int symmetryType, double delta = 0,
+ int bits = 0);
+
+//! returns 2D filter with the specified kernel
+CV_EXPORTS Ptr<BaseFilter> getLinearFilter(int srcType, int dstType,
+ InputArray kernel,
+ Point anchor = Point(-1,-1),
+ double delta = 0, int bits = 0);
+
+//! returns the separable linear filter engine
+CV_EXPORTS Ptr<FilterEngine> createSeparableLinearFilter(int srcType, int dstType,
+ InputArray rowKernel, InputArray columnKernel,
+ Point anchor = Point(-1,-1), double delta = 0,
+ int rowBorderType = BORDER_DEFAULT,
+ int columnBorderType = -1,
+ const Scalar& borderValue = Scalar());
+
+//! returns the non-separable linear filter engine
+CV_EXPORTS Ptr<FilterEngine> createLinearFilter(int srcType, int dstType,
+ InputArray kernel, Point _anchor = Point(-1,-1),
+ double delta = 0, int rowBorderType = BORDER_DEFAULT,
+ int columnBorderType = -1, const Scalar& borderValue = Scalar());
+
+//! returns the Gaussian kernel with the specified parameters
+CV_EXPORTS_W Mat getGaussianKernel( int ksize, double sigma, int ktype = CV_64F );
+
+//! returns the Gaussian filter engine
+CV_EXPORTS Ptr<FilterEngine> createGaussianFilter( int type, Size ksize,
+ double sigma1, double sigma2 = 0,
+ int borderType = BORDER_DEFAULT);
+
+//! initializes kernels of the generalized Sobel operator
+CV_EXPORTS_W void getDerivKernels( OutputArray kx, OutputArray ky,
+ int dx, int dy, int ksize,
+ bool normalize = false, int ktype = CV_32F );
+
+//! returns filter engine for the generalized Sobel operator
+CV_EXPORTS Ptr<FilterEngine> createDerivFilter( int srcType, int dstType,
+ int dx, int dy, int ksize,
+ int borderType = BORDER_DEFAULT );
+
+//! returns horizontal 1D box filter
+CV_EXPORTS Ptr<BaseRowFilter> getRowSumFilter(int srcType, int sumType,
+ int ksize, int anchor = -1);
+
+//! returns vertical 1D box filter
+CV_EXPORTS Ptr<BaseColumnFilter> getColumnSumFilter( int sumType, int dstType,
+ int ksize, int anchor = -1,
+ double scale = 1);
+//! returns box filter engine
+CV_EXPORTS Ptr<FilterEngine> createBoxFilter( int srcType, int dstType, Size ksize,
+ Point anchor = Point(-1,-1),
+ bool normalize = true,
+ int borderType = BORDER_DEFAULT);
+
+//! returns the Gabor kernel with the specified parameters
+CV_EXPORTS_W Mat getGaborKernel( Size ksize, double sigma, double theta, double lambd,
+ double gamma, double psi = CV_PI*0.5, int ktype = CV_64F );
+
+//! returns horizontal 1D morphological filter
+CV_EXPORTS Ptr<BaseRowFilter> getMorphologyRowFilter(int op, int type, int ksize, int anchor = -1);
+
+//! returns vertical 1D morphological filter
+CV_EXPORTS Ptr<BaseColumnFilter> getMorphologyColumnFilter(int op, int type, int ksize, int anchor = -1);
+
+//! returns 2D morphological filter
+CV_EXPORTS Ptr<BaseFilter> getMorphologyFilter(int op, int type, InputArray kernel,
+ Point anchor = Point(-1,-1));
+
+//! returns "magic" border value for erosion and dilation. It is automatically transformed to Scalar::all(-DBL_MAX) for dilation.
+static inline Scalar morphologyDefaultBorderValue() { return Scalar::all(DBL_MAX); }
+
+//! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
+CV_EXPORTS Ptr<FilterEngine> createMorphologyFilter(int op, int type, InputArray kernel,
+ Point anchor = Point(-1,-1), int rowBorderType = BORDER_CONSTANT,
+ int columnBorderType = -1, const Scalar& borderValue = morphologyDefaultBorderValue());
+
+//! returns structuring element of the specified shape and size
+CV_EXPORTS_W Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));
+
+//! smooths the image using median filter.
+CV_EXPORTS_W void medianBlur( InputArray src, OutputArray dst, int ksize );
+
+//! smooths the image using Gaussian filter.
+CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
+ double sigmaX, double sigmaY = 0,
+ int borderType = BORDER_DEFAULT );
+
+//! smooths the image using bilateral filter
+CV_EXPORTS_W void bilateralFilter( InputArray src, OutputArray dst, int d,
+ double sigmaColor, double sigmaSpace,
+ int borderType = BORDER_DEFAULT );
+
++//! smooths the image using adaptive bilateral filter
++CV_EXPORTS_W void adaptiveBilateralFilter( InputArray src, OutputArray dst, Size ksize,
++ double sigmaSpace, Point anchor=Point(-1, -1),
++ int borderType=BORDER_DEFAULT );
++
+//! smooths the image using the box filter. Each pixel is processed in O(1) time
+CV_EXPORTS_W void boxFilter( InputArray src, OutputArray dst, int ddepth,
+ Size ksize, Point anchor = Point(-1,-1),
+ bool normalize = true,
+ int borderType = BORDER_DEFAULT );
+
+//! a synonym for normalized box filter
+CV_EXPORTS_W void blur( InputArray src, OutputArray dst,
+ Size ksize, Point anchor = Point(-1,-1),
+ int borderType = BORDER_DEFAULT );
+
+//! applies non-separable 2D linear filter to the image
+CV_EXPORTS_W void filter2D( InputArray src, OutputArray dst, int ddepth,
+ InputArray kernel, Point anchor = Point(-1,-1),
+ double delta = 0, int borderType = BORDER_DEFAULT );
+
+//! applies separable 2D linear filter to the image
+CV_EXPORTS_W void sepFilter2D( InputArray src, OutputArray dst, int ddepth,
+ InputArray kernelX, InputArray kernelY,
+ Point anchor = Point(-1,-1),
+ double delta = 0, int borderType = BORDER_DEFAULT );
+
+//! applies generalized Sobel operator to the image
+CV_EXPORTS_W void Sobel( InputArray src, OutputArray dst, int ddepth,
+ int dx, int dy, int ksize = 3,
+ double scale = 1, double delta = 0,
+ int borderType = BORDER_DEFAULT );
+
+//! applies the vertical or horizontal Scharr operator to the image
+CV_EXPORTS_W void Scharr( InputArray src, OutputArray dst, int ddepth,
+ int dx, int dy, double scale = 1, double delta = 0,
+ int borderType = BORDER_DEFAULT );
+
+//! applies Laplacian operator to the image
+CV_EXPORTS_W void Laplacian( InputArray src, OutputArray dst, int ddepth,
+ int ksize = 1, double scale = 1, double delta = 0,
+ int borderType = BORDER_DEFAULT );
+
+//! applies Canny edge detector and produces the edge map.
+CV_EXPORTS_W void Canny( InputArray image, OutputArray edges,
+ double threshold1, double threshold2,
+ int apertureSize = 3, bool L2gradient = false );
+
+//! computes minimum eigen value of 2x2 derivative covariation matrix at each pixel - the cornerness criteria
+CV_EXPORTS_W void cornerMinEigenVal( InputArray src, OutputArray dst,
+ int blockSize, int ksize = 3,
+ int borderType = BORDER_DEFAULT );
+
+//! computes Harris cornerness criteria at each image pixel
+CV_EXPORTS_W void cornerHarris( InputArray src, OutputArray dst, int blockSize,
+ int ksize, double k,
+ int borderType = BORDER_DEFAULT );
+
+//! computes both eigenvalues and the eigenvectors of 2x2 derivative covariation matrix at each pixel. The output is stored as 6-channel matrix.
+CV_EXPORTS_W void cornerEigenValsAndVecs( InputArray src, OutputArray dst,
+ int blockSize, int ksize,
+ int borderType = BORDER_DEFAULT );
+
+//! computes another complex cornerness criteria at each pixel
+CV_EXPORTS_W void preCornerDetect( InputArray src, OutputArray dst, int ksize,
+ int borderType = BORDER_DEFAULT );
+
+//! adjusts the corner locations with sub-pixel accuracy to maximize the certain cornerness criteria
+CV_EXPORTS_W void cornerSubPix( InputArray image, InputOutputArray corners,
+ Size winSize, Size zeroZone,
+ TermCriteria criteria );
+
+//! finds the strong enough corners where the cornerMinEigenVal() or cornerHarris() report the local maxima
+CV_EXPORTS_W void goodFeaturesToTrack( InputArray image, OutputArray corners,
+ int maxCorners, double qualityLevel, double minDistance,
+ InputArray mask = noArray(), int blockSize = 3,
+ bool useHarrisDetector = false, double k = 0.04 );
+
+//! finds lines in the black-n-white image using the standard or pyramid Hough transform
+CV_EXPORTS_W void HoughLines( InputArray image, OutputArray lines,
+ double rho, double theta, int threshold,
+ double srn = 0, double stn = 0 );
+
+//! finds line segments in the black-n-white image using probabalistic Hough transform
+CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines,
+ double rho, double theta, int threshold,
+ double minLineLength = 0, double maxLineGap = 0 );
+
+//! finds circles in the grayscale image using 2+1 gradient Hough transform
+CV_EXPORTS_W void HoughCircles( InputArray image, OutputArray circles,
+ int method, double dp, double minDist,
+ double param1 = 100, double param2 = 100,
+ int minRadius = 0, int maxRadius = 0 );
+
+//! erodes the image (applies the local minimum operator)
+CV_EXPORTS_W void erode( InputArray src, OutputArray dst, InputArray kernel,
+ Point anchor = Point(-1,-1), int iterations = 1,
+ int borderType = BORDER_CONSTANT,
+ const Scalar& borderValue = morphologyDefaultBorderValue() );
+
+//! dilates the image (applies the local maximum operator)
+CV_EXPORTS_W void dilate( InputArray src, OutputArray dst, InputArray kernel,
+ Point anchor = Point(-1,-1), int iterations = 1,
+ int borderType = BORDER_CONSTANT,
+ const Scalar& borderValue = morphologyDefaultBorderValue() );
+
+//! applies an advanced morphological operation to the image
+CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst,
+ int op, InputArray kernel,
+ Point anchor = Point(-1,-1), int iterations = 1,
+ int borderType = BORDER_CONSTANT,
+ const Scalar& borderValue = morphologyDefaultBorderValue() );
+
+//! resizes the image
+CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
+ Size dsize, double fx = 0, double fy = 0,
+ int interpolation = INTER_LINEAR );
+
+//! warps the image using affine transformation
+CV_EXPORTS_W void warpAffine( InputArray src, OutputArray dst,
+ InputArray M, Size dsize,
+ int flags = INTER_LINEAR,
+ int borderMode = BORDER_CONSTANT,
+ const Scalar& borderValue = Scalar());
+
+//! warps the image using perspective transformation
+CV_EXPORTS_W void warpPerspective( InputArray src, OutputArray dst,
+ InputArray M, Size dsize,
+ int flags = INTER_LINEAR,
+ int borderMode = BORDER_CONSTANT,
+ const Scalar& borderValue = Scalar());
+
+//! warps the image using the precomputed maps. The maps are stored in either floating-point or integer fixed-point format
+CV_EXPORTS_W void remap( InputArray src, OutputArray dst,
+ InputArray map1, InputArray map2,
+ int interpolation, int borderMode = BORDER_CONSTANT,
+ const Scalar& borderValue = Scalar());
+
+//! converts maps for remap from floating-point to fixed-point format or backwards
+CV_EXPORTS_W void convertMaps( InputArray map1, InputArray map2,
+ OutputArray dstmap1, OutputArray dstmap2,
+ int dstmap1type, bool nninterpolation = false );
+
+//! returns 2x3 affine transformation matrix for the planar rotation.
+CV_EXPORTS_W Mat getRotationMatrix2D( Point2f center, double angle, double scale );
+
+//! returns 3x3 perspective transformation for the corresponding 4 point pairs.
+CV_EXPORTS Mat getPerspectiveTransform( const Point2f src[], const Point2f dst[] );
+
+//! returns 2x3 affine transformation for the corresponding 3 point pairs.
+CV_EXPORTS Mat getAffineTransform( const Point2f src[], const Point2f dst[] );
+
+//! computes 2x3 affine transformation matrix that is inverse to the specified 2x3 affine transformation.
+CV_EXPORTS_W void invertAffineTransform( InputArray M, OutputArray iM );
+
+CV_EXPORTS_W Mat getPerspectiveTransform( InputArray src, InputArray dst );
+
+CV_EXPORTS_W Mat getAffineTransform( InputArray src, InputArray dst );
+
+//! extracts rectangle from the image at sub-pixel location
+CV_EXPORTS_W void getRectSubPix( InputArray image, Size patchSize,
+ Point2f center, OutputArray patch, int patchType = -1 );
+
+//! computes the integral image
+CV_EXPORTS_W void integral( InputArray src, OutputArray sum, int sdepth = -1 );
+
+//! computes the integral image and integral for the squared image
+CV_EXPORTS_AS(integral2) void integral( InputArray src, OutputArray sum,
+ OutputArray sqsum, int sdepth = -1 );
+
+//! computes the integral image, integral for the squared image and the tilted integral image
+CV_EXPORTS_AS(integral3) void integral( InputArray src, OutputArray sum,
+ OutputArray sqsum, OutputArray tilted,
+ int sdepth = -1 );
+
+//! adds image to the accumulator (dst += src). Unlike cv::add, dst and src can have different types.
+CV_EXPORTS_W void accumulate( InputArray src, InputOutputArray dst,
+ InputArray mask = noArray() );
+
+//! adds squared src image to the accumulator (dst += src*src).
+CV_EXPORTS_W void accumulateSquare( InputArray src, InputOutputArray dst,
+ InputArray mask = noArray() );
+//! adds product of the 2 images to the accumulator (dst += src1*src2).
+CV_EXPORTS_W void accumulateProduct( InputArray src1, InputArray src2,
+ InputOutputArray dst, InputArray mask=noArray() );
+
+//! updates the running average (dst = dst*(1-alpha) + src*alpha)
+CV_EXPORTS_W void accumulateWeighted( InputArray src, InputOutputArray dst,
+ double alpha, InputArray mask = noArray() );
+
+CV_EXPORTS_W Point2d phaseCorrelate(InputArray src1, InputArray src2,
+ InputArray window = noArray(), CV_OUT double* response = 0);
+
+CV_EXPORTS_W void createHanningWindow(OutputArray dst, Size winSize, int type);
+
+//! applies fixed threshold to the image
+CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,
+ double thresh, double maxval, int type );
+
+
+//! applies variable (adaptive) threshold to the image
+CV_EXPORTS_W void adaptiveThreshold( InputArray src, OutputArray dst,
+ double maxValue, int adaptiveMethod,
+ int thresholdType, int blockSize, double C );
+
+//! smooths and downsamples the image
+CV_EXPORTS_W void pyrDown( InputArray src, OutputArray dst,
+ const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
+
+//! upsamples and smoothes the image
+CV_EXPORTS_W void pyrUp( InputArray src, OutputArray dst,
+ const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
+
+//! builds the gaussian pyramid using pyrDown() as a basic operation
+CV_EXPORTS void buildPyramid( InputArray src, OutputArrayOfArrays dst,
+ int maxlevel, int borderType = BORDER_DEFAULT );
+
+//! corrects lens distortion for the given camera matrix and distortion coefficients
+CV_EXPORTS_W void undistort( InputArray src, OutputArray dst,
+ InputArray cameraMatrix,
+ InputArray distCoeffs,
+ InputArray newCameraMatrix = noArray() );
+
+//! initializes maps for cv::remap() to correct lens distortion and optionally rectify the image
+CV_EXPORTS_W void initUndistortRectifyMap( InputArray cameraMatrix, InputArray distCoeffs,
+ InputArray R, InputArray newCameraMatrix,
+ Size size, int m1type, OutputArray map1, OutputArray map2 );
+
+//! initializes maps for cv::remap() for wide-angle
+CV_EXPORTS_W float initWideAngleProjMap( InputArray cameraMatrix, InputArray distCoeffs,
+ Size imageSize, int destImageWidth,
+ int m1type, OutputArray map1, OutputArray map2,
+ int projType = PROJ_SPHERICAL_EQRECT, double alpha = 0);
+
+//! returns the default new camera matrix (by default it is the same as cameraMatrix unless centerPricipalPoint=true)
+CV_EXPORTS_W Mat getDefaultNewCameraMatrix( InputArray cameraMatrix, Size imgsize = Size(),
+ bool centerPrincipalPoint = false );
+
+//! returns points' coordinates after lens distortion correction
+CV_EXPORTS_W void undistortPoints( InputArray src, OutputArray dst,
+ InputArray cameraMatrix, InputArray distCoeffs,
+ InputArray R = noArray(), InputArray P = noArray());
+
+//! computes the joint dense histogram for a set of images.
+CV_EXPORTS void calcHist( const Mat* images, int nimages,
+ const int* channels, InputArray mask,
+ OutputArray hist, int dims, const int* histSize,
+ const float** ranges, bool uniform = true, bool accumulate = false );
+
+//! computes the joint sparse histogram for a set of images.
+CV_EXPORTS void calcHist( const Mat* images, int nimages,
+ const int* channels, InputArray mask,
+ SparseMat& hist, int dims,
+ const int* histSize, const float** ranges,
+ bool uniform = true, bool accumulate = false );
+
+CV_EXPORTS_W void calcHist( InputArrayOfArrays images,
+ const std::vector<int>& channels,
+ InputArray mask, OutputArray hist,
+ const std::vector<int>& histSize,
+ const std::vector<float>& ranges,
+ bool accumulate = false );
+
+//! computes back projection for the set of images
+CV_EXPORTS void calcBackProject( const Mat* images, int nimages,
+ const int* channels, InputArray hist,
+ OutputArray backProject, const float** ranges,
+ double scale = 1, bool uniform = true );
+
+//! computes back projection for the set of images
+CV_EXPORTS void calcBackProject( const Mat* images, int nimages,
+ const int* channels, const SparseMat& hist,
+ OutputArray backProject, const float** ranges,
+ double scale = 1, bool uniform = true );
+
+CV_EXPORTS_W void calcBackProject( InputArrayOfArrays images, const std::vector<int>& channels,
+ InputArray hist, OutputArray dst,
+ const std::vector<float>& ranges,
+ double scale );
+
+//! compares two histograms stored in dense arrays
+CV_EXPORTS_W double compareHist( InputArray H1, InputArray H2, int method );
+
+//! compares two histograms stored in sparse arrays
+CV_EXPORTS double compareHist( const SparseMat& H1, const SparseMat& H2, int method );
+
+//! normalizes the grayscale image brightness and contrast by normalizing its histogram
+CV_EXPORTS_W void equalizeHist( InputArray src, OutputArray dst );
+
+CV_EXPORTS float EMD( InputArray signature1, InputArray signature2,
+ int distType, InputArray cost=noArray(),
+ float* lowerBound = 0, OutputArray flow = noArray() );
+
+//! segments the image using watershed algorithm
+CV_EXPORTS_W void watershed( InputArray image, InputOutputArray markers );
+
+//! filters image using meanshift algorithm
+CV_EXPORTS_W void pyrMeanShiftFiltering( InputArray src, OutputArray dst,
+ double sp, double sr, int maxLevel = 1,
+ TermCriteria termcrit=TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,5,1) );
+
+//! segments the image using GrabCut algorithm
+CV_EXPORTS_W void grabCut( InputArray img, InputOutputArray mask, Rect rect,
+ InputOutputArray bgdModel, InputOutputArray fgdModel,
+ int iterCount, int mode = GC_EVAL );
+
+
+//! builds the discrete Voronoi diagram
+CV_EXPORTS_AS(distanceTransformWithLabels) void distanceTransform( InputArray src, OutputArray dst,
+ OutputArray labels, int distanceType, int maskSize,
+ int labelType = DIST_LABEL_CCOMP );
+
+//! computes the distance transform map
+CV_EXPORTS_W void distanceTransform( InputArray src, OutputArray dst,
+ int distanceType, int maskSize );
+
+
+//! fills the semi-uniform image region starting from the specified seed point
+CV_EXPORTS int floodFill( InputOutputArray image,
+ Point seedPoint, Scalar newVal, CV_OUT Rect* rect = 0,
+ Scalar loDiff = Scalar(), Scalar upDiff = Scalar(),
+ int flags = 4 );
+
+//! fills the semi-uniform image region and/or the mask starting from the specified seed point
+CV_EXPORTS_W int floodFill( InputOutputArray image, InputOutputArray mask,
+ Point seedPoint, Scalar newVal, CV_OUT Rect* rect=0,
+ Scalar loDiff = Scalar(), Scalar upDiff = Scalar(),
+ int flags = 4 );
+
+//! converts image from one color space to another
+CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );
+
+// main function for all demosaicing procceses
+CV_EXPORTS_W void demosaicing(InputArray _src, OutputArray _dst, int code, int dcn = 0);
+
+//! computes moments of the rasterized shape or a vector of points
+CV_EXPORTS_W Moments moments( InputArray array, bool binaryImage = false );
+
+//! computes 7 Hu invariants from the moments
+CV_EXPORTS void HuMoments( const Moments& moments, double hu[7] );
+
+CV_EXPORTS_W void HuMoments( const Moments& m, OutputArray hu );
+
+//! computes the proximity map for the raster template and the image where the template is searched for
+CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ,
+ OutputArray result, int method );
+
+
+// computes the connected components labeled image of boolean image ``image``
+// with 4 or 8 way connectivity - returns N, the total
+// number of labels [0, N-1] where 0 represents the background label.
+// ltype specifies the output label image type, an important
+// consideration based on the total number of labels or
+// alternatively the total number of pixels in the source image.
+CV_EXPORTS_W int connectedComponents(InputArray image, OutputArray labels,
+ int connectivity = 8, int ltype = CV_32S);
+
+CV_EXPORTS_W int connectedComponentsWithStats(InputArray image, OutputArray labels,
+ OutputArray stats, OutputArray centroids,
+ int connectivity = 8, int ltype = CV_32S);
+
+
+//! retrieves contours and the hierarchical information from black-n-white image.
+CV_EXPORTS_W void findContours( InputOutputArray image, OutputArrayOfArrays contours,
+ OutputArray hierarchy, int mode,
+ int method, Point offset = Point());
+
+//! retrieves contours from black-n-white image.
+CV_EXPORTS void findContours( InputOutputArray image, OutputArrayOfArrays contours,
+ int mode, int method, Point offset = Point());
+
+//! approximates contour or a curve using Douglas-Peucker algorithm
+CV_EXPORTS_W void approxPolyDP( InputArray curve,
+ OutputArray approxCurve,
+ double epsilon, bool closed );
+
+//! computes the contour perimeter (closed=true) or a curve length
+CV_EXPORTS_W double arcLength( InputArray curve, bool closed );
+
+//! computes the bounding rectangle for a contour
+CV_EXPORTS_W Rect boundingRect( InputArray points );
+
+//! computes the contour area
+CV_EXPORTS_W double contourArea( InputArray contour, bool oriented = false );
+
+//! computes the minimal rotated rectangle for a set of points
+CV_EXPORTS_W RotatedRect minAreaRect( InputArray points );
+
+//! computes boxpoints
+CV_EXPORTS_W void boxPoints(RotatedRect box, OutputArray points);
+
+//! computes the minimal enclosing circle for a set of points
+CV_EXPORTS_W void minEnclosingCircle( InputArray points,
+ CV_OUT Point2f& center, CV_OUT float& radius );
+
+//! matches two contours using one of the available algorithms
+CV_EXPORTS_W double matchShapes( InputArray contour1, InputArray contour2,
+ int method, double parameter );
+
+//! computes convex hull for a set of 2D points.
+CV_EXPORTS_W void convexHull( InputArray points, OutputArray hull,
+ bool clockwise = false, bool returnPoints = true );
+
+//! computes the contour convexity defects
+CV_EXPORTS_W void convexityDefects( InputArray contour, InputArray convexhull, OutputArray convexityDefects );
+
+//! returns true if the contour is convex. Does not support contours with self-intersection
+CV_EXPORTS_W bool isContourConvex( InputArray contour );
+
+//! finds intersection of two convex polygons
+CV_EXPORTS_W float intersectConvexConvex( InputArray _p1, InputArray _p2,
+ OutputArray _p12, bool handleNested = true );
+
+//! fits ellipse to the set of 2D points
+CV_EXPORTS_W RotatedRect fitEllipse( InputArray points );
+
+//! fits line to the set of 2D points using M-estimator algorithm
+CV_EXPORTS_W void fitLine( InputArray points, OutputArray line, int distType,
+ double param, double reps, double aeps );
+
+//! checks if the point is inside the contour. Optionally computes the signed distance from the point to the contour boundary
+CV_EXPORTS_W double pointPolygonTest( InputArray contour, Point2f pt, bool measureDist );
+
+CV_EXPORTS_W Ptr<CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
+
+//! Ballard, D.H. (1981). Generalizing the Hough transform to detect arbitrary shapes. Pattern Recognition 13 (2): 111-122.
+//! Detects position only without traslation and rotation
+CV_EXPORTS Ptr<GeneralizedHoughBallard> createGeneralizedHoughBallard();
+
+//! Guil, N., González-Linares, J.M. and Zapata, E.L. (1999). Bidimensional shape detection using an invariant approach. Pattern Recognition 32 (6): 1025-1038.
+//! Detects position, traslation and rotation
+CV_EXPORTS Ptr<GeneralizedHoughGuil> createGeneralizedHoughGuil();
+
+} // cv
+
+#endif
radius = MAX(radius, 1);
d = radius*2 + 1;
- #if 0 && defined HAVE_IPP && (IPP_VERSION_MAJOR >= 7)
- if(cn == 1)
+ Mat temp;
+ copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
+
+ #if defined HAVE_IPP && (IPP_VERSION_MAJOR >= 7)
+ if( cn == 1 )
{
- IppiSize kernel = {d, d};
- IppiSize roi={src.cols, src.rows};
- int bufsize=0;
- ippiFilterBilateralGetBufSize_8u_C1R( ippiFilterBilateralGauss, roi, kernel, &bufsize);
- AutoBuffer<uchar> buf(bufsize+128);
- IppiFilterBilateralSpec *pSpec = (IppiFilterBilateralSpec *)alignPtr(&buf[0], 32);
- ippiFilterBilateralInit_8u_C1R( ippiFilterBilateralGauss, kernel, sigma_color*sigma_color, sigma_space*sigma_space, 1, pSpec );
- Mat tsrc;
- const Mat* psrc = &src;
- if( src.data == dst.data )
- {
- src.copyTo(tsrc);
- psrc = &tsrc;
- }
- if( ippiFilterBilateral_8u_C1R(psrc->data, (int)psrc->step[0],
- dst.data, (int)dst.step[0],
- roi, kernel, pSpec) >= 0 )
- return;
+ bool ok;
+ IPPBilateralFilter_8u_Invoker body(temp, dst, sigma_color * sigma_color, sigma_space * sigma_space, radius, &ok );
+ parallel_for_(Range(0, dst.rows), body, dst.total()/(double)(1<<16));
+ if( ok ) return;
}
#endif
- Mat temp;
- copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
- vector<float> _color_weight(cn*256);
- vector<float> _space_weight(d*d);
- vector<int> _space_ofs(d*d);
+ std::vector<float> _color_weight(cn*256);
+ std::vector<float> _space_weight(d*d);
+ std::vector<int> _space_ofs(d*d);
float* color_weight = &_color_weight[0];
float* space_weight = &_space_weight[0];
int* space_ofs = &_space_ofs[0];
"Bilateral filtering is only implemented for 8u and 32f images" );
}
- vector<float> space_weight;
+
+ /****************************************************************************************\
+ Adaptive Bilateral Filtering
+ \****************************************************************************************/
+
+ namespace cv
+ {
+ #define CALCVAR 1
+ #define FIXED_WEIGHT 0
+
+ class adaptiveBilateralFilter_8u_Invoker :
+ public ParallelLoopBody
+ {
+ public:
+ adaptiveBilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, Size _ksize, double _sigma_space, Point _anchor) :
+ temp(&_temp), dest(&_dest), ksize(_ksize), sigma_space(_sigma_space), anchor(_anchor)
+ {
+ if( sigma_space <= 0 )
+ sigma_space = 1;
+ CV_Assert((ksize.width & 1) && (ksize.height & 1));
+ space_weight.resize(ksize.width * ksize.height);
+ double sigma2 = sigma_space * sigma_space;
+ int idx = 0;
+ int w = ksize.width / 2;
+ int h = ksize.height / 2;
+ for(int y=-h; y<=h; y++)
+ for(int x=-w; x<=w; x++)
+ {
+ space_weight[idx++] = (float)(sigma2 / (sigma2 + x * x + y * y));
+ }
+ }
+ virtual void operator()(const Range& range) const
+ {
+ int cn = dest->channels();
+ int anX = anchor.x;
+
+ const uchar *tptr;
+
+ for(int i = range.start;i < range.end; i++)
+ {
+ int startY = i;
+ if(cn == 1)
+ {
+ float var;
+ int currVal;
+ int sumVal = 0;
+ int sumValSqr = 0;
+ int currValCenter;
+ int currWRTCenter;
+ float weight;
+ float totalWeight = 0.;
+ float tmpSum = 0.;
+
+ for(int j = 0;j < dest->cols *cn; j+=cn)
+ {
+ sumVal = 0;
+ sumValSqr= 0;
+ totalWeight = 0.;
+ tmpSum = 0.;
+
+ // Top row: don't sum the very last element
+ int startLMJ = 0;
+ int endLMJ = ksize.width - 1;
+ int howManyAll = (anX *2 +1)*(ksize.width );
+ #if CALCVAR
+ for(int x = startLMJ; x< endLMJ; x++)
+ {
+ tptr = temp->ptr(startY + x) +j;
+ for(int y=-anX; y<=anX; y++)
+ {
+ currVal = tptr[cn*(y+anX)];
+ sumVal += currVal;
+ sumValSqr += (currVal *currVal);
+ }
+ }
+ var = ( (sumValSqr * howManyAll)- sumVal * sumVal ) / ( (float)(howManyAll*howManyAll));
+ #else
+ var = 900.0;
+ #endif
+ startLMJ = 0;
+ endLMJ = ksize.width;
+ tptr = temp->ptr(startY + (startLMJ+ endLMJ)/2);
+ currValCenter =tptr[j+cn*anX];
+ for(int x = startLMJ; x< endLMJ; x++)
+ {
+ tptr = temp->ptr(startY + x) +j;
+ for(int y=-anX; y<=anX; y++)
+ {
+ #if FIXED_WEIGHT
+ weight = 1.0;
+ #else
+ currVal = tptr[cn*(y+anX)];
+ currWRTCenter = currVal - currValCenter;
+
+ weight = var / ( var + (currWRTCenter * currWRTCenter) ) * space_weight[x*ksize.width+y+anX];;
+ #endif
+ tmpSum += ((float)tptr[cn*(y+anX)] * weight);
+ totalWeight += weight;
+ }
+ }
+ tmpSum /= totalWeight;
+
+ dest->at<uchar>(startY ,j)= static_cast<uchar>(tmpSum);
+ }
+ }
+ else
+ {
+ assert(cn == 3);
+ float var_b, var_g, var_r;
+ int currVal_b, currVal_g, currVal_r;
+ int sumVal_b= 0, sumVal_g= 0, sumVal_r= 0;
+ int sumValSqr_b= 0, sumValSqr_g= 0, sumValSqr_r= 0;
+ int currValCenter_b= 0, currValCenter_g= 0, currValCenter_r= 0;
+ int currWRTCenter_b, currWRTCenter_g, currWRTCenter_r;
+ float weight_b, weight_g, weight_r;
+ float totalWeight_b= 0., totalWeight_g= 0., totalWeight_r= 0.;
+ float tmpSum_b = 0., tmpSum_g= 0., tmpSum_r = 0.;
+
+ for(int j = 0;j < dest->cols *cn; j+=cn)
+ {
+ sumVal_b= 0, sumVal_g= 0, sumVal_r= 0;
+ sumValSqr_b= 0, sumValSqr_g= 0, sumValSqr_r= 0;
+ totalWeight_b= 0., totalWeight_g= 0., totalWeight_r= 0.;
+ tmpSum_b = 0., tmpSum_g= 0., tmpSum_r = 0.;
+
+ // Top row: don't sum the very last element
+ int startLMJ = 0;
+ int endLMJ = ksize.width - 1;
+ int howManyAll = (anX *2 +1)*(ksize.width);
+ #if CALCVAR
+ for(int x = startLMJ; x< endLMJ; x++)
+ {
+ tptr = temp->ptr(startY + x) +j;
+ for(int y=-anX; y<=anX; y++)
+ {
+ currVal_b = tptr[cn*(y+anX)], currVal_g = tptr[cn*(y+anX)+1], currVal_r =tptr[cn*(y+anX)+2];
+ sumVal_b += currVal_b;
+ sumVal_g += currVal_g;
+ sumVal_r += currVal_r;
+ sumValSqr_b += (currVal_b *currVal_b);
+ sumValSqr_g += (currVal_g *currVal_g);
+ sumValSqr_r += (currVal_r *currVal_r);
+ }
+ }
+ var_b = ( (sumValSqr_b * howManyAll)- sumVal_b * sumVal_b ) / ( (float)(howManyAll*howManyAll));
+ var_g = ( (sumValSqr_g * howManyAll)- sumVal_g * sumVal_g ) / ( (float)(howManyAll*howManyAll));
+ var_r = ( (sumValSqr_r * howManyAll)- sumVal_r * sumVal_r ) / ( (float)(howManyAll*howManyAll));
+ #else
+ var_b = 900.0; var_g = 900.0;var_r = 900.0;
+ #endif
+ startLMJ = 0;
+ endLMJ = ksize.width;
+ tptr = temp->ptr(startY + (startLMJ+ endLMJ)/2) + j;
+ currValCenter_b =tptr[cn*anX], currValCenter_g =tptr[cn*anX+1], currValCenter_r =tptr[cn*anX+2];
+ for(int x = startLMJ; x< endLMJ; x++)
+ {
+ tptr = temp->ptr(startY + x) +j;
+ for(int y=-anX; y<=anX; y++)
+ {
+ #if FIXED_WEIGHT
+ weight_b = 1.0;
+ weight_g = 1.0;
+ weight_r = 1.0;
+ #else
+ currVal_b = tptr[cn*(y+anX)];currVal_g=tptr[cn*(y+anX)+1];currVal_r=tptr[cn*(y+anX)+2];
+ currWRTCenter_b = currVal_b - currValCenter_b;
+ currWRTCenter_g = currVal_g - currValCenter_g;
+ currWRTCenter_r = currVal_r - currValCenter_r;
+
+ float cur_spw = space_weight[x*ksize.width+y+anX];
+ weight_b = var_b / ( var_b + (currWRTCenter_b * currWRTCenter_b) ) * cur_spw;
+ weight_g = var_g / ( var_g + (currWRTCenter_g * currWRTCenter_g) ) * cur_spw;
+ weight_r = var_r / ( var_r + (currWRTCenter_r * currWRTCenter_r) ) * cur_spw;
+ #endif
+ tmpSum_b += ((float)tptr[cn*(y+anX)] * weight_b);
+ tmpSum_g += ((float)tptr[cn*(y+anX)+1] * weight_g);
+ tmpSum_r += ((float)tptr[cn*(y+anX)+2] * weight_r);
+ totalWeight_b += weight_b, totalWeight_g += weight_g, totalWeight_r += weight_r;
+ }
+ }
+ tmpSum_b /= totalWeight_b;
+ tmpSum_g /= totalWeight_g;
+ tmpSum_r /= totalWeight_r;
+
+ dest->at<uchar>(startY,j )= static_cast<uchar>(tmpSum_b);
+ dest->at<uchar>(startY,j+1)= static_cast<uchar>(tmpSum_g);
+ dest->at<uchar>(startY,j+2)= static_cast<uchar>(tmpSum_r);
+ }
+ }
+ }
+ }
+ private:
+ const Mat *temp;
+ Mat *dest;
+ Size ksize;
+ double sigma_space;
+ Point anchor;
++ std::vector<float> space_weight;
+ };
+ static void adaptiveBilateralFilter_8u( const Mat& src, Mat& dst, Size ksize, double sigmaSpace, Point anchor, int borderType )
+ {
+ Size size = src.size();
+
+ CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) &&
+ src.type() == dst.type() && src.size() == dst.size() &&
+ src.data != dst.data );
+ Mat temp;
+ copyMakeBorder(src, temp, anchor.x, anchor.y, anchor.x, anchor.y, borderType);
+
+ adaptiveBilateralFilter_8u_Invoker body(dst, temp, ksize, sigmaSpace, anchor);
+ parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
+ }
+ }
+ void cv::adaptiveBilateralFilter( InputArray _src, OutputArray _dst, Size ksize,
+ double sigmaSpace, Point anchor, int borderType )
+ {
+ Mat src = _src.getMat();
+ _dst.create(src.size(), src.type());
+ Mat dst = _dst.getMat();
+
+ CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3);
+
+ anchor = normalizeAnchor(anchor,ksize);
+ if( src.depth() == CV_8U )
+ adaptiveBilateralFilter_8u( src, dst, ksize, sigmaSpace, anchor, borderType );
+ else
+ CV_Error( CV_StsUnsupportedFormat,
+ "Adaptive Bilateral filtering is only implemented for 8u images" );
+ }
+
//////////////////////////////////////////////////////////////////////////////////////////
CV_IMPL void
--- /dev/null
- CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpave, int borderType=BORDER_DEFAULT);
+/*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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
+// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
+// Copyright (C) 2010-2012, Multicoreware, 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 oclMaterials 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*/
+
+#ifndef __OPENCV_OCL_HPP__
+#define __OPENCV_OCL_HPP__
+
+#include <memory>
+#include <vector>
+
+#include "opencv2/core.hpp"
+#include "opencv2/imgproc.hpp"
+#include "opencv2/objdetect.hpp"
+
+namespace cv
+{
+ namespace ocl
+ {
+ enum
+ {
+ CVCL_DEVICE_TYPE_DEFAULT = (1 << 0),
+ CVCL_DEVICE_TYPE_CPU = (1 << 1),
+ CVCL_DEVICE_TYPE_GPU = (1 << 2),
+ CVCL_DEVICE_TYPE_ACCELERATOR = (1 << 3),
+ //CVCL_DEVICE_TYPE_CUSTOM = (1 << 4)
+ CVCL_DEVICE_TYPE_ALL = 0xFFFFFFFF
+ };
+
+ enum DevMemRW
+ {
+ DEVICE_MEM_R_W = 0,
+ DEVICE_MEM_R_ONLY,
+ DEVICE_MEM_W_ONLY
+ };
+
+ enum DevMemType
+ {
+ DEVICE_MEM_DEFAULT = 0,
+ DEVICE_MEM_AHP, //alloc host pointer
+ DEVICE_MEM_UHP, //use host pointer
+ DEVICE_MEM_CHP, //copy host pointer
+ DEVICE_MEM_PM //persistent memory
+ };
+
+ //Get the global device memory and read/write type
+ //return 1 if unified memory system supported, otherwise return 0
+ CV_EXPORTS int getDevMemType(DevMemRW& rw_type, DevMemType& mem_type);
+
+ //Set the global device memory and read/write type,
+ //the newly generated oclMat will all use this type
+ //return -1 if the target type is unsupported, otherwise return 0
+ CV_EXPORTS int setDevMemType(DevMemRW rw_type = DEVICE_MEM_R_W, DevMemType mem_type = DEVICE_MEM_DEFAULT);
+
+ //this class contains ocl runtime information
+ class CV_EXPORTS Info
+ {
+ public:
+ struct Impl;
+ Impl *impl;
+
+ Info();
+ Info(const Info &m);
+ ~Info();
+ void release();
+ Info &operator = (const Info &m);
+ std::vector<String> DeviceName;
+ String PlatformName;
+ };
+ //////////////////////////////// Initialization & Info ////////////////////////
+ //this function may be obsoleted
+ //CV_EXPORTS cl_device_id getDevice();
+ //the function must be called before any other cv::ocl::functions, it initialize ocl runtime
+ //each Info relates to an OpenCL platform
+ //there is one or more devices in each platform, each one has a separate name
+ CV_EXPORTS int getDevice(std::vector<Info> &oclinfo, int devicetype = CVCL_DEVICE_TYPE_GPU);
+
+ //set device you want to use, optional function after getDevice be called
+ //the devnum is the index of the selected device in DeviceName vector of INfo
+ CV_EXPORTS void setDevice(Info &oclinfo, int devnum = 0);
+
+ //The two functions below enable other opencl program to use ocl module's cl_context and cl_command_queue
+ //returns cl_context *
+ CV_EXPORTS void* getoclContext();
+ //returns cl_command_queue *
+ CV_EXPORTS void* getoclCommandQueue();
+
+ //explicit call clFinish. The global command queue will be used.
+ CV_EXPORTS void finish();
+
+ //this function enable ocl module to use customized cl_context and cl_command_queue
+ //getDevice also need to be called before this function
+ CV_EXPORTS void setDeviceEx(Info &oclinfo, void *ctx, void *qu, int devnum = 0);
+
+ //returns true when global OpenCL context is initialized
+ CV_EXPORTS bool initialized();
+
+ //////////////////////////////// OpenCL context ////////////////////////
+ //This is a global singleton class used to represent a OpenCL context.
+ class CV_EXPORTS Context
+ {
+ protected:
+ Context();
+ friend class std::auto_ptr<Context>;
+ friend bool initialized();
+ private:
+ static std::auto_ptr<Context> clCxt;
+ static int val;
+ public:
+ ~Context();
+ void release();
+ Info::Impl* impl;
+
+ static Context *getContext();
+ static void setContext(Info &oclinfo);
+
+ enum {CL_DOUBLE, CL_UNIFIED_MEM, CL_VER_1_2};
+ bool supportsFeature(int ftype);
+ size_t computeUnits();
+ size_t maxWorkGroupSize();
+ void* oclContext();
+ void* oclCommandQueue();
+ };
+
+ //! Calls a kernel, by string. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
+ CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
+ const char **source, String kernelName,
+ size_t globalThreads[3], size_t localThreads[3],
+ std::vector< std::pair<size_t, const void *> > &args,
+ int channels, int depth, const char *build_options,
+ bool finish = true, bool measureKernelTime = false,
+ bool cleanUp = true);
+
+ //! Calls a kernel, by file. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
+ CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
+ const char **fileName, const int numFiles, String kernelName,
+ size_t globalThreads[3], size_t localThreads[3],
+ std::vector< std::pair<size_t, const void *> > &args,
+ int channels, int depth, const char *build_options,
+ bool finish = true, bool measureKernelTime = false,
+ bool cleanUp = true);
+
+ //! Enable or disable OpenCL program binary caching onto local disk
+ // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
+ // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
+ // binary file, which will be reused when the OpenCV executable is started again.
+ //
+ // Caching mode is controlled by the following enums
+ // Notes
+ // 1. the feature is by default enabled when OpenCV is built in release mode.
+ // 2. the CACHE_DEBUG / CACHE_RELEASE flags only effectively work with MSVC compiler;
+ // for GNU compilers, the function always treats the build as release mode (enabled by default).
+ enum
+ {
+ CACHE_NONE = 0, // do not cache OpenCL binary
+ CACHE_DEBUG = 0x1 << 0, // cache OpenCL binary when built in debug mode (only work with MSVC)
+ CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode (only work with MSVC)
+ CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE, // always cache opencl binary
+ CACHE_UPDATE = 0x1 << 2 // if the binary cache file with the same name is already on the disk, it will be updated.
+ };
+ CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
+
+ //! set where binary cache to be saved to
+ CV_EXPORTS void setBinpath(const char *path);
+
+ class CV_EXPORTS oclMatExpr;
+ //////////////////////////////// oclMat ////////////////////////////////
+ class CV_EXPORTS oclMat
+ {
+ public:
+ //! default constructor
+ oclMat();
+ //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
+ oclMat(int rows, int cols, int type);
+ oclMat(Size size, int type);
+ //! constucts oclMatrix and fills it with the specified value _s.
+ oclMat(int rows, int cols, int type, const Scalar &s);
+ oclMat(Size size, int type, const Scalar &s);
+ //! copy constructor
+ oclMat(const oclMat &m);
+
+ //! constructor for oclMatrix headers pointing to user-allocated data
+ oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
+ oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
+
+ //! creates a matrix header for a part of the bigger matrix
+ oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
+ oclMat(const oclMat &m, const Rect &roi);
+
+ //! builds oclMat from Mat. Perfom blocking upload to device.
+ explicit oclMat (const Mat &m);
+
+ //! destructor - calls release()
+ ~oclMat();
+
+ //! assignment operators
+ oclMat &operator = (const oclMat &m);
+ //! assignment operator. Perfom blocking upload to device.
+ oclMat &operator = (const Mat &m);
+ oclMat &operator = (const oclMatExpr& expr);
+
+ //! pefroms blocking upload data to oclMat.
+ void upload(const cv::Mat &m);
+
+
+ //! downloads data from device to host memory. Blocking calls.
+ operator Mat() const;
+ void download(cv::Mat &m) const;
+
+ //! convert to _InputArray
+ operator _InputArray();
+
+ //! convert to _OutputArray
+ operator _OutputArray();
+
+ //! returns a new oclMatrix header for the specified row
+ oclMat row(int y) const;
+ //! returns a new oclMatrix header for the specified column
+ oclMat col(int x) const;
+ //! ... for the specified row span
+ oclMat rowRange(int startrow, int endrow) const;
+ oclMat rowRange(const Range &r) const;
+ //! ... for the specified column span
+ oclMat colRange(int startcol, int endcol) const;
+ oclMat colRange(const Range &r) const;
+
+ //! returns deep copy of the oclMatrix, i.e. the data is copied
+ oclMat clone() const;
+ //! copies the oclMatrix content to "m".
+ // It calls m.create(this->size(), this->type()).
+ // It supports any data type
+ void copyTo( oclMat &m ) const;
+ //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
+ //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
+ void copyTo( oclMat &m, const oclMat &mask ) const;
+ //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
+ //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
+ void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
+
+ void assignTo( oclMat &m, int type = -1 ) const;
+
+ //! sets every oclMatrix element to s
+ //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
+ oclMat& operator = (const Scalar &s);
+ //! sets some of the oclMatrix elements to s, according to the mask
+ //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
+ oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
+ //! creates alternative oclMatrix header for the same data, with different
+ // number of channels and/or different number of rows. see cvReshape.
+ oclMat reshape(int cn, int rows = 0) const;
+
+ //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
+ // previous data is unreferenced if needed.
+ void create(int rows, int cols, int type);
+ void create(Size size, int type);
+
+ //! allocates new oclMatrix with specified device memory type.
+ void createEx(int rows, int cols, int type,
+ DevMemRW rw_type, DevMemType mem_type, void* hptr = 0);
+ void createEx(Size size, int type, DevMemRW rw_type,
+ DevMemType mem_type, void* hptr = 0);
+
+ //! decreases reference counter;
+ // deallocate the data when reference counter reaches 0.
+ void release();
+
+ //! swaps with other smart pointer
+ void swap(oclMat &mat);
+
+ //! locates oclMatrix header within a parent oclMatrix. See below
+ void locateROI( Size &wholeSize, Point &ofs ) const;
+ //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
+ oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
+ //! extracts a rectangular sub-oclMatrix
+ // (this is a generalized form of row, rowRange etc.)
+ oclMat operator()( Range rowRange, Range colRange ) const;
+ oclMat operator()( const Rect &roi ) const;
+
+ oclMat& operator+=( const oclMat& m );
+ oclMat& operator-=( const oclMat& m );
+ oclMat& operator*=( const oclMat& m );
+ oclMat& operator/=( const oclMat& m );
+
+ //! returns true if the oclMatrix data is continuous
+ // (i.e. when there are no gaps between successive rows).
+ // similar to CV_IS_oclMat_CONT(cvoclMat->type)
+ bool isContinuous() const;
+ //! returns element size in bytes,
+ // similar to CV_ELEM_SIZE(cvMat->type)
+ size_t elemSize() const;
+ //! returns the size of element channel in bytes.
+ size_t elemSize1() const;
+ //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
+ int type() const;
+ //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
+ //! 3 channels element actually use 4 channel space
+ int ocltype() const;
+ //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
+ int depth() const;
+ //! returns element type, similar to CV_MAT_CN(cvMat->type)
+ int channels() const;
+ //! returns element type, return 4 for 3 channels element,
+ //!becuase 3 channels element actually use 4 channel space
+ int oclchannels() const;
+ //! returns step/elemSize1()
+ size_t step1() const;
+ //! returns oclMatrix size:
+ // width == number of columns, height == number of rows
+ Size size() const;
+ //! returns true if oclMatrix data is NULL
+ bool empty() const;
+
+ //! returns pointer to y-th row
+ uchar* ptr(int y = 0);
+ const uchar *ptr(int y = 0) const;
+
+ //! template version of the above method
+ template<typename _Tp> _Tp *ptr(int y = 0);
+ template<typename _Tp> const _Tp *ptr(int y = 0) const;
+
+ //! matrix transposition
+ oclMat t() const;
+
+ /*! includes several bit-fields:
+ - the magic signature
+ - continuity flag
+ - depth
+ - number of channels
+ */
+ int flags;
+ //! the number of rows and columns
+ int rows, cols;
+ //! a distance between successive rows in bytes; includes the gap if any
+ size_t step;
+ //! pointer to the data(OCL memory object)
+ uchar *data;
+
+ //! pointer to the reference counter;
+ // when oclMatrix points to user-allocated data, the pointer is NULL
+ int *refcount;
+
+ //! helper fields used in locateROI and adjustROI
+ //datastart and dataend are not used in current version
+ uchar *datastart;
+ uchar *dataend;
+
+ //! OpenCL context associated with the oclMat object.
+ Context *clCxt;
+ //add offset for handle ROI, calculated in byte
+ int offset;
+ //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
+ int wholerows;
+ int wholecols;
+ };
+
+ // convert InputArray/OutputArray to oclMat references
+ CV_EXPORTS oclMat& getOclMatRef(InputArray src);
+ CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
+
+ ///////////////////// mat split and merge /////////////////////////////////
+ //! Compose a multi-channel array from several single-channel arrays
+ // Support all types
+ CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
+ CV_EXPORTS void merge(const std::vector<oclMat> &src, oclMat &dst);
+
+ //! Divides multi-channel array into several single-channel arrays
+ // Support all types
+ CV_EXPORTS void split(const oclMat &src, oclMat *dst);
+ CV_EXPORTS void split(const oclMat &src, std::vector<oclMat> &dst);
+
+ ////////////////////////////// Arithmetics ///////////////////////////////////
+ //#if defined DOUBLE_SUPPORT
+ //typedef double F;
+ //#else
+ //typedef float F;
+ //#endif
+ // CV_EXPORTS void addWeighted(const oclMat& a,F alpha, const oclMat& b,F beta,F gama, oclMat& c);
+ CV_EXPORTS void addWeighted(const oclMat &a, double alpha, const oclMat &b, double beta, double gama, oclMat &c);
+ //! adds one matrix to another (c = a + b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void add(const oclMat &a, const oclMat &b, oclMat &c);
+ //! adds one matrix to another (c = a + b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void add(const oclMat &a, const oclMat &b, oclMat &c, const oclMat &mask);
+ //! adds scalar to a matrix (c = a + s)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void add(const oclMat &a, const Scalar &sc, oclMat &c, const oclMat &mask = oclMat());
+ //! subtracts one matrix from another (c = a - b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void subtract(const oclMat &a, const oclMat &b, oclMat &c);
+ //! subtracts one matrix from another (c = a - b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void subtract(const oclMat &a, const oclMat &b, oclMat &c, const oclMat &mask);
+ //! subtracts scalar from a matrix (c = a - s)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void subtract(const oclMat &a, const Scalar &sc, oclMat &c, const oclMat &mask = oclMat());
+ //! subtracts scalar from a matrix (c = a - s)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void subtract(const Scalar &sc, const oclMat &a, oclMat &c, const oclMat &mask = oclMat());
+ //! computes element-wise product of the two arrays (c = a * b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void multiply(const oclMat &a, const oclMat &b, oclMat &c, double scale = 1);
+ //! multiplies matrix to a number (dst = scalar * src)
+ // supports CV_32FC1 only
+ CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
+ //! computes element-wise quotient of the two arrays (c = a / b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void divide(const oclMat &a, const oclMat &b, oclMat &c, double scale = 1);
+ //! computes element-wise quotient of the two arrays (c = a / b)
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void divide(double scale, const oclMat &b, oclMat &c);
+
+ //! compares elements of two arrays (c = a <cmpop> b)
+ // supports except CV_8SC1,CV_8SC2,CV8SC3,CV_8SC4 types
+ CV_EXPORTS void compare(const oclMat &a, const oclMat &b, oclMat &c, int cmpop);
+
+ //! transposes the matrix
+ // supports CV_8UC1, 8UC4, 8SC4, 16UC2, 16SC2, 32SC1 and 32FC1.(the same as cuda)
+ CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
+
+ //! computes element-wise absolute difference of two arrays (c = abs(a - b))
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void absdiff(const oclMat &a, const oclMat &b, oclMat &c);
+ //! computes element-wise absolute difference of array and scalar (c = abs(a - s))
+ // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
+ CV_EXPORTS void absdiff(const oclMat &a, const Scalar &s, oclMat &c);
+
+ //! computes mean value and standard deviation of all or selected array elements
+ // supports except CV_32F,CV_64F
+ CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
+
+ //! computes norm of array
+ // supports NORM_INF, NORM_L1, NORM_L2
+ // supports only CV_8UC1 type
+ CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
+
+ //! computes norm of the difference between two arrays
+ // supports NORM_INF, NORM_L1, NORM_L2
+ // supports only CV_8UC1 type
+ CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
+
+ //! reverses the order of the rows, columns or both in a matrix
+ // supports all types
+ CV_EXPORTS void flip(const oclMat &a, oclMat &b, int flipCode);
+
+ //! computes sum of array elements
+ // disabled until fix crash
+ // support all types
+ CV_EXPORTS Scalar sum(const oclMat &m);
+ CV_EXPORTS Scalar absSum(const oclMat &m);
+ CV_EXPORTS Scalar sqrSum(const oclMat &m);
+
+ //! finds global minimum and maximum array elements and returns their values
+ // support all C1 types
+
+ CV_EXPORTS void minMax(const oclMat &src, double *minVal, double *maxVal = 0, const oclMat &mask = oclMat());
+ CV_EXPORTS void minMax_buf(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask, oclMat& buf);
+
+ //! finds global minimum and maximum array elements and returns their values with locations
+ // support all C1 types
+
+ CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
+ const oclMat &mask = oclMat());
+
+ //! counts non-zero array elements
+ // support all types
+ CV_EXPORTS int countNonZero(const oclMat &src);
+
+ //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
+ // destination array will have the depth type as lut and the same channels number as source
+ //It supports 8UC1 8UC4 only
+ CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
+
+ //! only 8UC1 and 256 bins is supported now
+ CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
+ //! only 8UC1 and 256 bins is supported now
+ CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
+
+ //! only 8UC1 is supported now
+ CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
+
+ //! bilateralFilter
+ // supports 8UC1 8UC4
++ CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
++
++ //! Applies an adaptive bilateral filter to the input image
++ // This is not truly a bilateral filter. Instead of using user provided fixed parameters,
++ // the function calculates a constant at each window based on local standard deviation,
++ // and use this constant to do filtering.
++ // supports 8UC1 8UC3
++ CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
++
+ //! computes exponent of each matrix element (b = e**a)
+ // supports only CV_32FC1 type
+ CV_EXPORTS void exp(const oclMat &a, oclMat &b);
+
+ //! computes natural logarithm of absolute value of each matrix element: b = log(abs(a))
+ // supports only CV_32FC1 type
+ CV_EXPORTS void log(const oclMat &a, oclMat &b);
+
+ //! computes magnitude of each (x(i), y(i)) vector
+ // supports only CV_32F CV_64F type
+ CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
+ CV_EXPORTS void magnitudeSqr(const oclMat &x, const oclMat &y, oclMat &magnitude);
+
+ CV_EXPORTS void magnitudeSqr(const oclMat &x, oclMat &magnitude);
+
+ //! computes angle (angle(i)) of each (x(i), y(i)) vector
+ // supports only CV_32F CV_64F type
+ CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
+
+ //! the function raises every element of tne input array to p
+ //! support only CV_32F CV_64F type
+ CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
+
+ //! converts Cartesian coordinates to polar
+ // supports only CV_32F CV_64F type
+ CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
+
+ //! converts polar coordinates to Cartesian
+ // supports only CV_32F CV_64F type
+ CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
+
+ //! perfroms per-elements bit-wise inversion
+ // supports all types
+ CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
+ //! calculates per-element bit-wise disjunction of two arrays
+ // supports all types
+ CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
+ CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
+ //! calculates per-element bit-wise conjunction of two arrays
+ // supports all types
+ CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
+ CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
+ //! calculates per-element bit-wise "exclusive or" operation
+ // supports all types
+ CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
+ CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
+
+ //! Logical operators
+ CV_EXPORTS oclMat operator ~ (const oclMat &);
+ CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
+ CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
+ CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
+
+
+ //! Mathematics operators
+ CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
+ CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
+ CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
+ CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
+
+ struct CV_EXPORTS ConvolveBuf
+ {
+ Size result_size;
+ Size block_size;
+ Size user_block_size;
+ Size dft_size;
+
+ oclMat image_spect, templ_spect, result_spect;
+ oclMat image_block, templ_block, result_data;
+
+ void create(Size image_size, Size templ_size);
+ static Size estimateBlockSize(Size result_size, Size templ_size);
+ };
+
+ //! computes convolution of two images, may use discrete Fourier transform
+ //! support only CV_32FC1 type
+ CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
+ CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
+
+ //! Performs a per-element multiplication of two Fourier spectrums.
+ //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
+ //! support only CV_32FC2 type
+ CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
+
+ CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0);
+
+ //////////////////////////////// Filter Engine ////////////////////////////////
+
+ /*!
+ The Base Class for 1D or Row-wise Filters
+
+ This is the base class for linear or non-linear filters that process 1D data.
+ In particular, such filters are used for the "horizontal" filtering parts in separable filters.
+ */
+ class CV_EXPORTS BaseRowFilter_GPU
+ {
+ public:
+ BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
+ virtual ~BaseRowFilter_GPU() {}
+ virtual void operator()(const oclMat &src, oclMat &dst) = 0;
+ int ksize, anchor, bordertype;
+ };
+
+ /*!
+ The Base Class for Column-wise Filters
+
+ This is the base class for linear or non-linear filters that process columns of 2D arrays.
+ Such filters are used for the "vertical" filtering parts in separable filters.
+ */
+ class CV_EXPORTS BaseColumnFilter_GPU
+ {
+ public:
+ BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
+ virtual ~BaseColumnFilter_GPU() {}
+ virtual void operator()(const oclMat &src, oclMat &dst) = 0;
+ int ksize, anchor, bordertype;
+ };
+
+ /*!
+ The Base Class for Non-Separable 2D Filters.
+
+ This is the base class for linear or non-linear 2D filters.
+ */
+ class CV_EXPORTS BaseFilter_GPU
+ {
+ public:
+ BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
+ : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
+ virtual ~BaseFilter_GPU() {}
+ virtual void operator()(const oclMat &src, oclMat &dst) = 0;
+ Size ksize;
+ Point anchor;
+ int borderType;
+ };
+
+ /*!
+ The Base Class for Filter Engine.
+
+ The class can be used to apply an arbitrary filtering operation to an image.
+ It contains all the necessary intermediate buffers.
+ */
+ class CV_EXPORTS FilterEngine_GPU
+ {
+ public:
+ virtual ~FilterEngine_GPU() {}
+
+ virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
+ };
+
+ //! returns the non-separable filter engine with the specified filter
+ CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
+
+ //! returns the primitive row filter with the specified kernel
+ CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
+ int anchor = -1, int bordertype = BORDER_DEFAULT);
+
+ //! returns the primitive column filter with the specified kernel
+ CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
+ int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
+
+ //! returns the separable linear filter engine
+ CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
+ const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
+
+ //! returns the separable filter engine with the specified filters
+ CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
+ const Ptr<BaseColumnFilter_GPU> &columnFilter);
+
+ //! returns the Gaussian filter engine
+ CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
+
+ //! returns filter engine for the generalized Sobel operator
+ CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
+
+ //! applies Laplacian operator to the image
+ // supports only ksize = 1 and ksize = 3 8UC1 8UC4 32FC1 32FC4 data type
+ CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1);
+
+ //! returns 2D box filter
+ // supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
+ CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
+ const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! returns box filter engine
+ CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
+ const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! returns 2D filter with the specified kernel
+ // supports CV_8UC1 and CV_8UC4 types
+ CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
+ const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! returns the non-separable linear filter engine
+ CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
+ const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! smooths the image using the normalized box filter
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101,BORDER_WRAP
+ CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
+ Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! returns 2D morphological filter
+ //! only MORPH_ERODE and MORPH_DILATE are supported
+ // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
+ // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
+ CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
+ Point anchor = Point(-1, -1));
+
+ //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
+ CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
+ const Point &anchor = Point(-1, -1), int iterations = 1);
+
+ //! a synonym for normalized box filter
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
+ static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
+ int borderType = BORDER_CONSTANT)
+ {
+ boxFilter(src, dst, -1, ksize, anchor, borderType);
+ }
+
+ //! applies non-separable 2D linear filter to the image
+ // Note, at the moment this function only works when anchor point is in the kernel center
+ // and kernel size supported is either 3x3 or 5x5; otherwise the function will fail to output valid result
+ CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
+ Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
+
+ //! applies separable 2D linear filter to the image
+ CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
+ Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
+
+ //! applies generalized Sobel operator to the image
+ // dst.type must equalize src.type
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
+ CV_EXPORTS void Sobel(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
+
+ //! applies the vertical or horizontal Scharr operator to the image
+ // dst.type must equalize src.type
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
+ CV_EXPORTS void Scharr(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
+
+ //! smooths the image using Gaussian filter.
+ // dst.type must equalize src.type
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
+ CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
+
+ //! erodes the image (applies the local minimum operator)
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
+
+ int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
+
+
+ //! dilates the image (applies the local maximum operator)
+ // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
+ CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
+
+ int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
+
+
+ //! applies an advanced morphological operation to the image
+ CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
+
+ int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
+
+
+ ////////////////////////////// Image processing //////////////////////////////
+ //! Does mean shift filtering on GPU.
+ CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
+ TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
+
+ //! Does mean shift procedure on GPU.
+ CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
+ TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
+
+ //! Does mean shift segmentation with elimiation of small regions.
+ CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
+ TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
+
+ //! applies fixed threshold to the image.
+ // supports CV_8UC1 and CV_32FC1 data type
+ // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
+ CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
+
+ //! resizes the image
+ // Supports INTER_NEAREST, INTER_LINEAR
+ // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
+ CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
+
+ //! Applies a generic geometrical transformation to an image.
+
+ // Supports INTER_NEAREST, INTER_LINEAR.
+
+ // Map1 supports CV_16SC2, CV_32FC2 types.
+
+ // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
+
+ CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
+
+ //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
+ // supports CV_8UC1, CV_8UC4, CV_32SC1 types
+ CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
+
+ //! Smoothes image using median filter
+ // The source 1- or 4-channel image. When m is 3 or 5, the image depth should be CV 8U or CV 32F.
+ CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
+
+ //! warps the image using affine transformation
+ // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
+ // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
+ CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
+
+ //! warps the image using perspective transformation
+ // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
+ // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
+ CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
+
+ //! computes the integral image and integral for the squared image
+ // sum will have CV_32S type, sqsum - CV32F type
+ // supports only CV_8UC1 source type
+ CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum);
+ CV_EXPORTS void integral(const oclMat &src, oclMat &sum);
+ CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
+ CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
+ int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
+ CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
+ CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
+ int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
+
+
+ /////////////////////////////////// ML ///////////////////////////////////////////
+
+ //! Compute closest centers for each lines in source and lable it after center's index
+ // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
+ CV_EXPORTS void distanceToCenters(oclMat &dists, oclMat &labels, const oclMat &src, const oclMat ¢ers);
+
+ //!Does k-means procedure on GPU
+ // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
+ CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
+ TermCriteria criteria, int attemps, int flags, oclMat ¢ers);
+
+
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ class CV_EXPORTS OclCascadeClassifier : public cv::CascadeClassifier
+ {
+ 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());
+ };
+
+ /////////////////////////////// Pyramid /////////////////////////////////////
+ CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
+
+ //! upsamples the source image and then smoothes it
+ CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
+
+ //! performs linear blending of two images
+ //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
+ // supports only CV_8UC1 source type
+ CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
+
+ //! computes vertical sum, supports only CV_32FC1 images
+ CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
+
+ ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
+ struct CV_EXPORTS MatchTemplateBuf
+ {
+ Size user_block_size;
+ oclMat imagef, templf;
+ std::vector<oclMat> images;
+ std::vector<oclMat> image_sums;
+ std::vector<oclMat> image_sqsums;
+ };
+
+ //! computes the proximity map for the raster template and the image where the template is searched for
+ // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
+ // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
+ CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
+
+ //! computes the proximity map for the raster template and the image where the template is searched for
+ // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
+ // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
+ CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
+
+
+
+ ///////////////////////////////////////////// Canny /////////////////////////////////////////////
+ struct CV_EXPORTS CannyBuf;
+
+ //! compute edges of the input image using Canny operator
+ // Support CV_8UC1 only
+ CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
+ CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
+ CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
+ CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
+
+ struct CV_EXPORTS CannyBuf
+ {
+ CannyBuf() : counter(NULL) {}
+ ~CannyBuf()
+ {
+ release();
+ }
+ explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(NULL)
+ {
+ create(image_size, apperture_size);
+ }
+ CannyBuf(const oclMat &dx_, const oclMat &dy_);
+ void create(const Size &image_size, int apperture_size = 3);
+ void release();
+
+ oclMat dx, dy;
+ oclMat dx_buf, dy_buf;
+ oclMat magBuf, mapBuf;
+ oclMat trackBuf1, trackBuf2;
+ void *counter;
+ Ptr<FilterEngine_GPU> filterDX, filterDY;
+ };
+
+ ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
+ //! HoughCircles
+ struct HoughCirclesBuf
+ {
+ oclMat edges;
+ oclMat accum;
+ oclMat srcPoints;
+ oclMat centers;
+ CannyBuf cannyBuf;
+ };
+
+ CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
+ CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
+ CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
+
+
+ ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
+ //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
+ //! Param dft_size is the size of DFT transform.
+ //!
+ //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
+ // support src type of CV32FC1, CV32FC2
+ // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
+ // dft_size is the size of original input, which is used for transformation from complex to real.
+ // dft_size must be powers of 2, 3 and 5
+ // real to complex dft requires at least v1.8 clAmdFft
+ // real to complex dft output is not the same with cpu version
+ // real to complex and complex to real does not support DFT_ROWS
+ CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(0, 0), int flags = 0);
+
+ //! implements generalized matrix product algorithm GEMM from BLAS
+ // The functionality requires clAmdBlas library
+ // only support type CV_32FC1
+ // flag GEMM_3_T is not supported
+ CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
+ const oclMat &src3, double beta, oclMat &dst, int flags = 0);
+
+ //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
+
+ struct CV_EXPORTS HOGDescriptor
+
+ {
+
+ enum { DEFAULT_WIN_SIGMA = -1 };
+
+ enum { DEFAULT_NLEVELS = 64 };
+
+ enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
+
+
+
+ HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
+
+ Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
+
+ int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
+
+ double threshold_L2hys = 0.2, bool gamma_correction = true,
+
+ int nlevels = DEFAULT_NLEVELS);
+
+
+
+ size_t getDescriptorSize() const;
+
+ size_t getBlockHistogramSize() const;
+
+
+
+ void setSVMDetector(const std::vector<float> &detector);
+
+
+
+ static std::vector<float> getDefaultPeopleDetector();
+
+ static std::vector<float> getPeopleDetector48x96();
+
+ static std::vector<float> getPeopleDetector64x128();
+
+
+
+ void detect(const oclMat &img, std::vector<Point> &found_locations,
+
+ double hit_threshold = 0, Size win_stride = Size(),
+
+ Size padding = Size());
+
+
+
+ void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
+
+ double hit_threshold = 0, Size win_stride = Size(),
+
+ Size padding = Size(), double scale0 = 1.05,
+
+ int group_threshold = 2);
+
+
+
+ void getDescriptors(const oclMat &img, Size win_stride,
+
+ oclMat &descriptors,
+
+ int descr_format = DESCR_FORMAT_COL_BY_COL);
+
+
+
+ Size win_size;
+
+ Size block_size;
+
+ Size block_stride;
+
+ Size cell_size;
+
+ int nbins;
+
+ double win_sigma;
+
+ double threshold_L2hys;
+
+ bool gamma_correction;
+
+ int nlevels;
+
+
+
+ protected:
+
+ // initialize buffers; only need to do once in case of multiscale detection
+
+ void init_buffer(const oclMat &img, Size win_stride);
+
+
+
+ void computeBlockHistograms(const oclMat &img);
+
+ void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
+
+
+
+ double getWinSigma() const;
+
+ bool checkDetectorSize() const;
+
+
+
+ static int numPartsWithin(int size, int part_size, int stride);
+
+ static Size numPartsWithin(Size size, Size part_size, Size stride);
+
+
+
+ // Coefficients of the separating plane
+
+ float free_coef;
+
+ oclMat detector;
+
+
+
+ // Results of the last classification step
+
+ oclMat labels;
+
+ Mat labels_host;
+
+
+
+ // Results of the last histogram evaluation step
+
+ oclMat block_hists;
+
+
+
+ // Gradients conputation results
+
+ oclMat grad, qangle;
+
+
+
+ // scaled image
+
+ oclMat image_scale;
+
+
+
+ // effect size of input image (might be different from original size after scaling)
+
+ Size effect_size;
+
+ };
+
+
+ ////////////////////////feature2d_ocl/////////////////
+ /****************************************************************************************\
+ * Distance *
+ \****************************************************************************************/
+ template<typename T>
+ struct CV_EXPORTS Accumulator
+ {
+ typedef T Type;
+ };
+ template<> struct Accumulator<unsigned char>
+ {
+ typedef float Type;
+ };
+ template<> struct Accumulator<unsigned short>
+ {
+ typedef float Type;
+ };
+ template<> struct Accumulator<char>
+ {
+ typedef float Type;
+ };
+ template<> struct Accumulator<short>
+ {
+ typedef float Type;
+ };
+
+ /*
+ * Manhattan distance (city block distance) functor
+ */
+ template<class T>
+ struct CV_EXPORTS L1
+ {
+ enum { normType = NORM_L1 };
+ typedef T ValueType;
+ typedef typename Accumulator<T>::Type ResultType;
+
+ ResultType operator()( const T *a, const T *b, int size ) const
+ {
+ return normL1<ValueType, ResultType>(a, b, size);
+ }
+ };
+
+ /*
+ * Euclidean distance functor
+ */
+ template<class T>
+ struct CV_EXPORTS L2
+ {
+ enum { normType = NORM_L2 };
+ typedef T ValueType;
+ typedef typename Accumulator<T>::Type ResultType;
+
+ ResultType operator()( const T *a, const T *b, int size ) const
+ {
+ return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
+ }
+ };
+
+ /*
+ * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
+ * bit count of A exclusive XOR'ed with B
+ */
+ struct CV_EXPORTS Hamming
+ {
+ enum { normType = NORM_HAMMING };
+ typedef unsigned char ValueType;
+ typedef int ResultType;
+
+ /** this will count the bits in a ^ b
+ */
+ ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
+ {
+ return normHamming(a, b, size);
+ }
+ };
+
+ ////////////////////////////////// BruteForceMatcher //////////////////////////////////
+
+ class CV_EXPORTS BruteForceMatcher_OCL_base
+ {
+ public:
+ enum DistType {L1Dist = 0, L2Dist, HammingDist};
+ explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
+
+ // Add descriptors to train descriptor collection
+ void add(const std::vector<oclMat> &descCollection);
+
+ // Get train descriptors collection
+ const std::vector<oclMat> &getTrainDescriptors() const;
+
+ // Clear train descriptors collection
+ void clear();
+
+ // Return true if there are not train descriptors in collection
+ bool empty() const;
+
+ // Return true if the matcher supports mask in match methods
+ bool isMaskSupported() const;
+
+ // Find one best match for each query descriptor
+ void matchSingle(const oclMat &query, const oclMat &train,
+ oclMat &trainIdx, oclMat &distance,
+ const oclMat &mask = oclMat());
+
+ // Download trainIdx and distance and convert it to CPU vector with DMatch
+ static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
+ // Convert trainIdx and distance to vector with DMatch
+ static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
+
+ // Find one best match for each query descriptor
+ void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
+
+ // Make gpu collection of trains and masks in suitable format for matchCollection function
+ void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
+
+ // Find one best match from train collection for each query descriptor
+ void matchCollection(const oclMat &query, const oclMat &trainCollection,
+ oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
+ const oclMat &masks = oclMat());
+
+ // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
+ static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
+ // Convert trainIdx, imgIdx and distance to vector with DMatch
+ static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
+
+ // Find one best match from train collection for each query descriptor.
+ void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
+
+ // Find k best matches for each query descriptor (in increasing order of distances)
+ void knnMatchSingle(const oclMat &query, const oclMat &train,
+ oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
+ const oclMat &mask = oclMat());
+
+ // Download trainIdx and distance and convert it to vector with DMatch
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+ // Convert trainIdx and distance to vector with DMatch
+ static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+
+ // Find k best matches for each query descriptor (in increasing order of distances).
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ void knnMatch(const oclMat &query, const oclMat &train,
+ std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
+ bool compactResult = false);
+
+ // Find k best matches from train collection for each query descriptor (in increasing order of distances)
+ void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
+ oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
+ const oclMat &maskCollection = oclMat());
+
+ // Download trainIdx and distance and convert it to vector with DMatch
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+ // Convert trainIdx and distance to vector with DMatch
+ static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+
+ // Find k best matches for each query descriptor (in increasing order of distances).
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
+ const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
+
+ // Find best matches for each query descriptor which have distance less than maxDistance.
+ // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
+ // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
+ // because it didn't have enough memory.
+ // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
+ // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
+ // Matches doesn't sorted.
+ void radiusMatchSingle(const oclMat &query, const oclMat &train,
+ oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
+ const oclMat &mask = oclMat());
+
+ // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
+ // matches will be sorted in increasing order of distances.
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+ // Convert trainIdx, nMatches and distance to vector with DMatch.
+ static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+
+ // Find best matches for each query descriptor which have distance less than maxDistance
+ // in increasing order of distances).
+ void radiusMatch(const oclMat &query, const oclMat &train,
+ std::vector< std::vector<DMatch> > &matches, float maxDistance,
+ const oclMat &mask = oclMat(), bool compactResult = false);
+
+ // Find best matches for each query descriptor which have distance less than maxDistance.
+ // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
+ // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
+ // Matches doesn't sorted.
+ void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
+ const std::vector<oclMat> &masks = std::vector<oclMat>());
+
+ // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
+ // matches will be sorted in increasing order of distances.
+ // compactResult is used when mask is not empty. If compactResult is false matches
+ // vector will have the same size as queryDescriptors rows. If compactResult is true
+ // matches vector will not contain matches for fully masked out query descriptors.
+ static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+ // Convert trainIdx, nMatches and distance to vector with DMatch.
+ static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
+ std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
+
+ // Find best matches from train collection for each query descriptor which have distance less than
+ // maxDistance (in increasing order of distances).
+ void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
+ const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
+
+ DistType distType;
+
+ private:
+ std::vector<oclMat> trainDescCollection;
+ };
+
+ template <class Distance>
+ class CV_EXPORTS BruteForceMatcher_OCL;
+
+ template <typename T>
+ class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
+ {
+ public:
+ explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
+ explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
+ };
+ template <typename T>
+ class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
+ {
+ public:
+ explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
+ explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
+ };
+ template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
+ {
+ public:
+ explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
+ explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
+ };
+
+ class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
+ {
+ public:
+ explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
+ };
+
+ class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
+ {
+ public:
+ explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
+ int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
+
+ //! return 1 rows matrix with CV_32FC2 type
+ void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
+ //! download points of type Point2f to a vector. the vector's content will be erased
+ void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
+
+ int maxCorners;
+ double qualityLevel;
+ double minDistance;
+
+ int blockSize;
+ bool useHarrisDetector;
+ double harrisK;
+ void releaseMemory()
+ {
+ Dx_.release();
+ Dy_.release();
+ eig_.release();
+ minMaxbuf_.release();
+ tmpCorners_.release();
+ }
+ private:
+ oclMat Dx_;
+ oclMat Dy_;
+ oclMat eig_;
+ oclMat minMaxbuf_;
+ oclMat tmpCorners_;
+ };
+
+ inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
+ int blockSize_, bool useHarrisDetector_, double harrisK_)
+ {
+ maxCorners = maxCorners_;
+ qualityLevel = qualityLevel_;
+ minDistance = minDistance_;
+ blockSize = blockSize_;
+ useHarrisDetector = useHarrisDetector_;
+ harrisK = harrisK_;
+ }
+
+ /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
+
+ class CV_EXPORTS PyrLKOpticalFlow
+ {
+ public:
+ PyrLKOpticalFlow()
+ {
+ winSize = Size(21, 21);
+ maxLevel = 3;
+ iters = 30;
+ derivLambda = 0.5;
+ useInitialFlow = false;
+ minEigThreshold = 1e-4f;
+ getMinEigenVals = false;
+ isDeviceArch11_ = false;
+ }
+
+ void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
+ oclMat &status, oclMat *err = 0);
+
+ void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
+
+ Size winSize;
+ int maxLevel;
+ int iters;
+ double derivLambda;
+ bool useInitialFlow;
+ float minEigThreshold;
+ bool getMinEigenVals;
+
+ void releaseMemory()
+ {
+ dx_calcBuf_.release();
+ dy_calcBuf_.release();
+
+ prevPyr_.clear();
+ nextPyr_.clear();
+
+ dx_buf_.release();
+ dy_buf_.release();
+ }
+
+ private:
+ void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
+
+ void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
+
+ oclMat dx_calcBuf_;
+ oclMat dy_calcBuf_;
+
+ std::vector<oclMat> prevPyr_;
+ std::vector<oclMat> nextPyr_;
+
+ oclMat dx_buf_;
+ oclMat dy_buf_;
+
+ oclMat uPyr_[2];
+ oclMat vPyr_[2];
+
+ bool isDeviceArch11_;
+ };
+
+ class CV_EXPORTS FarnebackOpticalFlow
+ {
+ public:
+ FarnebackOpticalFlow();
+
+ int numLevels;
+ double pyrScale;
+ bool fastPyramids;
+ int winSize;
+ int numIters;
+ int polyN;
+ double polySigma;
+ int flags;
+
+ void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
+
+ void releaseMemory();
+
+ private:
+ void prepareGaussian(
+ int n, double sigma, float *g, float *xg, float *xxg,
+ double &ig11, double &ig03, double &ig33, double &ig55);
+
+ void setPolynomialExpansionConsts(int n, double sigma);
+
+ void updateFlow_boxFilter(
+ const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
+ oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
+
+ void updateFlow_gaussianBlur(
+ const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
+ oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
+
+ oclMat frames_[2];
+ oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
+ std::vector<oclMat> pyramid0_, pyramid1_;
+ };
+
+ //////////////// build warping maps ////////////////////
+ //! builds plane warping maps
+ CV_EXPORTS void buildWarpPlaneMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T, float scale, oclMat &map_x, oclMat &map_y);
+ //! builds cylindrical warping maps
+ CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
+ //! builds spherical warping maps
+ CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
+ //! builds Affine warping maps
+ CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
+
+ //! builds Perspective warping maps
+ CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
+
+ ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
+ //! Interpolate frames (images) using provided optical flow (displacement field).
+ //! frame0 - frame 0 (32-bit floating point images, single channel)
+ //! frame1 - frame 1 (the same type and size)
+ //! fu - forward horizontal displacement
+ //! fv - forward vertical displacement
+ //! bu - backward horizontal displacement
+ //! bv - backward vertical displacement
+ //! pos - new frame position
+ //! newFrame - new frame
+ //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
+ //! occlusion masks 0, occlusion masks 1,
+ //! interpolated forward flow 0, interpolated forward flow 1,
+ //! interpolated backward flow 0, interpolated backward flow 1
+ //!
+ CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
+ const oclMat &fu, const oclMat &fv,
+ const oclMat &bu, const oclMat &bv,
+ float pos, oclMat &newFrame, oclMat &buf);
+
+ //! computes moments of the rasterized shape or a vector of points
+ CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
+
+ class CV_EXPORTS StereoBM_OCL
+ {
+ public:
+ enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
+
+ enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
+
+ //! the default constructor
+ StereoBM_OCL();
+ //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
+ StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
+
+ //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
+ //! Output disparity has CV_8U type.
+ void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
+
+ //! Some heuristics that tries to estmate
+ // if current GPU will be faster then CPU in this algorithm.
+ // It queries current active device.
+ static bool checkIfGpuCallReasonable();
+
+ int preset;
+ int ndisp;
+ int winSize;
+
+ // If avergeTexThreshold == 0 => post procesing is disabled
+ // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
+ // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
+ // i.e. input left image is low textured.
+ float avergeTexThreshold;
+ private:
+ oclMat minSSD, leBuf, riBuf;
+ };
+
+ class CV_EXPORTS StereoBeliefPropagation
+ {
+ public:
+ enum { DEFAULT_NDISP = 64 };
+ enum { DEFAULT_ITERS = 5 };
+ enum { DEFAULT_LEVELS = 5 };
+ static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
+ explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
+ int iters = DEFAULT_ITERS,
+ int levels = DEFAULT_LEVELS,
+ int msg_type = CV_16S);
+ StereoBeliefPropagation(int ndisp, int iters, int levels,
+ float max_data_term, float data_weight,
+ float max_disc_term, float disc_single_jump,
+ int msg_type = CV_32F);
+ void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
+ void operator()(const oclMat &data, oclMat &disparity);
+ int ndisp;
+ int iters;
+ int levels;
+ float max_data_term;
+ float data_weight;
+ float max_disc_term;
+ float disc_single_jump;
+ int msg_type;
+ private:
+ oclMat u, d, l, r, u2, d2, l2, r2;
+ std::vector<oclMat> datas;
+ oclMat out;
+ };
+
+ class CV_EXPORTS StereoConstantSpaceBP
+ {
+ public:
+ enum { DEFAULT_NDISP = 128 };
+ enum { DEFAULT_ITERS = 8 };
+ enum { DEFAULT_LEVELS = 4 };
+ enum { DEFAULT_NR_PLANE = 4 };
+ static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
+ explicit StereoConstantSpaceBP(
+ int ndisp = DEFAULT_NDISP,
+ int iters = DEFAULT_ITERS,
+ int levels = DEFAULT_LEVELS,
+ int nr_plane = DEFAULT_NR_PLANE,
+ int msg_type = CV_32F);
+ StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
+ float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
+ int min_disp_th = 0,
+ int msg_type = CV_32F);
+ void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
+ int ndisp;
+ int iters;
+ int levels;
+ int nr_plane;
+ float max_data_term;
+ float data_weight;
+ float max_disc_term;
+ float disc_single_jump;
+ int min_disp_th;
+ int msg_type;
+ bool use_local_init_data_cost;
+ private:
+ oclMat u[2], d[2], l[2], r[2];
+ oclMat disp_selected_pyr[2];
+ oclMat data_cost;
+ oclMat data_cost_selected;
+ oclMat temp;
+ oclMat out;
+ };
+
+ // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
+ //
+ // see reference:
+ // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
+ // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
+ class CV_EXPORTS OpticalFlowDual_TVL1_OCL
+ {
+ public:
+ OpticalFlowDual_TVL1_OCL();
+
+ void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
+
+ void collectGarbage();
+
+ /**
+ * Time step of the numerical scheme.
+ */
+ double tau;
+
+ /**
+ * Weight parameter for the data term, attachment parameter.
+ * This is the most relevant parameter, which determines the smoothness of the output.
+ * The smaller this parameter is, the smoother the solutions we obtain.
+ * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
+ */
+ double lambda;
+
+ /**
+ * Weight parameter for (u - v)^2, tightness parameter.
+ * It serves as a link between the attachment and the regularization terms.
+ * In theory, it should have a small value in order to maintain both parts in correspondence.
+ * The method is stable for a large range of values of this parameter.
+ */
+ double theta;
+
+ /**
+ * Number of scales used to create the pyramid of images.
+ */
+ int nscales;
+
+ /**
+ * Number of warpings per scale.
+ * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
+ * This is a parameter that assures the stability of the method.
+ * It also affects the running time, so it is a compromise between speed and accuracy.
+ */
+ int warps;
+
+ /**
+ * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
+ * A small value will yield more accurate solutions at the expense of a slower convergence.
+ */
+ double epsilon;
+
+ /**
+ * Stopping criterion iterations number used in the numerical scheme.
+ */
+ int iterations;
+
+ bool useInitialFlow;
+
+ private:
+ void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
+
+ std::vector<oclMat> I0s;
+ std::vector<oclMat> I1s;
+ std::vector<oclMat> u1s;
+ std::vector<oclMat> u2s;
+
+ oclMat I1x_buf;
+ oclMat I1y_buf;
+
+ oclMat I1w_buf;
+ oclMat I1wx_buf;
+ oclMat I1wy_buf;
+
+ oclMat grad_buf;
+ oclMat rho_c_buf;
+
+ oclMat p11_buf;
+ oclMat p12_buf;
+ oclMat p21_buf;
+ oclMat p22_buf;
+
+ oclMat diff_buf;
+ oclMat norm_buf;
+ };
+ // current supported sorting methods
+ enum
+ {
+ SORT_BITONIC, // only support power-of-2 buffer size
+ SORT_SELECTION, // cannot sort duplicate keys
+ SORT_MERGE,
+ SORT_RADIX // only support signed int/float keys(CV_32S/CV_32F)
+ };
+ //! Returns the sorted result of all the elements in input based on equivalent keys.
+ //
+ // The element unit in the values to be sorted is determined from the data type,
+ // i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
+ // matrix dimension.
+ // both keys and values will be sorted inplace
+ // Key needs to be single channel oclMat.
+ //
+ // Example:
+ // input -
+ // keys = {2, 3, 1} (CV_8UC1)
+ // values = {10,5, 4,3, 6,2} (CV_8UC2)
+ // sortByKey(keys, values, SORT_SELECTION, false);
+ // output -
+ // keys = {1, 2, 3} (CV_8UC1)
+ // values = {6,2, 10,5, 4,3} (CV_8UC2)
+ void CV_EXPORTS sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
++ /*!Base class for MOG and MOG2!*/
++ class CV_EXPORTS BackgroundSubtractor
++ {
++ public:
++ //! the virtual destructor
++ virtual ~BackgroundSubtractor();
++ //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
++ virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
++
++ //! computes a background image
++ virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
++ };
++ /*!
++ Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
++
++ The class implements the following algorithm:
++ "An improved adaptive background mixture model for real-time tracking with shadow detection"
++ P. KadewTraKuPong and R. Bowden,
++ Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
++ http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
++ */
++ class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
++ {
++ public:
++ //! the default constructor
++ MOG(int nmixtures = -1);
++
++ //! re-initiaization method
++ void initialize(Size frameSize, int frameType);
++
++ //! the update operator
++ void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
++
++ //! computes a background image which are the mean of all background gaussians
++ void getBackgroundImage(oclMat& backgroundImage) const;
++
++ //! releases all inner buffers
++ void release();
++
++ int history;
++ float varThreshold;
++ float backgroundRatio;
++ float noiseSigma;
++
++ private:
++ int nmixtures_;
++
++ Size frameSize_;
++ int frameType_;
++ int nframes_;
++
++ oclMat weight_;
++ oclMat sortKey_;
++ oclMat mean_;
++ oclMat var_;
++ };
++
++ /*!
++ The class implements the following algorithm:
++ "Improved adaptive Gausian mixture model for background subtraction"
++ Z.Zivkovic
++ International Conference Pattern Recognition, UK, August, 2004.
++ http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
++ */
++ class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
++ {
++ public:
++ //! the default constructor
++ MOG2(int nmixtures = -1);
++
++ //! re-initiaization method
++ void initialize(Size frameSize, int frameType);
++
++ //! the update operator
++ void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
++
++ //! computes a background image which are the mean of all background gaussians
++ void getBackgroundImage(oclMat& backgroundImage) const;
++
++ //! releases all inner buffers
++ void release();
++
++ // parameters
++ // you should call initialize after parameters changes
++
++ int history;
++
++ //! here it is the maximum allowed number of mixture components.
++ //! Actual number is determined dynamically per pixel
++ float varThreshold;
++ // threshold on the squared Mahalanobis distance to decide if it is well described
++ // by the background model or not. Related to Cthr from the paper.
++ // This does not influence the update of the background. A typical value could be 4 sigma
++ // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
++
++ /////////////////////////
++ // less important parameters - things you might change but be carefull
++ ////////////////////////
++
++ float backgroundRatio;
++ // corresponds to fTB=1-cf from the paper
++ // TB - threshold when the component becomes significant enough to be included into
++ // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
++ // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
++ // it is considered foreground
++ // float noiseSigma;
++ float varThresholdGen;
++
++ //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
++ //when a sample is close to the existing components. If it is not close
++ //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
++ //Smaller Tg leads to more generated components and higher Tg might make
++ //lead to small number of components but they can grow too large
++ float fVarInit;
++ float fVarMin;
++ float fVarMax;
++
++ //initial variance for the newly generated components.
++ //It will will influence the speed of adaptation. A good guess should be made.
++ //A simple way is to estimate the typical standard deviation from the images.
++ //I used here 10 as a reasonable value
++ // min and max can be used to further control the variance
++ float fCT; //CT - complexity reduction prior
++ //this is related to the number of samples needed to accept that a component
++ //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
++ //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
++
++ //shadow detection parameters
++ bool bShadowDetection; //default 1 - do shadow detection
++ unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
++ float fTau;
++ // Tau - shadow threshold. The shadow is detected if the pixel is darker
++ //version of the background. Tau is a threshold on how much darker the shadow can be.
++ //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
++ //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
++
++ private:
++ int nmixtures_;
++
++ Size frameSize_;
++ int frameType_;
++ int nframes_;
++
++ oclMat weight_;
++ oclMat variance_;
++ oclMat mean_;
++
++ oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
++ };
+ }
+}
+#if defined _MSC_VER && _MSC_VER >= 1200
+# pragma warning( push)
+# pragma warning( disable: 4267)
+#endif
+#include "opencv2/ocl/matrix_operations.hpp"
+#if defined _MSC_VER && _MSC_VER >= 1200
+# pragma warning( pop)
+#endif
+
+#endif /* __OPENCV_OCL_HPP__ */
--- /dev/null
- cv::BackgroundSubtractorMOG mog;
+ /*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) 2010-2012, Multicoreware, Inc., all rights reserved.
+ // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
+ // Third party copyrights are property of their respective owners.
+ //
+ // @Authors
+ // Fangfang Bai, fangfang@multicorewareinc.com
+ // Jin Ma, jin@multicorewareinc.com
+ //
+ // 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 oclMaterials 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 "perf_precomp.hpp"
+ using namespace perf;
+ using namespace std;
+ using namespace cv::ocl;
+ using namespace cv;
+ using std::tr1::tuple;
+ using std::tr1::get;
+ #if defined(HAVE_XINE) || \
+ defined(HAVE_GSTREAMER) || \
+ defined(HAVE_QUICKTIME) || \
+ defined(HAVE_AVFOUNDATION) || \
+ defined(HAVE_FFMPEG) || \
+ defined(WIN32)
+
+ # define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
+ #else
+ # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
+ #endif
+
+ #if BUILD_WITH_VIDEO_INPUT_SUPPORT
+ static void cvtFrameFmt(vector<Mat>& input, vector<Mat>& output)
+ {
+ for(int i = 0; i< (int)(input.size()); i++)
+ {
+ cvtColor(input[i], output[i], COLOR_RGB2GRAY);
+ }
+ }
+ //prepare data for CPU
+ static void prepareData(VideoCapture& cap, int cn, vector<Mat>& frame_buffer)
+ {
+ cv::Mat frame;
+ std::vector<Mat> frame_buffer_init;
+ int nFrame = (int)frame_buffer.size();
+ for(int i = 0; i < nFrame; i++)
+ {
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+ frame_buffer_init.push_back(frame);
+ }
+
+ if(cn == 1)
+ cvtFrameFmt(frame_buffer_init, frame_buffer);
+ else
+ frame_buffer = frame_buffer_init;
+ }
+ //copy CPU data to GPU
+ static void prepareData(vector<Mat>& frame_buffer, vector<oclMat>& frame_buffer_ocl)
+ {
+ for(int i = 0; i < (int)frame_buffer.size(); i++)
+ frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i]));
+ }
+ #endif
+ ///////////// MOG ////////////////////////
+ #if BUILD_WITH_VIDEO_INPUT_SUPPORT
+
+ typedef tuple<string, int, double> VideoMOGParamType;
+ typedef TestBaseWithParam<VideoMOGParamType> VideoMOGFixture;
+
+ PERF_TEST_P(VideoMOGFixture, MOG,
+ ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
+ ::testing::Values(1, 3),
+ ::testing::Values(0.0, 0.01)))
+ {
+ VideoMOGParamType params = GetParam();
+
+ const string inputFile = perf::TestBase::getDataPath(get<0>(params));
+ const int cn = get<1>(params);
+ const float learningRate = static_cast<float>(get<2>(params));
+
+ const int nFrame = 5;
+
+ Mat foreground_cpu;
+ std::vector<Mat> frame_buffer(nFrame);
+ std::vector<oclMat> frame_buffer_ocl;
+
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+
+ prepareData(cap, cn, frame_buffer);
+
+ cv::Mat foreground;
+ cv::ocl::oclMat foreground_d;
+ if(RUN_PLAIN_IMPL)
+ {
+ TEST_CYCLE()
+ {
- mog(frame_buffer[i], foreground, learningRate);
++ cv::Ptr<cv::BackgroundSubtractorMOG> mog = createBackgroundSubtractorMOG();
+ foreground.release();
+ for (int i = 0; i < nFrame; i++)
+ {
- cv::BackgroundSubtractorMOG2 mog2;
- mog2.set("detectShadows", false);
++ mog->apply(frame_buffer[i], foreground, learningRate);
+ }
+ }
+ SANITY_CHECK(foreground);
+ }else if(RUN_OCL_IMPL)
+ {
+ prepareData(frame_buffer, frame_buffer_ocl);
+ CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
+ OCL_TEST_CYCLE()
+ {
+ cv::ocl::MOG d_mog;
+ foreground_d.release();
+ for (int i = 0; i < nFrame; ++i)
+ {
+ d_mog(frame_buffer_ocl[i], foreground_d, learningRate);
+ }
+ }
+ foreground_d.download(foreground);
+ SANITY_CHECK(foreground);
+ }else
+ OCL_PERF_ELSE
+ }
+ #endif
+
+ ///////////// MOG2 ////////////////////////
+ #if BUILD_WITH_VIDEO_INPUT_SUPPORT
+
+ typedef tuple<string, int> VideoMOG2ParamType;
+ typedef TestBaseWithParam<VideoMOG2ParamType> VideoMOG2Fixture;
+
+ PERF_TEST_P(VideoMOG2Fixture, MOG2,
+ ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
+ ::testing::Values(1, 3)))
+ {
+ VideoMOG2ParamType params = GetParam();
+
+ const string inputFile = perf::TestBase::getDataPath(get<0>(params));
+ const int cn = get<1>(params);
+ int nFrame = 5;
+
+ std::vector<cv::Mat> frame_buffer(nFrame);
+ std::vector<cv::ocl::oclMat> frame_buffer_ocl;
+
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+ prepareData(cap, cn, frame_buffer);
+ cv::Mat foreground;
+ cv::ocl::oclMat foreground_d;
+
+ if(RUN_PLAIN_IMPL)
+ {
+ TEST_CYCLE()
+ {
- mog2(frame_buffer[i], foreground);
++ cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
++ mog2->set("detectShadows", false);
+ foreground.release();
+
+ for (int i = 0; i < nFrame; i++)
+ {
- cv::BackgroundSubtractorMOG2 mog2;
- mog2.set("detectShadows", false);
++ mog2->apply(frame_buffer[i], foreground);
+ }
+ }
+ SANITY_CHECK(foreground);
+ }else if(RUN_OCL_IMPL)
+ {
+ prepareData(frame_buffer, frame_buffer_ocl);
+ CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
+ OCL_TEST_CYCLE()
+ {
+ cv::ocl::MOG2 d_mog2;
+ foreground_d.release();
+ for (int i = 0; i < nFrame; i++)
+ {
+ d_mog2(frame_buffer_ocl[i], foreground_d);
+ }
+ }
+ foreground_d.download(foreground);
+ SANITY_CHECK(foreground);
+ }else
+ OCL_PERF_ELSE
+ }
+ #endif
+
+ ///////////// MOG2_GetBackgroundImage //////////////////
+ #if BUILD_WITH_VIDEO_INPUT_SUPPORT
+
+ typedef TestBaseWithParam<VideoMOG2ParamType> Video_MOG2GetBackgroundImage;
+
+ PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2,
+ ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
+ ::testing::Values(3)))
+ {
+ VideoMOG2ParamType params = GetParam();
+
+ const string inputFile = perf::TestBase::getDataPath(get<0>(params));
+ const int cn = get<1>(params);
+ int nFrame = 5;
+
+ std::vector<cv::Mat> frame_buffer(nFrame);
+ std::vector<cv::ocl::oclMat> frame_buffer_ocl;
+
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+
+ prepareData(cap, cn, frame_buffer);
+
+ cv::Mat foreground;
+ cv::Mat background;
+ cv::ocl::oclMat foreground_d;
+ cv::ocl::oclMat background_d;
+
+ if(RUN_PLAIN_IMPL)
+ {
+ TEST_CYCLE()
+ {
- mog2(frame_buffer[i], foreground);
++ cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
++ mog2->set("detectShadows", false);
+ foreground.release();
+ background.release();
+ for (int i = 0; i < nFrame; i++)
+ {
- mog2.getBackgroundImage(background);
++ mog2->apply(frame_buffer[i], foreground);
+ }
++ mog2->getBackgroundImage(background);
+ }
+ SANITY_CHECK(background);
+ }else if(RUN_OCL_IMPL)
+ {
+ prepareData(frame_buffer, frame_buffer_ocl);
+ CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
+ OCL_TEST_CYCLE()
+ {
+ cv::ocl::MOG2 d_mog2;
+ foreground_d.release();
+ background_d.release();
+ for (int i = 0; i < nFrame; i++)
+ {
+ d_mog2(frame_buffer_ocl[i], foreground_d);
+ }
+ d_mog2.getBackgroundImage(background_d);
+ }
+ background_d.download(background);
+ SANITY_CHECK(background);
+ }else
+ OCL_PERF_ELSE
+ }
+ #endif
#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 "cvconfig.h"
+#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;
--- /dev/null
- vector< pair<size_t, const void*> > args;
+ /*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) 2010-2013, Multicoreware, Inc., all rights reserved.
+ // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
+ // Third party copyrights are property of their respective owners.
+ //
+ // @Authors
+ // Jin Ma, jin@multicorewareinc.com
+ //
+ // 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 oclMaterials 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"
+ using namespace cv;
+ using namespace cv::ocl;
+ namespace cv
+ {
+ namespace ocl
+ {
+ extern const char* bgfg_mog;
+
+ typedef struct _contant_struct
+ {
+ cl_float c_Tb;
+ cl_float c_TB;
+ cl_float c_Tg;
+ cl_float c_varInit;
+ cl_float c_varMin;
+ cl_float c_varMax;
+ cl_float c_tau;
+ cl_uchar c_shadowVal;
+ }contant_struct;
+
+ cl_mem cl_constants = NULL;
+ float c_TB;
+ }
+ }
+
+ #if defined _MSC_VER
+ #define snprintf sprintf_s
+ #endif
+
+ namespace cv { namespace ocl { namespace device
+ {
+ namespace mog
+ {
+ void mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
+ int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma);
+
+ void getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio);
+
+ void loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau,
+ unsigned char shadowVal);
+
+ void mog2_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& modesUsed, oclMat& weight, oclMat& variance, oclMat& mean,
+ float alphaT, float prune, bool detectShadows, int nmixtures);
+
+ void getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures);
+ }
+ }}}
+
+ namespace mog
+ {
+ const int defaultNMixtures = 5;
+ const int defaultHistory = 200;
+ const float defaultBackgroundRatio = 0.7f;
+ const float defaultVarThreshold = 2.5f * 2.5f;
+ const float defaultNoiseSigma = 30.0f * 0.5f;
+ const float defaultInitialWeight = 0.05f;
+ }
+ void cv::ocl::BackgroundSubtractor::operator()(const oclMat&, oclMat&, float)
+ {
+
+ }
+ cv::ocl::BackgroundSubtractor::~BackgroundSubtractor()
+ {
+
+ }
+
+ cv::ocl::MOG::MOG(int nmixtures) :
+ frameSize_(0, 0), frameType_(0), nframes_(0)
+ {
+ nmixtures_ = std::min(nmixtures > 0 ? nmixtures : mog::defaultNMixtures, 8);
+ history = mog::defaultHistory;
+ varThreshold = mog::defaultVarThreshold;
+ backgroundRatio = mog::defaultBackgroundRatio;
+ noiseSigma = mog::defaultNoiseSigma;
+ }
+
+ void cv::ocl::MOG::initialize(cv::Size frameSize, int frameType)
+ {
+ CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4);
+
+ frameSize_ = frameSize;
+ frameType_ = frameType;
+
+ int ch = CV_MAT_CN(frameType);
+ int work_ch = ch;
+
+ // for each gaussian mixture of each pixel bg model we store
+ // the mixture sort key (w/sum_of_variances), the mixture weight (w),
+ // the mean (nchannels values) and
+ // the diagonal covariance matrix (another nchannels values)
+
+ weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
+ sortKey_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
+ mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch));
+ var_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch));
+
+ weight_.setTo(cv::Scalar::all(0));
+ sortKey_.setTo(cv::Scalar::all(0));
+ mean_.setTo(cv::Scalar::all(0));
+ var_.setTo(cv::Scalar::all(0));
+
+ nframes_ = 0;
+ }
+
+ void cv::ocl::MOG::operator()(const cv::ocl::oclMat& frame, cv::ocl::oclMat& fgmask, float learningRate)
+ {
+ using namespace cv::ocl::device::mog;
+
+ CV_Assert(frame.depth() == CV_8U);
+
+ int ch = frame.oclchannels();
+ int work_ch = ch;
+
+ if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.oclchannels())
+ initialize(frame.size(), frame.type());
+
+ fgmask.create(frameSize_, CV_8UC1);
+
+ ++nframes_;
+ learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history);
+ CV_Assert(learningRate >= 0.0f);
+
+ mog_ocl(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_,
+ varThreshold, learningRate, backgroundRatio, noiseSigma);
+ }
+
+ void cv::ocl::MOG::getBackgroundImage(oclMat& backgroundImage) const
+ {
+ using namespace cv::ocl::device::mog;
+
+ backgroundImage.create(frameSize_, frameType_);
+
+ cv::ocl::device::mog::getBackgroundImage_ocl(backgroundImage.oclchannels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio);
+ }
+
+ void cv::ocl::MOG::release()
+ {
+ frameSize_ = Size(0, 0);
+ frameType_ = 0;
+ nframes_ = 0;
+
+ weight_.release();
+ sortKey_.release();
+ mean_.release();
+ var_.release();
+ clReleaseMemObject(cl_constants);
+ }
+
+ static void mog_withoutLearning(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& mean, oclMat& var,
+ int nmixtures, float varThreshold, float backgroundRatio)
+ {
+ Context* clCxt = Context::getContext();
+
+ size_t local_thread[] = {32, 8, 1};
+ size_t global_thread[] = {frame.cols, frame.rows, 1};
+
+ int frame_step = (int)(frame.step/frame.elemSize());
+ int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
+ int weight_step = (int)(weight.step/weight.elemSize());
+ int mean_step = (int)(mean.step/mean.elemSize());
+ int var_step = (int)(var.step/var.elemSize());
+
+ int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
+ int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
+ fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();
+
+ int frame_offset_y = (int)(frame.offset/frame.step);
+ int frame_offset_x = (int)(frame.offset%frame.step);
+ frame_offset_x = frame_offset_x/(int)frame.elemSize();
+
+ char build_option[50];
+ if(cn == 1)
+ {
+ snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
+ }else
+ {
+ snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
+ }
+
+ String kernel_name = "mog_withoutLearning_kernel";
- args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&var.data));
++ std::vector<std::pair<size_t, const void*> > args;
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&var.data));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols));
+
- args.push_back(make_pair(sizeof(cl_float), (void*)&varThreshold));
- args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&varThreshold));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
+
- vector< pair<size_t, const void*> > args;
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y));
+
+ openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
+ }
+
+
+ static void mog_withLearning(const oclMat& frame, int cn, oclMat& fgmask_raw, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
+ int nmixtures, float varThreshold, float backgroundRatio, float learningRate, float minVar)
+ {
+ Context* clCxt = Context::getContext();
+
+ size_t local_thread[] = {32, 8, 1};
+ size_t global_thread[] = {frame.cols, frame.rows, 1};
+
+ oclMat fgmask(fgmask_raw.size(), CV_32SC1);
+
+ int frame_step = (int)(frame.step/frame.elemSize());
+ int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
+ int weight_step = (int)(weight.step/weight.elemSize());
+ int sortKey_step = (int)(sortKey.step/sortKey.elemSize());
+ int mean_step = (int)(mean.step/mean.elemSize());
+ int var_step = (int)(var.step/var.elemSize());
+
+ int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
+ int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
+ fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();
+
+ int frame_offset_y = (int)(frame.offset/frame.step);
+ int frame_offset_x = (int)(frame.offset%frame.step);
+ frame_offset_x = frame_offset_x/(int)frame.elemSize();
+
+ char build_option[50];
+ if(cn == 1)
+ {
+ snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
+ }else
+ {
+ snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
+ }
+
+ String kernel_name = "mog_withLearning_kernel";
- args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&sortKey.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&var.data));
++ std::vector<std::pair<size_t, const void*> > args;
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&sortKey.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&var.data));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&sortKey_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols));
+
- args.push_back(make_pair(sizeof(cl_float), (void*)&varThreshold));
- args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));
- args.push_back(make_pair(sizeof(cl_float), (void*)&learningRate));
- args.push_back(make_pair(sizeof(cl_float), (void*)&minVar));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&sortKey_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&varThreshold));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&learningRate));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&minVar));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
+
- vector< pair<size_t, const void*> > args;
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y));
+
+ openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
+ fgmask.convertTo(fgmask, CV_8U);
+ fgmask.copyTo(fgmask_raw);
+ }
+
+ void cv::ocl::device::mog::mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
+ int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma)
+ {
+ const float minVar = noiseSigma * noiseSigma;
+
+ if(learningRate > 0.0f)
+ mog_withLearning(frame, cn, fgmask, weight, sortKey, mean, var, nmixtures,
+ varThreshold, backgroundRatio, learningRate, minVar);
+ else
+ mog_withoutLearning(frame, cn, fgmask, weight, mean, var, nmixtures, varThreshold, backgroundRatio);
+ }
+
+ void cv::ocl::device::mog::getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio)
+ {
+ Context* clCxt = Context::getContext();
+
+ size_t local_thread[] = {32, 8, 1};
+ size_t global_thread[] = {dst.cols, dst.rows, 1};
+
+ int weight_step = (int)(weight.step/weight.elemSize());
+ int mean_step = (int)(mean.step/mean.elemSize());
+ int dst_step = (int)(dst.step/dst.elemSize());
+
+ char build_option[50];
+ if(cn == 1)
+ {
+ snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
+ }else
+ {
+ snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
+ }
+
+ String kernel_name = "getBackgroundImage_kernel";
- args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&dst.data));
++ std::vector<std::pair<size_t, const void*> > args;
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst.rows));
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst.cols));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&dst.data));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst.rows));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst.cols));
+
- args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_step));
+
- vector< pair<size_t, const void*> > args;
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&backgroundRatio));
+
+ openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
+ }
+
+ void cv::ocl::device::mog::loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal)
+ {
+ varMin = cv::min(varMin, varMax);
+ varMax = cv::max(varMin, varMax);
+
+ c_TB = TB;
+
+ _contant_struct *constants = new _contant_struct;
+ constants->c_Tb = Tb;
+ constants->c_TB = TB;
+ constants->c_Tg = Tg;
+ constants->c_varInit = varInit;
+ constants->c_varMin = varMin;
+ constants->c_varMax = varMax;
+ constants->c_tau = tau;
+ constants->c_shadowVal = shadowVal;
+
+ cl_constants = load_constant(*((cl_context*)getoclContext()), *((cl_command_queue*)getoclCommandQueue()),
+ (void *)constants, sizeof(_contant_struct));
+ }
+
+ void cv::ocl::device::mog::mog2_ocl(const oclMat& frame, int cn, oclMat& fgmaskRaw, oclMat& modesUsed, oclMat& weight, oclMat& variance,
+ oclMat& mean, float alphaT, float prune, bool detectShadows, int nmixtures)
+ {
+ oclMat fgmask(fgmaskRaw.size(), CV_32SC1);
+
+ Context* clCxt = Context::getContext();
+
+ const float alpha1 = 1.0f - alphaT;
+
+ cl_int detectShadows_flag = 0;
+ if(detectShadows)
+ detectShadows_flag = 1;
+
+ size_t local_thread[] = {32, 8, 1};
+ size_t global_thread[] = {frame.cols, frame.rows, 1};
+
+ int frame_step = (int)(frame.step/frame.elemSize());
+ int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
+ int weight_step = (int)(weight.step/weight.elemSize());
+ int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize());
+ int mean_step = (int)(mean.step/mean.elemSize());
+ int var_step = (int)(variance.step/variance.elemSize());
+
+ int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
+ int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
+ fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();
+
+ int frame_offset_y = (int)(frame.offset/frame.step);
+ int frame_offset_x = (int)(frame.offset%frame.step);
+ frame_offset_x = frame_offset_x/(int)frame.elemSize();
+
+ String kernel_name = "mog2_kernel";
- args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&variance.data));
++ std::vector<std::pair<size_t, const void*> > args;
+
+ char build_option[50];
+ if(cn == 1)
+ {
+ snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
+ }else
+ {
+ snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
+ }
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&frame.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&fgmask.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&variance.data));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.rows));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame.cols));
+
- args.push_back(make_pair(sizeof(cl_float), (void*)&alphaT));
- args.push_back(make_pair(sizeof(cl_float), (void*)&alpha1));
- args.push_back(make_pair(sizeof(cl_float), (void*)&prune));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&var_step));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&detectShadows_flag));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&alphaT));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&alpha1));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&prune));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&detectShadows_flag));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&cl_constants));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));
+
- vector< pair<size_t, const void*> > args;
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&frame_offset_y));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&cl_constants));
+
+ openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
+
+ fgmask.convertTo(fgmask, CV_8U);
+ fgmask.copyTo(fgmaskRaw);
+ }
+
+ void cv::ocl::device::mog::getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures)
+ {
+ Context* clCxt = Context::getContext();
+
+ size_t local_thread[] = {32, 8, 1};
+ size_t global_thread[] = {modesUsed.cols, modesUsed.rows, 1};
+
+ int weight_step = (int)(weight.step/weight.elemSize());
+ int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize());
+ int mean_step = (int)(mean.step/mean.elemSize());
+ int dst_step = (int)(dst.step/dst.elemSize());
+
+ int dst_y = (int)(dst.offset/dst.step);
+ int dst_x = (int)(dst.offset%dst.step);
+ dst_x = dst_x/(int)dst.elemSize();
+
+ String kernel_name = "getBackgroundImage2_kernel";
- args.push_back(make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
- args.push_back(make_pair(sizeof(cl_mem), (void*)&dst.data));
- args.push_back(make_pair(sizeof(cl_float), (void*)&c_TB));
++ std::vector<std::pair<size_t, const void*> > args;
+
+ char build_option[50];
+ if(cn == 1)
+ {
+ snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
+ }else
+ {
+ snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
+ }
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed.rows));
- args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed.cols));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&weight.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&mean.data));
++ args.push_back(std::make_pair(sizeof(cl_mem), (void*)&dst.data));
++ args.push_back(std::make_pair(sizeof(cl_float), (void*)&c_TB));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed.rows));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed.cols));
+
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst_x));
- args.push_back(make_pair(sizeof(cl_int), (void*)&dst_y));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&modesUsed_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&weight_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&mean_step));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_step));
+
-}
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_x));
++ args.push_back(std::make_pair(sizeof(cl_int), (void*)&dst_y));
+
+ openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
+ }
+
+ /////////////////////////////////////////////////////////////////
+ // MOG2
+
+ namespace mog2
+ {
+ // default parameters of gaussian background detection algorithm
+ const int defaultHistory = 500; // Learning rate; alpha = 1/defaultHistory2
+ const float defaultVarThreshold = 4.0f * 4.0f;
+ const int defaultNMixtures = 5; // maximal number of Gaussians in mixture
+ const float defaultBackgroundRatio = 0.9f; // threshold sum of weights for background test
+ const float defaultVarThresholdGen = 3.0f * 3.0f;
+ const float defaultVarInit = 15.0f; // initial variance for new components
+ const float defaultVarMax = 5.0f * defaultVarInit;
+ const float defaultVarMin = 4.0f;
+
+ // additional parameters
+ const float defaultfCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
+ const unsigned char defaultnShadowDetection = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
+ const float defaultfTau = 0.5f; // Tau - shadow threshold, see the paper for explanation
+ }
+
+ cv::ocl::MOG2::MOG2(int nmixtures) : frameSize_(0, 0), frameType_(0), nframes_(0)
+ {
+ nmixtures_ = nmixtures > 0 ? nmixtures : mog2::defaultNMixtures;
+
+ history = mog2::defaultHistory;
+ varThreshold = mog2::defaultVarThreshold;
+ bShadowDetection = true;
+
+ backgroundRatio = mog2::defaultBackgroundRatio;
+ fVarInit = mog2::defaultVarInit;
+ fVarMax = mog2::defaultVarMax;
+ fVarMin = mog2::defaultVarMin;
+
+ varThresholdGen = mog2::defaultVarThresholdGen;
+ fCT = mog2::defaultfCT;
+ nShadowDetection = mog2::defaultnShadowDetection;
+ fTau = mog2::defaultfTau;
+ }
+
+ void cv::ocl::MOG2::initialize(cv::Size frameSize, int frameType)
+ {
+ using namespace cv::ocl::device::mog;
+ CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4);
+
+ frameSize_ = frameSize;
+ frameType_ = frameType;
+ nframes_ = 0;
+
+ int ch = CV_MAT_CN(frameType);
+ int work_ch = ch;
+
+ // for each gaussian mixture of each pixel bg model we store ...
+ // the mixture weight (w),
+ // the mean (nchannels values) and
+ // the covariance
+ weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
+ weight_.setTo(Scalar::all(0));
+
+ variance_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
+ variance_.setTo(Scalar::all(0));
+
+ mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); //4 channels
+ mean_.setTo(Scalar::all(0));
+
+ //make the array for keeping track of the used modes per pixel - all zeros at start
+ bgmodelUsedModes_.create(frameSize_, CV_32FC1);
+ bgmodelUsedModes_.setTo(cv::Scalar::all(0));
+
+ loadConstants(varThreshold, backgroundRatio, varThresholdGen, fVarInit, fVarMin, fVarMax, fTau, nShadowDetection);
+ }
+
+ void cv::ocl::MOG2::operator()(const oclMat& frame, oclMat& fgmask, float learningRate)
+ {
+ using namespace cv::ocl::device::mog;
+
+ int ch = frame.oclchannels();
+ int work_ch = ch;
+
+ if (nframes_ == 0 || learningRate >= 1.0f || frame.size() != frameSize_ || work_ch != mean_.oclchannels())
+ initialize(frame.size(), frame.type());
+
+ fgmask.create(frameSize_, CV_8UC1);
+ fgmask.setTo(cv::Scalar::all(0));
+
+ ++nframes_;
+ learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(2 * nframes_, history);
+ CV_Assert(learningRate >= 0.0f);
+
+ mog2_ocl(frame, frame.oclchannels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_, learningRate, -learningRate * fCT, bShadowDetection, nmixtures_);
+ }
+
+ void cv::ocl::MOG2::getBackgroundImage(oclMat& backgroundImage) const
+ {
+ using namespace cv::ocl::device::mog;
+
+ backgroundImage.create(frameSize_, frameType_);
+
+ cv::ocl::device::mog::getBackgroundImage2_ocl(backgroundImage.oclchannels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, nmixtures_);
+ }
+
+ void cv::ocl::MOG2::release()
+ {
+ frameSize_ = Size(0, 0);
+ frameType_ = 0;
+ nframes_ = 0;
+
+ weight_.release();
+ variance_.release();
+ mean_.release();
+
+ bgmodelUsedModes_.release();
++}
Ptr<FilterEngine_GPU> f = createGaussianFilter_GPU(src.type(), ksize, sigma1, sigma2, bordertype);
f->apply(src, dst);
}
- std::vector<pair<size_t , const void *> > args;
+
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Adaptive Bilateral Filter
+
+ void cv::ocl::adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor, int borderType)
+ {
+ CV_Assert((ksize.width & 1) && (ksize.height & 1)); // ksize must be odd
+ CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3); // source must be 8bit RGB image
+ if( sigmaSpace <= 0 )
+ sigmaSpace = 1;
+ Mat lut(Size(ksize.width, ksize.height), CV_32FC1);
+ double sigma2 = sigmaSpace * sigmaSpace;
+ int idx = 0;
+ int w = ksize.width / 2;
+ int h = ksize.height / 2;
+ for(int y=-h; y<=h; y++)
+ for(int x=-w; x<=w; x++)
+ {
+ lut.at<float>(idx++) = sigma2 / (sigma2 + x * x + y * y);
+ }
+ oclMat dlut(lut);
+ int depth = src.depth();
+ int cn = src.oclchannels();
+
+ normalizeAnchor(anchor, ksize);
+ const static String kernelName = "edgeEnhancingFilter";
+
+ dst.create(src.size(), src.type());
+
+ char btype[30];
+ switch(borderType)
+ {
+ case BORDER_CONSTANT:
+ sprintf(btype, "BORDER_CONSTANT");
+ break;
+ case BORDER_REPLICATE:
+ sprintf(btype, "BORDER_REPLICATE");
+ break;
+ case BORDER_REFLECT:
+ sprintf(btype, "BORDER_REFLECT");
+ break;
+ case BORDER_WRAP:
+ sprintf(btype, "BORDER_WRAP");
+ break;
+ case BORDER_REFLECT101:
+ sprintf(btype, "BORDER_REFLECT_101");
+ break;
+ default:
+ CV_Error(CV_StsBadArg, "This border type is not supported");
+ break;
+ }
+
+ //the following constants may be adjusted for performance concerns
+ const static size_t blockSizeX = 64, blockSizeY = 1, EXTRA = ksize.height - 1;
+
+ //Normalize the result by default
+ const float alpha = ksize.height * ksize.width;
+
+ const size_t gSize = blockSizeX - ksize.width / 2 * 2;
+ const size_t globalSizeX = (src.cols) % gSize == 0 ?
+ src.cols / gSize * blockSizeX :
+ (src.cols / gSize + 1) * blockSizeX;
+ const size_t rows_per_thread = 1 + EXTRA;
+ const size_t globalSizeY = ((src.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
+ ((src.rows + rows_per_thread - 1) / rows_per_thread) :
+ (((src.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
+
+ size_t globalThreads[3] = { globalSizeX, globalSizeY, 1};
+ size_t localThreads[3] = { blockSizeX, blockSizeY, 1};
+
+ char build_options[250];
+
+ //LDATATYPESIZE is sizeof local data store. This is to exemplify effect of LDS on kernel performance
+ sprintf(build_options,
+ "-D VAR_PER_CHANNEL=1 -D CALCVAR=1 -D FIXED_WEIGHT=0 -D EXTRA=%d"
+ " -D THREADS=%d -D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
+ static_cast<int>(EXTRA), static_cast<int>(blockSizeX), anchor.x, anchor.y, ksize.width, ksize.height, btype);
+
-}
++ std::vector<std::pair<size_t , const void *> > args;
+ args.push_back(std::make_pair(sizeof(cl_mem), &src.data));
+ args.push_back(std::make_pair(sizeof(cl_mem), &dst.data));
+ args.push_back(std::make_pair(sizeof(cl_float), (void *)&alpha));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.wholerows));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.wholecols));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.step));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.step));
+ args.push_back(std::make_pair(sizeof(cl_mem), &dlut.data));
+ int lut_step = dlut.step1();
+ args.push_back(std::make_pair(sizeof(cl_int), (void *)&lut_step));
+
+ openCLExecuteKernel(Context::getContext(), &filtering_adaptive_bilateral, kernelName,
+ globalThreads, localThreads, args, cn, depth, build_options);
++}
void cv::ocl::gemm(const oclMat&, const oclMat&, double,
const oclMat&, double, oclMat&, int)
{
- CV_Error(CV_StsNotImplemented, "OpenCL BLAS is not implemented");
+ CV_Error(Error::StsNotImplemented, "OpenCL BLAS is not implemented");
}
+
+ void cv::ocl::clBlasSetup()
+ {
+ CV_Error(CV_StsNotImplemented, "OpenCL BLAS is not implemented");
+ }
+
+ void cv::ocl::clBlasTeardown()
+ {
+ //intentionally do nothing
+ }
+
#else
#include "clAmdBlas.h"
using namespace cv;
--- /dev/null
- cv::BackgroundSubtractorMOG mog_gold;
+ /*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) 2010-2013, Multicoreware, Inc., all rights reserved.
+ // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
+ // Third party copyrights are property of their respective owners.
+ //
+ // @Authors
+ // Jin Ma, jin@multicorewareinc.com
+ //
+ // 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 oclMaterials 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_OPENCL
+
+ using namespace cv;
+ using namespace cv::ocl;
+ using namespace cvtest;
+ using namespace testing;
+ using namespace std;
+
+ extern string workdir;
+ //////////////////////////////////////////////////////
+ // MOG
+
+ namespace
+ {
+ IMPLEMENT_PARAM_CLASS(UseGray, bool)
+ IMPLEMENT_PARAM_CLASS(LearningRate, double)
+ }
+
+ PARAM_TEST_CASE(mog, UseGray, LearningRate, bool)
+ {
+ bool useGray;
+ double learningRate;
+ bool useRoi;
+
+ virtual void SetUp()
+ {
+ useGray = GET_PARAM(0);
+
+ learningRate = GET_PARAM(1);
+
+ useRoi = GET_PARAM(2);
+ }
+ };
+
+ TEST_P(mog, Update)
+ {
+ std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+
+ cv::Mat frame;
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+
+ cv::ocl::MOG mog;
+ cv::ocl::oclMat foreground = createMat_ocl(frame.size(), CV_8UC1, useRoi);
+
- mog_gold(frame, foreground_gold, learningRate);
++ Ptr<cv::BackgroundSubtractorMOG> mog_gold = createBackgroundSubtractorMOG();
+ cv::Mat foreground_gold;
+
+ for (int i = 0; i < 10; ++i)
+ {
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+
+ if (useGray)
+ {
+ cv::Mat temp;
+ cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
+ cv::swap(temp, frame);
+ }
+
+ mog(loadMat_ocl(frame, useRoi), foreground, (float)learningRate);
+
- cv::BackgroundSubtractorMOG2 mog2_gold;
- mog2_gold.set("detectShadows", detectShadow);
++ mog_gold->apply(frame, foreground_gold, learningRate);
+
+ EXPECT_MAT_NEAR(foreground_gold, foreground, 0.0);
+ }
+ }
+ INSTANTIATE_TEST_CASE_P(OCL_Video, mog, testing::Combine(
+ testing::Values(UseGray(false), UseGray(true)),
+ testing::Values(LearningRate(0.0), LearningRate(0.01)),
+ Values(true, false)));
+
+ //////////////////////////////////////////////////////
+ // MOG2
+
+ namespace
+ {
+ IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
+ }
+
+ PARAM_TEST_CASE(mog2, UseGray, DetectShadow, bool)
+ {
+ bool useGray;
+ bool detectShadow;
+ bool useRoi;
+ virtual void SetUp()
+ {
+ useGray = GET_PARAM(0);
+ detectShadow = GET_PARAM(1);
+ useRoi = GET_PARAM(2);
+ }
+ };
+
+ TEST_P(mog2, Update)
+ {
+ std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+
+ cv::Mat frame;
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+
+ cv::ocl::MOG2 mog2;
+ mog2.bShadowDetection = detectShadow;
+ cv::ocl::oclMat foreground = createMat_ocl(frame.size(), CV_8UC1, useRoi);
+
- mog2_gold(frame, foreground_gold);
++ cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = createBackgroundSubtractorMOG2();
++ mog2_gold->set("detectShadows", detectShadow);
+ cv::Mat foreground_gold;
+
+ for (int i = 0; i < 10; ++i)
+ {
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+
+ if (useGray)
+ {
+ cv::Mat temp;
+ cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
+ cv::swap(temp, frame);
+ }
+
+ mog2(loadMat_ocl(frame, useRoi), foreground);
+
- cv::BackgroundSubtractorMOG2 mog2_gold;
- mog2_gold.set("detectShadows", detectShadow);
++ mog2_gold->apply(frame, foreground_gold);
+
+ if (detectShadow)
+ EXPECT_MAT_SIMILAR(foreground_gold, foreground, 15e-3)
+ else
+ EXPECT_MAT_NEAR(foreground_gold, foreground, 0)
+ }
+ }
+
+ TEST_P(mog2, getBackgroundImage)
+ {
+ if (useGray)
+ return;
+
+ std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
+ cv::VideoCapture cap(inputFile);
+ ASSERT_TRUE(cap.isOpened());
+
+ cv::Mat frame;
+
+ cv::ocl::MOG2 mog2;
+ mog2.bShadowDetection = detectShadow;
+ cv::ocl::oclMat foreground;
+
- mog2_gold(frame, foreground_gold);
++ cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = createBackgroundSubtractorMOG2();
++ mog2_gold->set("detectShadows", detectShadow);
+ cv::Mat foreground_gold;
+
+ for (int i = 0; i < 10; ++i)
+ {
+ cap >> frame;
+ ASSERT_FALSE(frame.empty());
+
+ mog2(loadMat_ocl(frame, useRoi), foreground);
+
- mog2_gold.getBackgroundImage(background_gold);
++ mog2_gold->apply(frame, foreground_gold);
+ }
+
+ cv::ocl::oclMat background = createMat_ocl(frame.size(), frame.type(), useRoi);
+ mog2.getBackgroundImage(background);
+
+ cv::Mat background_gold;
++ mog2_gold->getBackgroundImage(background_gold);
+
+ EXPECT_MAT_NEAR(background_gold, background, 1.0);
+ }
+
+ INSTANTIATE_TEST_CASE_P(OCL_Video, mog2, testing::Combine(
+ testing::Values(UseGray(true), UseGray(false)),
+ testing::Values(DetectShadow(true), DetectShadow(false)),
+ Values(true, false)));
+
+ #endif
--- /dev/null
-#include "opencv2/core/core.hpp"
-#include "opencv2/imgproc/imgproc.hpp"
-#include "opencv2/highgui/highgui.hpp"
-#include "opencv2/ocl/ocl.hpp"
+ // This sample shows the difference of adaptive bilateral filter and bilateral filter.
- "{ i | input | | specify input image }"
- "{ k | ksize | 5 | specify kernel size }";
++#include "opencv2/core.hpp"
++#include "opencv2/core/utility.hpp"
++#include "opencv2/imgproc.hpp"
++#include "opencv2/highgui.hpp"
++#include "opencv2/ocl.hpp"
+
+ using namespace cv;
+ using namespace std;
+
+
+ int main( int argc, const char** argv )
+ {
+ const char* keys =
-}
++ "{ i input | | specify input image }"
++ "{ k ksize | 5 | specify kernel size }";
+ CommandLineParser cmd(argc, argv, keys);
+ string src_path = cmd.get<string>("i");
+ int ks = cmd.get<int>("k");
+ const char * winName[] = {"input", "adaptive bilateral CPU", "adaptive bilateral OpenCL", "bilateralFilter OpenCL"};
+
+ Mat src = imread(src_path);
+ Mat abFilterCPU;
+ if(src.empty()){
+ //cout << "error read image: " << src_path << endl;
+ return -1;
+ }
+
+ std::vector<ocl::Info> infos;
+ ocl::getDevice(infos);
+
+ ocl::oclMat dsrc(src), dABFilter, dBFilter;
+
+ Size ksize(ks, ks);
+ adaptiveBilateralFilter(src,abFilterCPU, ksize, 10);
+ ocl::adaptiveBilateralFilter(dsrc, dABFilter, ksize, 10);
+ ocl::bilateralFilter(dsrc, dBFilter, ks, 30, 9);
+
+ Mat abFilter = dABFilter;
+ Mat bFilter = dBFilter;
+ imshow(winName[0], src);
+
+ imshow(winName[1], abFilterCPU);
+
+ imshow(winName[2], abFilter);
+
+ imshow(winName[3], bFilter);
+
+ waitKey();
+ return 0;
+
++}
--- /dev/null
-#include "opencv2/core/core.hpp"
-#include "opencv2/ocl/ocl.hpp"
-#include "opencv2/highgui/highgui.hpp"
+ #include <iostream>
+ #include <string>
+
- "{ c | camera | false | use camera }"
- "{ f | file | 768x576.avi | input video file }"
- "{ m | method | mog | method (mog, mog2) }"
- "{ h | help | false | print help message }");
++#include "opencv2/core.hpp"
++#include "opencv2/core/utility.hpp"
++#include "opencv2/ocl.hpp"
++#include "opencv2/highgui.hpp"
+
+ using namespace std;
+ using namespace cv;
+ using namespace cv::ocl;
+
+ #define M_MOG 1
+ #define M_MOG2 2
+
+ int main(int argc, const char** argv)
+ {
+
+ cv::CommandLineParser cmd(argc, argv,
- cmd.printParams();
++ "{ c camera | false | use camera }"
++ "{ f file | 768x576.avi | input video file }"
++ "{ m method | mog | method (mog, mog2) }"
++ "{ h help | false | print help message }");
+
+ if (cmd.get<bool>("help"))
+ {
+ cout << "Usage : bgfg_segm [options]" << endl;
+ cout << "Avaible options:" << endl;
++ cmd.printMessage();
+ return 0;
+ }
+
+ bool useCamera = cmd.get<bool>("camera");
+ string file = cmd.get<string>("file");
+ string method = cmd.get<string>("method");
+
+ if (method != "mog" && method != "mog2")
+ {
+ cerr << "Incorrect method" << endl;
+ return -1;
+ }
+
+ int m = method == "mog" ? M_MOG : M_MOG2;
+
+ VideoCapture cap;
+
+ if (useCamera)
+ cap.open(0);
+ else
+ cap.open(file);
+
+ if (!cap.isOpened())
+ {
+ cerr << "can not open camera or video file" << endl;
+ return -1;
+ }
+
+ std::vector<cv::ocl::Info>info;
+ cv::ocl::getDevice(info);
+
+ Mat frame;
+ cap >> frame;
+
+ oclMat d_frame(frame);
+
+ cv::ocl::MOG mog;
+ cv::ocl::MOG2 mog2;
+
+ oclMat d_fgmask;
+ oclMat d_fgimg;
+ oclMat d_bgimg;
+
+ d_fgimg.create(d_frame.size(), d_frame.type());
+
+ Mat fgmask;
+ Mat fgimg;
+ Mat bgimg;
+
+ switch (m)
+ {
+ case M_MOG:
+ mog(d_frame, d_fgmask, 0.01f);
+ break;
+
+ case M_MOG2:
+ mog2(d_frame, d_fgmask);
+ break;
+ }
+
+ for(;;)
+ {
+ cap >> frame;
+ if (frame.empty())
+ break;
+ d_frame.upload(frame);
+
+ int64 start = cv::getTickCount();
+
+ //update the model
+ switch (m)
+ {
+ case M_MOG:
+ mog(d_frame, d_fgmask, 0.01f);
+ mog.getBackgroundImage(d_bgimg);
+ break;
+
+ case M_MOG2:
+ mog2(d_frame, d_fgmask);
+ mog2.getBackgroundImage(d_bgimg);
+ break;
+ }
+
+ double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
+ std::cout << "FPS : " << fps << std::endl;
+
+ d_fgimg.setTo(Scalar::all(0));
+ d_frame.copyTo(d_fgimg, d_fgmask);
+
+ d_fgmask.download(fgmask);
+ d_fgimg.download(fgimg);
+ if (!d_bgimg.empty())
+ d_bgimg.download(bgimg);
+
+ imshow("image", frame);
+ imshow("foreground mask", fgmask);
+ imshow("foreground image", fgimg);
+ if (!bgimg.empty())
+ imshow("mean background image", bgimg);
+
+ int key = waitKey(30);
+ if (key == 27)
+ break;
+ }
+
+ return 0;
+ }