added gpu::columnSum, fixed compile error (if there is no cuda), refactored
authorAlexey Spizhevoy <no@email>
Wed, 8 Dec 2010 15:06:10 +0000 (15:06 +0000)
committerAlexey Spizhevoy <no@email>
Wed, 8 Dec 2010 15:06:10 +0000 (15:06 +0000)
modules/gpu/include/opencv2/gpu/gpu.hpp
modules/gpu/src/cuda/imgproc.cu
modules/gpu/src/cuda/match_template.cu
modules/gpu/src/imgproc_gpu.cpp
modules/gpu/src/match_template.cpp
tests/gpu/src/match_template.cpp

index 0e7cc57..fa8e3ed 100644 (file)
@@ -638,6 +638,9 @@ namespace cv
         //! supports only CV_8UC1 source type\r
         CV_EXPORTS void integral(GpuMat& src, GpuMat& sum, GpuMat& sqsum);\r
 \r
+        //! computes vertical sum, supports only CV_32FC1 images\r
+        CV_EXPORTS void columnSum(const GpuMat& src, GpuMat& sum);\r
+\r
         //! computes the standard deviation of integral images\r
         //! supports only CV_32SC1 source type and CV_32FC1 sqr type\r
         //! output will have CV_32FC1 type\r
index 8408182..94cfb40 100644 (file)
@@ -42,7 +42,6 @@
 \r
 #include "internal_shared.hpp"\r
 #include "opencv2/gpu/device/border_interpolate.hpp"\r
-#include "internal_shared.hpp"\r
 \r
 using namespace cv::gpu;\r
 using namespace cv::gpu::device;\r
@@ -717,5 +716,36 @@ namespace cv { namespace gpu { namespace imgproc
         cudaSafeCall(cudaUnbindTexture(minEigenValDxTex));\r
         cudaSafeCall(cudaUnbindTexture(minEigenValDyTex));\r
     }\r
+\r
+////////////////////////////// Column Sum //////////////////////////////////////\r
+\r
+    __global__ void columnSumKernel_32F(int cols, int rows, const PtrStep src, const PtrStep dst)\r
+    {\r
+        int x = blockIdx.x * blockDim.x + threadIdx.x;\r
+\r
+        const float* src_data = (const float*)src.data + x;\r
+        float* dst_data = (float*)dst.data + x;\r
+\r
+        if (x < cols)\r
+        {\r
+            float sum = 0.f;\r
+            for (int y = 0; y < rows; ++y)\r
+            {\r
+                sum += src_data[y];\r
+                dst_data[y] = sum;\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    void columnSum_32F(const DevMem2D src, const DevMem2D dst)\r
+    {\r
+        dim3 threads(256);\r
+        dim3 grid(divUp(src.cols, threads.x));\r
+\r
+        columnSumKernel_32F<<<grid, threads>>>(src.cols, src.rows, src, dst);\r
+        cudaSafeCall(cudaThreadSynchronize());\r
+    }\r
+\r
 }}}\r
 \r
index 700baa6..7b28c64 100644 (file)
@@ -55,7 +55,7 @@ texture<unsigned char, 2> imageTex_8U;
 texture<unsigned char, 2> templTex_8U;\r
 \r
 \r
-__global__ void matchTemplateKernel_8U_SQDIFF(int w, int h, DevMem2Df result)\r
+__global__ void matchTemplateNaiveKernel_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
@@ -80,7 +80,7 @@ __global__ void matchTemplateKernel_8U_SQDIFF(int w, int h, DevMem2Df result)
 }\r
 \r
 \r
-void matchTemplate_8U_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result)\r
+void matchTemplateNaive_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
@@ -92,7 +92,7 @@ void matchTemplate_8U_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2
     imageTex_8U.filterMode = cudaFilterModePoint;\r
     templTex_8U.filterMode = cudaFilterModePoint;\r
 \r
-    matchTemplateKernel_8U_SQDIFF<<<grid, threads>>>(templ.cols, templ.rows, result);\r
+    matchTemplateNaiveKernel_8U_SQDIFF<<<grid, threads>>>(templ.cols, templ.rows, result);\r
     cudaSafeCall(cudaThreadSynchronize());\r
     cudaSafeCall(cudaUnbindTexture(imageTex_8U));\r
     cudaSafeCall(cudaUnbindTexture(templTex_8U));\r
@@ -103,7 +103,7 @@ texture<float, 2> imageTex_32F;
 texture<float, 2> templTex_32F;\r
 \r
 \r
-__global__ void matchTemplateKernel_32F_SQDIFF(int w, int h, DevMem2Df result)\r
+__global__ void matchTemplateNaiveKernel_32F_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
@@ -128,7 +128,7 @@ __global__ void matchTemplateKernel_32F_SQDIFF(int w, int h, DevMem2Df result)
 }\r
 \r
 \r
-void matchTemplate_32F_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result)\r
+void matchTemplateNaive_32F_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
@@ -140,7 +140,7 @@ void matchTemplate_32F_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem
     imageTex_8U.filterMode = cudaFilterModePoint;\r
     templTex_8U.filterMode = cudaFilterModePoint;\r
 \r
-    matchTemplateKernel_32F_SQDIFF<<<grid, threads>>>(templ.cols, templ.rows, result);\r
+    matchTemplateNaiveKernel_32F_SQDIFF<<<grid, threads>>>(templ.cols, templ.rows, result);\r
     cudaSafeCall(cudaThreadSynchronize());\r
     cudaSafeCall(cudaUnbindTexture(imageTex_32F));\r
     cudaSafeCall(cudaUnbindTexture(templTex_32F));\r
@@ -165,6 +165,7 @@ void multiplyAndNormalizeSpects(int n, float scale, const cufftComplex* a, const
     dim3 threads(256);\r
     dim3 grid(divUp(n, threads.x));\r
     multiplyAndNormalizeSpectsKernel<<<grid, threads>>>(n, scale, a, b, c);\r
+    cudaSafeCall(cudaThreadSynchronize());\r
 }\r
 \r
 \r
index 376d1e0..0ff08c1 100644 (file)
@@ -61,6 +61,7 @@ void cv::gpu::warpAffine(const GpuMat&, GpuMat&, const Mat&, Size, int) { throw_
 void cv::gpu::warpPerspective(const GpuMat&, GpuMat&, const Mat&, Size, int) { throw_nogpu(); }\r
 void cv::gpu::rotate(const GpuMat&, GpuMat&, Size, double, double, double, int) { throw_nogpu(); }\r
 void cv::gpu::integral(GpuMat&, GpuMat&, GpuMat&) { throw_nogpu(); }\r
+void cv::gpu::columnSum(const GpuMat&, GpuMat&) { throw_nogpu(); }\r
 void cv::gpu::rectStdDev(const GpuMat&, const GpuMat&, GpuMat&, const Rect&) { throw_nogpu(); }\r
 void cv::gpu::Canny(const GpuMat&, GpuMat&, double, double, int) { throw_nogpu(); }\r
 void cv::gpu::evenLevels(GpuMat&, int, int, int) { throw_nogpu(); }\r
@@ -555,6 +556,22 @@ void cv::gpu::integral(GpuMat& src, GpuMat& sum, GpuMat& sqsum)
         sum.step, sqsum.ptr<Npp32f>(), sqsum.step, sz, 0, 0.0f, h) );\r
 }\r
 \r
+//////////////////////////////////////////////////////////////////////////////\r
+// columnSum\r
+\r
+namespace cv { namespace gpu { namespace imgproc\r
+{\r
+    void columnSum_32F(const DevMem2D src, const DevMem2D dst);\r
+}}}\r
+\r
+void cv::gpu::columnSum(const GpuMat& src, GpuMat& dst)\r
+{\r
+    CV_Assert(src.type() == CV_32F);\r
+\r
+    dst.create(src.size(), CV_32F);\r
+    imgproc::columnSum_32F(src, dst);\r
+}\r
+\r
 void cv::gpu::rectStdDev(const GpuMat& src, const GpuMat& sqr, GpuMat& dst, const Rect& rect)\r
 {\r
     CV_Assert(src.type() == CV_32SC1 && sqr.type() == CV_32FC1);\r
index 7ce8838..b5090f6 100644 (file)
@@ -41,7 +41,6 @@
 //M*/\r
 \r
 #include "precomp.hpp"\r
-#include <cufft.h>\r
 #include <iostream>\r
 #include <utility>\r
 \r
@@ -56,12 +55,14 @@ void cv::gpu::matchTemplate(const GpuMat&, const GpuMat&, GpuMat&, int) { throw_
 \r
 #else\r
 \r
+#include <cufft.h>\r
+\r
 namespace cv { namespace gpu { namespace imgproc \r
 {  \r
     void multiplyAndNormalizeSpects(int n, float scale, const cufftComplex* a, \r
                                     const cufftComplex* b, cufftComplex* c);\r
-    void matchTemplate_8U_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result);\r
-    void matchTemplate_32F_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result);\r
+    void matchTemplateNaive_8U_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result);\r
+    void matchTemplateNaive_32F_SQDIFF(const DevMem2D image, const DevMem2D templ, DevMem2Df result);\r
 }}}\r
 \r
 \r
@@ -90,7 +91,7 @@ namespace
     void matchTemplate<CV_8U, CV_TM_SQDIFF>(const GpuMat& image, const GpuMat& templ, GpuMat& result)\r
     {\r
         result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);\r
-        imgproc::matchTemplate_8U_SQDIFF(image, templ, result);\r
+        imgproc::matchTemplateNaive_8U_SQDIFF(image, templ, result);\r
     }\r
 \r
     \r
@@ -98,7 +99,7 @@ namespace
     void matchTemplate<CV_32F, CV_TM_SQDIFF>(const GpuMat& image, const GpuMat& templ, GpuMat& result)\r
     {\r
         result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);\r
-        imgproc::matchTemplate_32F_SQDIFF(image, templ, result);\r
+        imgproc::matchTemplateNaive_32F_SQDIFF(image, templ, result);\r
     }\r
 \r
 \r
index c876255..1805678 100644 (file)
@@ -97,15 +97,15 @@ struct CV_GpuMatchTemplateTest: CvTest
                 F(cout << "gpu_block: " << clock() - t << endl;)\r
                 if (!check(dst_gold, Mat(dst), 0.25f * h * w * 1e-5f)) return;\r
 \r
-                gen(image, n, m, CV_32F);\r
-                gen(templ, h, w, CV_32F);\r
-                F(t = clock();)\r
-                matchTemplate(image, templ, dst_gold, CV_TM_CCORR);\r
-                F(cout << "cpu:" << clock() - t << endl;)\r
-                F(t = clock();)\r
-                gpu::matchTemplate(gpu::GpuMat(image), gpu::GpuMat(templ), dst, CV_TM_CCORR);\r
-                F(cout << "gpu_block: " << clock() - t << endl;)\r
-                if (!check(dst_gold, Mat(dst), 0.25f * h * w * 1e-5f)) return;\r
+                //gen(image, n, m, CV_32F);\r
+                //gen(templ, h, w, CV_32F);\r
+                //F(t = clock();)\r
+                //matchTemplate(image, templ, dst_gold, CV_TM_CCORR);\r
+                //F(cout << "cpu:" << clock() - t << endl;)\r
+                //F(t = clock();)\r
+                //gpu::matchTemplate(gpu::GpuMat(image), gpu::GpuMat(templ), dst, CV_TM_CCORR);\r
+                //F(cout << "gpu_block: " << clock() - t << endl;)\r
+                //if (!check(dst_gold, Mat(dst), 0.25f * h * w * 1e-5f)) return;\r
             }\r
         }\r
         catch (const Exception& e)\r