From 16e74ab306dc78c65b564589f2add1ded424e328 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Tue, 1 Feb 2011 10:46:19 +0000 Subject: [PATCH] added buffered version of norm, updated performance sample and docs --- doc/gpu_matrix_reductions.tex | 11 +++++++++-- modules/gpu/include/opencv2/gpu/gpu.hpp | 7 ++++++- modules/gpu/src/matrix_reductions.cpp | 13 ++++++++++--- samples/gpu/performance/tests.cpp | 12 +++++++----- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/doc/gpu_matrix_reductions.tex b/doc/gpu_matrix_reductions.tex index 70b5c5f..875d751 100644 --- a/doc/gpu_matrix_reductions.tex +++ b/doc/gpu_matrix_reductions.tex @@ -17,12 +17,19 @@ See also: \cvCppCross{meanStdDev}. \cvCppFunc{gpu::norm} Returns norm of matrix (or of two matrices difference). -\cvdefCpp{double norm(const GpuMat\& src1, int normType=NORM\_L2);} +\cvdefCpp{double norm(const GpuMat\& src, int normType=NORM\_L2);} \begin{description} -\cvarg{src1}{Source matrix. Any matrices except 64F are supported.} +\cvarg{src}{Source matrix. Any matrices except 64F are supported.} \cvarg{normType}{Norm type. \texttt{NORM\_L1}, \texttt{NORM\_L2} and \texttt{NORM\_INF} are supported for now.} \end{description} +\cvdefCpp{double norm(const GpuMat\& src, int normType, GpuMat\& buf);} +\begin{description} +\cvarg{src}{Source matrix. Any matrices except 64F are supported.} +\cvarg{normType}{Norm type. \texttt{NORM\_L1}, \texttt{NORM\_L2} and \texttt{NORM\_INF} are supported for now.} +\cvarg{buf}{Optional buffer to avoid extra memory allocations. It's resized automatically.} +\end{description} + \cvdefCpp{double norm(const GpuMat\& src1, const GpuMat\& src2,\par int normType=NORM\_L2);} \begin{description} diff --git a/modules/gpu/include/opencv2/gpu/gpu.hpp b/modules/gpu/include/opencv2/gpu/gpu.hpp index 77f2acd..df30a8c 100644 --- a/modules/gpu/include/opencv2/gpu/gpu.hpp +++ b/modules/gpu/include/opencv2/gpu/gpu.hpp @@ -750,9 +750,14 @@ namespace cv //! computes norm of array //! supports NORM_INF, NORM_L1, NORM_L2 - //! supports only CV_8UC1 type + //! supports all matrices except 64F CV_EXPORTS double norm(const GpuMat& src1, int normType=NORM_L2); + //! computes norm of array + //! supports NORM_INF, NORM_L1, NORM_L2 + //! supports all matrices except 64F + CV_EXPORTS double norm(const GpuMat& src1, int normType, GpuMat& buf); + //! computes norm of the difference between two arrays //! supports NORM_INF, NORM_L1, NORM_L2 //! supports only CV_8UC1 type diff --git a/modules/gpu/src/matrix_reductions.cpp b/modules/gpu/src/matrix_reductions.cpp index 3ef13f6..f9baf66 100644 --- a/modules/gpu/src/matrix_reductions.cpp +++ b/modules/gpu/src/matrix_reductions.cpp @@ -49,6 +49,7 @@ using namespace cv::gpu; void cv::gpu::meanStdDev(const GpuMat&, Scalar&, Scalar&) { throw_nogpu(); } double cv::gpu::norm(const GpuMat&, int) { throw_nogpu(); return 0.0; } +double cv::gpu::norm(const GpuMat&, int, GpuMat&) { throw_nogpu(); return 0.0; } double cv::gpu::norm(const GpuMat&, const GpuMat&, int) { throw_nogpu(); return 0.0; } Scalar cv::gpu::sum(const GpuMat&) { throw_nogpu(); return Scalar(); } Scalar cv::gpu::sum(const GpuMat&, GpuMat&) { throw_nogpu(); return Scalar(); } @@ -88,18 +89,24 @@ void cv::gpu::meanStdDev(const GpuMat& src, Scalar& mean, Scalar& stddev) double cv::gpu::norm(const GpuMat& src, int normType) { + GpuMat buf; + return norm(src, normType, buf); +} + +double cv::gpu::norm(const GpuMat& src, int normType, GpuMat& buf) +{ GpuMat src_single_channel = src.reshape(1); if (normType == NORM_L1) - return absSum(src_single_channel)[0]; + return absSum(src_single_channel, buf)[0]; if (normType == NORM_L2) - return sqrt(sqrSum(src_single_channel)[0]); + return sqrt(sqrSum(src_single_channel, buf)[0]); if (normType == NORM_INF) { double min_val, max_val; - minMax(src_single_channel, &min_val, &max_val); + minMax(src_single_channel, &min_val, &max_val, GpuMat(), buf); return std::max(std::abs(min_val), std::abs(max_val)); } diff --git a/samples/gpu/performance/tests.cpp b/samples/gpu/performance/tests.cpp index 340d6f1..6c7b01b 100644 --- a/samples/gpu/performance/tests.cpp +++ b/samples/gpu/performance/tests.cpp @@ -198,22 +198,24 @@ TEST(integral) TEST(norm) { Mat src; - gpu::GpuMat d_src; + gpu::GpuMat d_src, d_buf; for (int size = 1000; size <= 8000; size *= 2) { - SUBTEST << "size " << size << ", 8U"; + SUBTEST << "size " << size << ", 32F"; - gen(src, size, size, CV_8U, 0, 256); + gen(src, size, size, CV_32F, 0, 1); CPU_ON; - norm(src); + for (int i = 0; i < 10; ++i) + norm(src, NORM_L2); CPU_OFF; d_src = src; GPU_ON; - gpu::norm(d_src); + for (int i = 0; i < 10; ++i) + gpu::norm(d_src, NORM_L2, d_buf); GPU_OFF; } } -- 2.7.4