added buffered version of gpu::convolve
authorAlexey Spizhevoy <no@email>
Mon, 27 Dec 2010 10:18:42 +0000 (10:18 +0000)
committerAlexey Spizhevoy <no@email>
Mon, 27 Dec 2010 10:18:42 +0000 (10:18 +0000)
modules/gpu/include/opencv2/gpu/gpu.hpp
modules/gpu/src/imgproc_gpu.cpp

index 56758ce..0ac16e2 100644 (file)
@@ -656,7 +656,34 @@ namespace cv
         //! computes convolution (or cross-correlation) of two images using discrete Fourier transform\r
         //! supports source images of 32FC1 type only\r
         //! result matrix will have 32FC1 type\r
-        CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, bool ccorr=false);\r
+        CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
+                                 bool ccorr=false);\r
+\r
+        struct CV_EXPORTS ConvolveBuf;\r
+\r
+        //! buffered version\r
+        CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
+                                 bool ccorr, ConvolveBuf& buf);\r
+\r
+        struct CV_EXPORTS ConvolveBuf\r
+        {\r
+            ConvolveBuf() {}\r
+            ConvolveBuf(Size image_size, Size templ_size) \r
+                { create(image_size, templ_size); }\r
+            void create(Size image_size, Size templ_size);\r
+\r
+        private:\r
+            static Size estimateBlockSize(Size result_size, Size templ_size);\r
+            friend void convolve(const GpuMat&, const GpuMat&, GpuMat&, bool, ConvolveBuf&);\r
+\r
+            Size result_size;\r
+            Size block_size;\r
+            Size dft_size;\r
+            int spect_len;\r
+\r
+            GpuMat image_spect, templ_spect, result_spect;\r
+            GpuMat image_block, templ_block, result_data;\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
index bf8e7f6..3b866c9 100644 (file)
@@ -77,7 +77,9 @@ void cv::gpu::cornerMinEigenVal(const GpuMat&, GpuMat&, int, int, int) { throw_n
 void cv::gpu::mulSpectrums(const GpuMat&, const GpuMat&, GpuMat&, int, bool) { throw_nogpu(); }\r
 void cv::gpu::mulAndScaleSpectrums(const GpuMat&, const GpuMat&, GpuMat&, int, float, bool) { throw_nogpu(); }\r
 void cv::gpu::dft(const GpuMat&, GpuMat&, Size, int) { throw_nogpu(); }\r
+void cv::gpu::ConvolveBuf::create(Size, Size) { throw_nogpu(); }\r
 void cv::gpu::convolve(const GpuMat&, const GpuMat&, GpuMat&, bool) { throw_nogpu(); }\r
+void cv::gpu::convolve(const GpuMat&, const GpuMat&, GpuMat&, bool, ConvolveBuf&) { throw_nogpu(); }\r
 \r
 \r
 #else /* !defined (HAVE_CUDA) */\r
@@ -1211,36 +1213,65 @@ void cv::gpu::dft(const GpuMat& src, GpuMat& dst, Size dft_size, int flags)
 }\r
 \r
 //////////////////////////////////////////////////////////////////////////////\r
-// crossCorr\r
+// convolve\r
 \r
-namespace \r
+\r
+void cv::gpu::ConvolveBuf::create(Size image_size, Size templ_size)\r
 {\r
-    // Estimates optimal block size\r
-    void convolveOptBlockSize(int w, int h, int tw, int th, int& bw, int& bh)\r
-    {\r
-        int major, minor;\r
-        getComputeCapability(getDevice(), major, minor);\r
+    result_size = Size(image_size.width - templ_size.width + 1,\r
+                       image_size.height - templ_size.height + 1);\r
+    block_size = estimateBlockSize(result_size, templ_size);\r
+\r
+    dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1);\r
+    dft_size.height = getOptimalDFTSize(block_size.width + templ_size.height - 1);\r
+    createContinuous(dft_size, CV_32F, image_block);\r
+    createContinuous(dft_size, CV_32F, templ_block);\r
+    createContinuous(dft_size, CV_32F, result_data);\r
+\r
+    spect_len = dft_size.height * (dft_size.width / 2 + 1);\r
+    createContinuous(1, spect_len, CV_32FC2, image_spect);\r
+    createContinuous(1, spect_len, CV_32FC2, templ_spect);\r
+    createContinuous(1, spect_len, CV_32FC2, result_spect);\r
+\r
+    block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);\r
+    block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);\r
+}\r
 \r
-        int scale = 40;\r
-        int bh_min = 1024;\r
-        int bw_min = 1024;\r
 \r
-        // Check whether we use Fermi generation or newer GPU\r
-        if (major >= 2) \r
-        {\r
-            bh_min = 2048;\r
-            bw_min = 2048;\r
-        }\r
+Size cv::gpu::ConvolveBuf::estimateBlockSize(Size result_size, Size templ_size)\r
+{\r
+    int major, minor;\r
+    getComputeCapability(getDevice(), major, minor);\r
 \r
-        bw = std::max(tw * scale, bw_min);\r
-        bh = std::max(th * scale, bh_min);\r
-        bw = std::min(bw, w);\r
-        bh = std::min(bh, h);\r
+    int scale = 40;\r
+    Size bsize_min(1024, 1024);\r
+\r
+    // Check whether we use Fermi generation or newer GPU\r
+    if (major >= 2) \r
+    {\r
+        bsize_min.width = 2048;\r
+        bsize_min.height = 2048;\r
     }\r
+\r
+    Size bsize(std::max(templ_size.width * scale, bsize_min.width),\r
+               std::max(templ_size.height * scale, bsize_min.height));\r
+\r
+    bsize.width = std::min(bsize.width, result_size.width);\r
+    bsize.height = std::min(bsize.height, result_size.height);\r
+    return bsize;\r
+}\r
+\r
+\r
+void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
+                       bool ccorr)\r
+{\r
+    ConvolveBuf buf;\r
+    convolve(image, templ, result, ccorr, buf);\r
 }\r
 \r
 \r
-void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, bool ccorr)\r
+void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
+                       bool ccorr, ConvolveBuf& buf)\r
 {\r
     StaticAssert<sizeof(float) == sizeof(cufftReal)>::check();\r
     StaticAssert<sizeof(float) * 2 == sizeof(cufftComplex)>::check();\r
@@ -1248,32 +1279,25 @@ void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result,
     CV_Assert(image.type() == CV_32F);\r
     CV_Assert(templ.type() == CV_32F);\r
 \r
-    result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);\r
+    buf.create(image.size(), templ.size());\r
+    result.create(buf.result_size, CV_32F);\r
 \r
-    Size block_size;\r
-    convolveOptBlockSize(result.cols, result.rows, templ.cols, templ.rows, \r
-                          block_size.width, block_size.height);\r
+    Size& block_size = buf.block_size;\r
+    Size& dft_size = buf.dft_size;\r
+    int& spect_len = buf.spect_len;\r
 \r
-    Size dft_size;\r
-    dft_size.width = getOptimalDFTSize(block_size.width + templ.cols - 1);\r
-    dft_size.height = getOptimalDFTSize(block_size.width + templ.rows - 1);\r
+    GpuMat& image_block = buf.image_block;\r
+    GpuMat& templ_block = buf.templ_block;\r
+    GpuMat& result_data = buf.result_data;\r
 \r
-    block_size.width = std::min(dft_size.width - templ.cols + 1, result.cols);\r
-    block_size.height = std::min(dft_size.height - templ.rows + 1, result.rows);\r
-\r
-    int spect_len = dft_size.height * (dft_size.width / 2 + 1);\r
-    GpuMat image_spect = createContinuous(1, spect_len, CV_32FC2);\r
-    GpuMat templ_spect = createContinuous(1, spect_len, CV_32FC2);\r
-    GpuMat result_spect = createContinuous(1, spect_len, CV_32FC2);\r
+    GpuMat& image_spect = buf.image_spect;\r
+    GpuMat& templ_spect = buf.templ_spect;\r
+    GpuMat& result_spect = buf.result_spect;\r
 \r
     cufftHandle planR2C, planC2R;\r
     cufftSafeCall(cufftPlan2d(&planC2R, dft_size.height, dft_size.width, CUFFT_C2R));\r
     cufftSafeCall(cufftPlan2d(&planR2C, dft_size.height, dft_size.width, CUFFT_R2C));\r
 \r
-    GpuMat image_block = createContinuous(dft_size, CV_32F);\r
-    GpuMat templ_block = createContinuous(dft_size, CV_32F);\r
-    GpuMat result_data = createContinuous(dft_size, CV_32F);\r
-\r
     GpuMat templ_roi(templ.size(), CV_32F, templ.data, templ.step);\r
     copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0, \r
                    templ_block.cols - templ_roi.cols, 0);\r
@@ -1288,9 +1312,10 @@ void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result,
         {\r
             Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,\r
                                 std::min(y + dft_size.height, image.rows) - y);\r
-            GpuMat image_roi(image_roi_size, CV_32F, (void*)(image.ptr<float>(y) + x), image.step);\r
-            copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows, 0, \r
-                           image_block.cols - image_roi.cols, 0);\r
+            GpuMat image_roi(image_roi_size, CV_32F, (void*)(image.ptr<float>(y) + x), \r
+                             image.step);\r
+            copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,\r
+                           0, image_block.cols - image_roi.cols, 0);\r
 \r
             cufftSafeCall(cufftExecR2C(planR2C, image_block.ptr<cufftReal>(), \r
                                        image_spect.ptr<cufftComplex>()));\r
@@ -1301,8 +1326,10 @@ void cv::gpu::convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result,
 \r
             Size result_roi_size(std::min(x + block_size.width, result.cols) - x,\r
                                  std::min(y + block_size.height, result.rows) - y);\r
-            GpuMat result_roi(result_roi_size, result.type(), (void*)(result.ptr<float>(y) + x), result.step);\r
-            GpuMat result_block(result_roi_size, result_data.type(), result_data.ptr(), result_data.step);\r
+            GpuMat result_roi(result_roi_size, result.type(), \r
+                              (void*)(result.ptr<float>(y) + x), result.step);\r
+            GpuMat result_block(result_roi_size, result_data.type(), \r
+                                result_data.ptr(), result_data.step);\r
             result_block.copyTo(result_roi);\r
         }\r
     }\r