refactored Laplacian filter

pull/1010/head
Vladislav Vinogradov 12 years ago
parent 1eedc6c42a
commit ee7eb1b807
  1. 18
      modules/gpufilters/include/opencv2/gpufilters.hpp
  2. 52
      modules/gpufilters/perf/perf_filters.cpp
  3. 30
      modules/gpufilters/src/filtering.cpp
  4. 16
      modules/gpufilters/test/test_filters.cpp

@ -113,17 +113,23 @@ inline void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray ker
f->apply(src, dst, stream); f->apply(src, dst, stream);
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
// Laplacian Filter
//! applies Laplacian operator to the image //! applies Laplacian operator to the image
//! supports only ksize = 1 and ksize = 3 //! supports only ksize = 1 and ksize = 3
CV_EXPORTS void Laplacian(const GpuMat& src, GpuMat& dst, int ddepth, int ksize = 1, double scale = 1, int borderType = BORDER_DEFAULT, Stream& stream = Stream::Null()); CV_EXPORTS Ptr<Filter> createLaplacianFilter(int srcType, int dstType, int ksize = 1, double scale = 1,
int borderMode = BORDER_DEFAULT, Scalar borderVal = Scalar::all(0));
__OPENCV_GPUFILTERS_DEPR_BEFORE__ void Laplacian(InputArray src, OutputArray dst, int ddepth,
int ksize = 1, double scale = 1, int borderType = BORDER_DEFAULT,
Stream& stream = Stream::Null()) __OPENCV_GPUFILTERS_DEPR_AFTER__;
inline void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize, double scale, int borderType, Stream& stream)
{
Ptr<gpu::Filter> f = gpu::createLaplacianFilter(src.type(), ddepth, ksize, scale, borderType);
f->apply(src, dst, stream);
}

@ -124,17 +124,10 @@ PERF_TEST_P(Sz_Type_KernelSz, Filter2D, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Sobel // Laplacian
PERF_TEST_P(Sz_Type_KernelSz, Sobel, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1), Values(3, 5, 7, 9, 11, 13, 15))) PERF_TEST_P(Sz_Type_KernelSz, Laplacian, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4), Values(1, 3)))
{ {
declare.time(20.0); declare.time(20.0);
@ -149,9 +142,10 @@ PERF_TEST_P(Sz_Type_KernelSz, Sobel, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8U
{ {
const cv::gpu::GpuMat d_src(src); const cv::gpu::GpuMat d_src(src);
cv::gpu::GpuMat dst; cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::Sobel(d_src, dst, -1, 1, 1, d_buf, ksize); cv::Ptr<cv::gpu::Filter> laplacian = cv::gpu::createLaplacianFilter(d_src.type(), -1, ksize);
TEST_CYCLE() laplacian->apply(d_src, dst);
GPU_SANITY_CHECK(dst); GPU_SANITY_CHECK(dst);
} }
@ -159,21 +153,29 @@ PERF_TEST_P(Sz_Type_KernelSz, Sobel, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8U
{ {
cv::Mat dst; cv::Mat dst;
TEST_CYCLE() cv::Sobel(src, dst, -1, 1, 1, ksize); TEST_CYCLE() cv::Laplacian(src, dst, -1, ksize);
CPU_SANITY_CHECK(dst); CPU_SANITY_CHECK(dst);
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Scharr // Sobel
PERF_TEST_P(Sz_Type, Scharr, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1))) PERF_TEST_P(Sz_Type_KernelSz, Sobel, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1), Values(3, 5, 7, 9, 11, 13, 15)))
{ {
declare.time(20.0); declare.time(20.0);
const cv::Size size = GET_PARAM(0); const cv::Size size = GET_PARAM(0);
const int type = GET_PARAM(1); const int type = GET_PARAM(1);
const int ksize = GET_PARAM(2);
cv::Mat src(size, type); cv::Mat src(size, type);
declare.in(src, WARMUP_RNG); declare.in(src, WARMUP_RNG);
@ -184,7 +186,7 @@ PERF_TEST_P(Sz_Type, Scharr, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8
cv::gpu::GpuMat dst; cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_buf; cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::Scharr(d_src, dst, -1, 1, 0, d_buf); TEST_CYCLE() cv::gpu::Sobel(d_src, dst, -1, 1, 1, d_buf, ksize);
GPU_SANITY_CHECK(dst); GPU_SANITY_CHECK(dst);
} }
@ -192,22 +194,21 @@ PERF_TEST_P(Sz_Type, Scharr, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8
{ {
cv::Mat dst; cv::Mat dst;
TEST_CYCLE() cv::Scharr(src, dst, -1, 1, 0); TEST_CYCLE() cv::Sobel(src, dst, -1, 1, 1, ksize);
CPU_SANITY_CHECK(dst); CPU_SANITY_CHECK(dst);
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// GaussianBlur // Scharr
PERF_TEST_P(Sz_Type_KernelSz, GaussianBlur, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1), Values(3, 5, 7, 9, 11, 13, 15))) PERF_TEST_P(Sz_Type, Scharr, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1)))
{ {
declare.time(20.0); declare.time(20.0);
const cv::Size size = GET_PARAM(0); const cv::Size size = GET_PARAM(0);
const int type = GET_PARAM(1); const int type = GET_PARAM(1);
const int ksize = GET_PARAM(2);
cv::Mat src(size, type); cv::Mat src(size, type);
declare.in(src, WARMUP_RNG); declare.in(src, WARMUP_RNG);
@ -218,7 +219,7 @@ PERF_TEST_P(Sz_Type_KernelSz, GaussianBlur, Combine(GPU_TYPICAL_MAT_SIZES, Value
cv::gpu::GpuMat dst; cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_buf; cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::GaussianBlur(d_src, dst, cv::Size(ksize, ksize), d_buf, 0.5); TEST_CYCLE() cv::gpu::Scharr(d_src, dst, -1, 1, 0, d_buf);
GPU_SANITY_CHECK(dst); GPU_SANITY_CHECK(dst);
} }
@ -226,16 +227,16 @@ PERF_TEST_P(Sz_Type_KernelSz, GaussianBlur, Combine(GPU_TYPICAL_MAT_SIZES, Value
{ {
cv::Mat dst; cv::Mat dst;
TEST_CYCLE() cv::GaussianBlur(src, dst, cv::Size(ksize, ksize), 0.5); TEST_CYCLE() cv::Scharr(src, dst, -1, 1, 0);
CPU_SANITY_CHECK(dst); CPU_SANITY_CHECK(dst);
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Laplacian // GaussianBlur
PERF_TEST_P(Sz_Type_KernelSz, Laplacian, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4), Values(1, 3))) PERF_TEST_P(Sz_Type_KernelSz, GaussianBlur, Combine(GPU_TYPICAL_MAT_SIZES, Values(CV_8UC1, CV_8UC4, CV_32FC1), Values(3, 5, 7, 9, 11, 13, 15)))
{ {
declare.time(20.0); declare.time(20.0);
@ -250,8 +251,9 @@ PERF_TEST_P(Sz_Type_KernelSz, Laplacian, Combine(GPU_TYPICAL_MAT_SIZES, Values(C
{ {
const cv::gpu::GpuMat d_src(src); const cv::gpu::GpuMat d_src(src);
cv::gpu::GpuMat dst; cv::gpu::GpuMat dst;
cv::gpu::GpuMat d_buf;
TEST_CYCLE() cv::gpu::Laplacian(d_src, dst, -1, ksize); TEST_CYCLE() cv::gpu::GaussianBlur(d_src, dst, cv::Size(ksize, ksize), d_buf, 0.5);
GPU_SANITY_CHECK(dst); GPU_SANITY_CHECK(dst);
} }
@ -259,7 +261,7 @@ PERF_TEST_P(Sz_Type_KernelSz, Laplacian, Combine(GPU_TYPICAL_MAT_SIZES, Values(C
{ {
cv::Mat dst; cv::Mat dst;
TEST_CYCLE() cv::Laplacian(src, dst, -1, ksize); TEST_CYCLE() cv::GaussianBlur(src, dst, cv::Size(ksize, ksize), 0.5);
CPU_SANITY_CHECK(dst); CPU_SANITY_CHECK(dst);
} }

@ -51,6 +51,8 @@ Ptr<Filter> cv::gpu::createBoxFilter(int, int, Size, Point, int, Scalar) { throw
Ptr<Filter> cv::gpu::createLinearFilter(int, int, InputArray, Point, int, Scalar) { throw_no_cuda(); return Ptr<Filter>(); } Ptr<Filter> cv::gpu::createLinearFilter(int, int, InputArray, Point, int, Scalar) { throw_no_cuda(); return Ptr<Filter>(); }
Ptr<Filter> cv::gpu::createLaplacianFilter(int, int, int, double, int, Scalar) { throw_no_cuda(); return Ptr<Filter>(); }
Ptr<FilterEngine_GPU> cv::gpu::createFilter2D_GPU(const Ptr<BaseFilter_GPU>&, int, int) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); } Ptr<FilterEngine_GPU> cv::gpu::createFilter2D_GPU(const Ptr<BaseFilter_GPU>&, int, int) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); }
Ptr<FilterEngine_GPU> cv::gpu::createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>&, const Ptr<BaseColumnFilter_GPU>&, int, int, int) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); } Ptr<FilterEngine_GPU> cv::gpu::createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>&, const Ptr<BaseColumnFilter_GPU>&, int, int, int) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); }
Ptr<FilterEngine_GPU> cv::gpu::createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>&, const Ptr<BaseColumnFilter_GPU>&, int, int, int, GpuMat&) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); } Ptr<FilterEngine_GPU> cv::gpu::createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>&, const Ptr<BaseColumnFilter_GPU>&, int, int, int, GpuMat&) { throw_no_cuda(); return Ptr<FilterEngine_GPU>(0); }
@ -84,7 +86,6 @@ void cv::gpu::Scharr(const GpuMat&, GpuMat&, int, int, int, double, int, int) {
void cv::gpu::Scharr(const GpuMat&, GpuMat&, int, int, int, GpuMat&, double, int, int, Stream&) { throw_no_cuda(); } void cv::gpu::Scharr(const GpuMat&, GpuMat&, int, int, int, GpuMat&, double, int, int, Stream&) { throw_no_cuda(); }
void cv::gpu::GaussianBlur(const GpuMat&, GpuMat&, Size, double, double, int, int) { throw_no_cuda(); } void cv::gpu::GaussianBlur(const GpuMat&, GpuMat&, Size, double, double, int, int) { throw_no_cuda(); }
void cv::gpu::GaussianBlur(const GpuMat&, GpuMat&, Size, GpuMat&, double, double, int, int, Stream&) { throw_no_cuda(); } void cv::gpu::GaussianBlur(const GpuMat&, GpuMat&, Size, GpuMat&, double, double, int, int, Stream&) { throw_no_cuda(); }
void cv::gpu::Laplacian(const GpuMat&, GpuMat&, int, int, double, int, Stream&) { throw_no_cuda(); }
#else #else
@ -293,30 +294,24 @@ Ptr<Filter> cv::gpu::createLinearFilter(int srcType, int dstType, InputArray ker
return new LinearFilter(srcType, dstType, kernel, anchor, borderMode, borderVal); return new LinearFilter(srcType, dstType, kernel, anchor, borderMode, borderVal);
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
// Laplacian Filter
Ptr<Filter> cv::gpu::createLaplacianFilter(int srcType, int dstType, int ksize, double scale, int borderMode, Scalar borderVal)
void cv::gpu::Laplacian(const GpuMat& src, GpuMat& dst, int ddepth, int ksize, double scale, int borderType, Stream& stream)
{ {
CV_Assert(ksize == 1 || ksize == 3); CV_Assert( ksize == 1 || ksize == 3 );
static const int K[2][9] = static const float K[2][9] =
{ {
{0, 1, 0, 1, -4, 1, 0, 1, 0}, {0.0f, 1.0f, 0.0f, 1.0f, -4.0f, 1.0f, 0.0f, 1.0f, 0.0f},
{2, 0, 2, 0, -8, 0, 2, 0, 2} {2.0f, 0.0f, 2.0f, 0.0f, -8.0f, 0.0f, 2.0f, 0.0f, 2.0f}
}; };
Mat kernel(3, 3, CV_32S, (void*)K[ksize == 3]);
Mat kernel(3, 3, CV_32FC1, (void*)K[ksize == 3]);
if (scale != 1) if (scale != 1)
kernel *= scale; kernel *= scale;
Ptr<gpu::Filter> f = gpu::createLinearFilter(src.type(), ddepth, kernel, Point(-1,-1), borderType); return gpu::createLinearFilter(srcType, dstType, kernel, Point(-1,-1), borderMode, borderVal);
f->apply(src, dst, stream);
} }
@ -347,6 +342,7 @@ void cv::gpu::Laplacian(const GpuMat& src, GpuMat& dst, int ddepth, int ksize, d
namespace namespace

@ -170,18 +170,6 @@ INSTANTIATE_TEST_CASE_P(GPU_Filters, Filter2D, testing::Combine(
testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT)), testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT)),
WHOLE_SUBMAT)); WHOLE_SUBMAT));
///////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////
// Laplacian // Laplacian
@ -209,8 +197,10 @@ GPU_TEST_P(Laplacian, Accuracy)
{ {
cv::Mat src = randomMat(size, type); cv::Mat src = randomMat(size, type);
cv::Ptr<cv::gpu::Filter> laplacian = cv::gpu::createLaplacianFilter(src.type(), -1, ksize.width);
cv::gpu::GpuMat dst = createMat(size, type, useRoi); cv::gpu::GpuMat dst = createMat(size, type, useRoi);
cv::gpu::Laplacian(loadMat(src, useRoi), dst, -1, ksize.width); laplacian->apply(loadMat(src, useRoi), dst);
cv::Mat dst_gold; cv::Mat dst_gold;
cv::Laplacian(src, dst_gold, -1, ksize.width); cv::Laplacian(src, dst_gold, -1, ksize.width);

Loading…
Cancel
Save