From f3b45af8767f269f62081b20236b2bacf57393f7 Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Wed, 18 May 2011 06:51:05 +0000 Subject: [PATCH] GpuMat::setTo optimization (call cudaMemcpy2D if possible) --- modules/gpu/src/matrix_operations.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/gpu/src/matrix_operations.cpp b/modules/gpu/src/matrix_operations.cpp index e1c34dd..e4d5059 100644 --- a/modules/gpu/src/matrix_operations.cpp +++ b/modules/gpu/src/matrix_operations.cpp @@ -442,6 +442,22 @@ GpuMat& GpuMat::setTo(const Scalar& s, const GpuMat& mask) if (mask.empty()) { + if (s[0] == 0.0 && s[1] == 0.0 && s[2] == 0.0 && s[3] == 0.0) + { + cudaSafeCall( cudaMemset2D(data, step, 0, cols * elemSize(), rows) ); + return *this; + } + if (depth() == CV_8U) + { + int cn = channels(); + + if (cn == 1 || (cn == 2 && s[0] == s[1]) || (cn == 3 && s[0] == s[1] && s[0] == s[2]) || (cn == 4 && s[0] == s[1] && s[0] == s[2] && s[0] == s[3])) + { + int val = saturate_cast(s[0]); + cudaSafeCall( cudaMemset2D(data, step, val, cols, rows) ); + return *this; + } + } typedef void (*set_caller_t)(GpuMat& src, const Scalar& s); static const set_caller_t set_callers[8][4] = { -- 2.7.4