added first version of gpu::matchTemplate, currently it works only with 8UC1 images...
authorAlexey Spizhevoy <no@email>
Mon, 6 Dec 2010 14:19:41 +0000 (14:19 +0000)
committerAlexey Spizhevoy <no@email>
Mon, 6 Dec 2010 14:19:41 +0000 (14:19 +0000)
modules/gpu/include/opencv2/gpu/gpu.hpp
modules/gpu/src/border_interpolate.cpp
modules/gpu/src/cuda/border_interpolate.hpp
modules/gpu/src/cuda/match_template.cu [new file with mode: 0644]
modules/gpu/src/internal_common.hpp [deleted file]
modules/gpu/src/internal_shared.hpp [new file with mode: 0644]
modules/gpu/src/match_template.cpp [new file with mode: 0644]
tests/gpu/src/gputest_main.cpp
tests/gpu/src/match_template.cpp [new file with mode: 0644]

index 53a1a74b2c077446d37c995b8edc0e8f9202e43b..6655af1592b839841e4bf50e6cb1b6fc6a9a34e8 100644 (file)
@@ -656,6 +656,10 @@ namespace cv
         CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, int borderType=BORDER_REFLECT101);\r
 \r
 \r
+        //! computes the proximity map for the raster template and the image where the template is searched for\r
+        CV_EXPORTS void matchTemplate(const GpuMat& image, const GpuMat& templ, GpuMat& result, int method);\r
+\r
+\r
         //////////////////////////////// Filter Engine ////////////////////////////////\r
 \r
         /*!\r
index dbd839451110ed69fe8fc5d8b724c49ebb582b2a..a162a2e0dac45241d849bc8103e1de3bc52d14bc 100644 (file)
@@ -40,7 +40,7 @@
 //\r
 //M*/\r
 \r
-#include "internal_common.hpp"\r
+#include "internal_shared.hpp"\r
 #include "border_interpolate.hpp"\r
 #include "opencv2/gpu/gpu.hpp"\r
 \r
index 3dad03eb44cd630a542ccdc6f0130d04f366b52d..d15e11d575ff543142bfced133b80980a2d18462 100644 (file)
@@ -43,7 +43,7 @@
 #ifndef __OPENCV_GPU_BORDER_INTERPOLATE_HPP__\r
 #define __OPENCV_GPU_BORDER_INTERPOLATE_HPP__\r
 \r
-#include "../internal_common.hpp"\r
+#include "../internal_shared.hpp"\r
 \r
 namespace cv { namespace gpu {\r
 \r
diff --git a/modules/gpu/src/cuda/match_template.cu b/modules/gpu/src/cuda/match_template.cu
new file mode 100644 (file)
index 0000000..ea797f0
--- /dev/null
@@ -0,0 +1,97 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "cuda_shared.hpp"\r
+\r
+using namespace cv::gpu;\r
+\r
+namespace cv { namespace gpu { namespace imgproc {\r
+\r
+texture<unsigned char, 2> imageTex_8U;\r
+texture<unsigned char, 2> templTex_8U;\r
+\r
+\r
+__global__ void matchTemplateKernel_8U_SqDiff(int w, int h, DevMem2Df result)\r
+{\r
+    int x = blockDim.x * blockIdx.x + threadIdx.x;\r
+    int y = blockDim.y * blockIdx.y + threadIdx.y;\r
+\r
+    if (x < result.cols && y < result.rows)\r
+    {\r
+        float sum = 0.f;\r
+        float delta;\r
+\r
+        for (int i = 0; i < h; ++i)\r
+        {\r
+            for (int j = 0; j < w; ++j)\r
+            {\r
+                delta = (float)tex2D(imageTex_8U, x + j, y + i) - \r
+                        (float)tex2D(templTex_8U, j, i);\r
+                sum += delta * delta;\r
+            }\r
+        }\r
+\r
+        result.ptr(y)[x] = sum;\r
+    }\r
+}\r
+\r
+\r
+void matchTemplateCaller_8U_SqDiff(const DevMem2D image, const DevMem2D templ, DevMem2Df result)\r
+{\r
+    dim3 threads(32, 8);\r
+    dim3 grid(divUp(image.cols - templ.cols + 1, threads.x), \r
+              divUp(image.rows - templ.rows + 1, threads.y));\r
+\r
+    cudaChannelFormatDesc desc = cudaCreateChannelDesc<unsigned char>();\r
+    cudaBindTexture2D(0, imageTex_8U, image.data, desc, image.cols, image.rows, image.step);\r
+    cudaBindTexture2D(0, templTex_8U, templ.data, desc, templ.cols, templ.rows, templ.step);\r
+    imageTex_8U.filterMode = cudaFilterModePoint;\r
+    templTex_8U.filterMode = cudaFilterModePoint;\r
+\r
+    matchTemplateKernel_8U_SqDiff<<<grid, threads>>>(templ.cols, templ.rows, result);\r
+    cudaSafeCall(cudaThreadSynchronize());\r
+    cudaSafeCall(cudaUnbindTexture(imageTex_8U));\r
+    cudaSafeCall(cudaUnbindTexture(templTex_8U));\r
+}\r
+\r
+\r
+}}}\r
diff --git a/modules/gpu/src/internal_common.hpp b/modules/gpu/src/internal_common.hpp
deleted file mode 100644 (file)
index c9af363..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*M///////////////////////////////////////////////////////////////////////////////////////\r
-//\r
-//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
-//\r
-//  By downloading, copying, installing or using the software you agree to this license.\r
-//  If you do not agree to this license, do not download, install,\r
-//  copy or use the software.\r
-//\r
-//\r
-//                           License Agreement\r
-//                For Open Source Computer Vision Library\r
-//\r
-// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
-// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
-// Third party copyrights are property of their respective owners.\r
-//\r
-// Redistribution and use in source and binary forms, with or without modification,\r
-// are permitted provided that the following conditions are met:\r
-//\r
-//   * Redistribution's of source code must retain the above copyright notice,\r
-//     this list of conditions and the following disclaimer.\r
-//\r
-//   * Redistribution's in binary form must reproduce the above copyright notice,\r
-//     this list of conditions and the following disclaimer in the documentation\r
-//     and/or other materials provided with the distribution.\r
-//\r
-//   * The name of the copyright holders may not be used to endorse or promote products\r
-//     derived from this software without specific prior written permission.\r
-//\r
-// This software is provided by the copyright holders and contributors "as is" and\r
-// any express or implied warranties, including, but not limited to, the implied\r
-// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
-// In no event shall the Intel Corporation or contributors be liable for any direct,\r
-// indirect, incidental, special, exemplary, or consequential damages\r
-// (including, but not limited to, procurement of substitute goods or services;\r
-// loss of use, data, or profits; or business interruption) however caused\r
-// and on any theory of liability, whether in contract, strict liability,\r
-// or tort (including negligence or otherwise) arising in any way out of\r
-// the use of this software, even if advised of the possibility of such damage.\r
-//\r
-//M*/\r
-\r
-#ifndef __OPENCV_GPU_INTERNAL_COMMON_HPP__\r
-#define __OPENCV_GPU_INTERNAL_COMMON_HPP__\r
-\r
-namespace cv { namespace gpu {\r
-\r
-    // Internal GPU anlagues of CPU border extrapolation types\r
-    enum \r
-    {\r
-        BORDER_REFLECT101 = 0,\r
-        BORDER_REPLICATE\r
-    };\r
-\r
-}}\r
-\r
-#endif
\ No newline at end of file
diff --git a/modules/gpu/src/internal_shared.hpp b/modules/gpu/src/internal_shared.hpp
new file mode 100644 (file)
index 0000000..c3a5882
--- /dev/null
@@ -0,0 +1,57 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#ifndef __OPENCV_GPU_INTERNAL_SHARED_HPP__\r
+#define __OPENCV_GPU_INTERNAL_SHARED_HPP__\r
+\r
+namespace cv { namespace gpu {\r
+\r
+    // Internal GPU anlagues of CPU border extrapolation types\r
+    enum \r
+    {\r
+        BORDER_REFLECT101 = 0,\r
+        BORDER_REPLICATE\r
+    };\r
+\r
+}}\r
+\r
+#endif
\ No newline at end of file
diff --git a/modules/gpu/src/match_template.cpp b/modules/gpu/src/match_template.cpp
new file mode 100644 (file)
index 0000000..db182bd
--- /dev/null
@@ -0,0 +1,69 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "precomp.hpp"\r
+\r
+#if !defined (HAVE_CUDA)\r
+\r
+void cv::gpu::matchTemplate(const GpuMat&, const GpuMat&, GpuMat&, int) { throw_nogpu(); }\r
+\r
+#else\r
+\r
+namespace cv { namespace gpu { namespace imgproc {\r
+    \r
+    void matchTemplateCaller_8U_SqDiff(const DevMem2D, const DevMem2D, DevMem2Df);\r
+\r
+}}}\r
+\r
+void cv::gpu::matchTemplate(const GpuMat& image, const GpuMat& templ, GpuMat& result, int method)\r
+{\r
+    CV_Assert(image.type() == CV_8U);\r
+    CV_Assert(method == CV_TM_SQDIFF);\r
+\r
+    CV_Assert(image.type() == templ.type());\r
+    CV_Assert(image.cols >= templ.cols && image.rows >= templ.rows);\r
+\r
+    result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);\r
+    imgproc::matchTemplateCaller_8U_SqDiff(image, templ, result);\r
+}\r
+\r
+#endif\r
index a634ef0edd03acad2b97a0dc3c6577264a5eff45..917ce9ae1de9e7709a80b77fef9973f1310deed8 100644 (file)
@@ -56,7 +56,10 @@ const char* blacklist[] =
 int main( int argc, char** argv )
 
 {
-    return test_system.run( argc, argv, blacklist );
+    argc = 3;
+    const char* manargs[] = { "gputest", "-tn", "GPU-MatchTemplateTest"};
+    
+    return test_system.run( argc, (char**)manargs, blacklist );
 }
 
 /* End of file. */
diff --git a/tests/gpu/src/match_template.cpp b/tests/gpu/src/match_template.cpp
new file mode 100644 (file)
index 0000000..c80cb29
--- /dev/null
@@ -0,0 +1,145 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other GpuMaterials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or bpied warranties, including, but not limited to, the bpied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "gputest.hpp"\r
+\r
+using namespace cv;\r
+using namespace std;\r
+\r
+struct CV_GpuMatchTemplateTest: CvTest \r
+{\r
+    CV_GpuMatchTemplateTest(): CvTest("GPU-MatchTemplateTest", "matchTemplate") {}\r
+\r
+    void run(int)\r
+    {\r
+        try\r
+        {\r
+            Mat image, templ;\r
+            Mat dst_gold;\r
+            gpu::GpuMat dst;\r
+            int n, m, h, w;\r
+\r
+            for (int i = 0; i < 4; ++i)\r
+            {\r
+                n = 1 + rand() % 100;\r
+                m = 1 + rand() % 100;\r
+                do h = 1 + rand() % 20; while (h > n);\r
+                do w = 1 + rand() % 20; while (w > m);\r
+                gen(image, n, m, CV_8U);\r
+                gen(templ, h, w, CV_8U);\r
+\r
+                match_template_naive(image, templ, dst_gold);\r
+                gpu::matchTemplate(gpu::GpuMat(image), gpu::GpuMat(templ), dst, CV_TM_SQDIFF);\r
+                if (!check8U(dst_gold, Mat(dst))) return;\r
+            }\r
+        }\r
+        catch (const Exception& e)\r
+        {\r
+            if (!check_and_treat_gpu_exception(e, ts)) throw;\r
+            return;\r
+        }\r
+    }\r
+\r
+\r
+    void gen(Mat& a, int rows, int cols, int type)\r
+    {\r
+        RNG rng;\r
+        a.create(rows, cols, type);\r
+        if (type == CV_8U)\r
+            rng.fill(a, RNG::UNIFORM, Scalar(0), Scalar(10));\r
+    }\r
+\r
+    // Naive version for unsigned char\r
+    // Time complexity is O(a.size().area() * b.size().area()).\r
+    void match_template_naive(const Mat& a, const Mat& b, Mat& c)\r
+    {\r
+        c.create(a.rows - b.rows + 1, a.cols - b.cols + 1, CV_32F);         \r
+        for (int i = 0; i < c.rows; ++i)\r
+        {\r
+            for (int j = 0; j < c.cols; ++j)\r
+            {\r
+                float delta;\r
+                float sum = 0.f;\r
+                for (int y = 0; y < b.rows; ++y)\r
+                {\r
+                    const unsigned char* arow = a.ptr(i + y);\r
+                    const unsigned char* brow = b.ptr(y);\r
+                    for (int x = 0; x < b.cols; ++x)\r
+                    {\r
+                        delta = (float)(arow[j + x] - brow[x]);\r
+                        sum += delta * delta;\r
+                    }\r
+                }\r
+                c.at<float>(i, j) = sum;\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    bool check8U(const Mat& a, const Mat& b)\r
+    {\r
+        if (a.size() != b.size())\r
+        {\r
+            ts->printf(CvTS::CONSOLE, "bad size");\r
+            ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);\r
+            return false;\r
+        }\r
+\r
+        for (int i = 0; i < a.rows; ++i)\r
+        {\r
+            for (int j = 0; j < a.cols; ++j)\r
+            {\r
+                float v1 = a.at<float>(i, j);\r
+                float v2 = b.at<float>(i, j);\r
+                if (fabs(v1 - v2) > 1e-3f)\r
+                {\r
+                    ts->printf(CvTS::CONSOLE, "(gold)%f != %f, pos: (%d, %d) size: (%d, %d)\n", \r
+                               v1, v2, j, i, a.cols, a.rows);\r
+                    ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);\r
+                    return false;\r
+                }\r
+            }\r
+        }\r
+\r
+        return true;\r
+    }\r
+} match_template_test;\r