added DisparityBilateralFilter to gpu module
authorVladislav Vinogradov <no@email>
Thu, 19 Aug 2010 08:44:06 +0000 (08:44 +0000)
committerVladislav Vinogradov <no@email>
Thu, 19 Aug 2010 08:44:06 +0000 (08:44 +0000)
modules/gpu/include/opencv2/gpu/gpu.hpp
modules/gpu/src/bilateral_filter.cpp [new file with mode: 0644]
modules/gpu/src/cuda/bilateral_filter.cu [new file with mode: 0644]

index bf9abba..e5c475f 100644 (file)
@@ -391,6 +391,7 @@ namespace cv
         ////////////////////////// StereoBeliefPropagation ///////////////////////////\r
         // "Efficient Belief Propagation for Early Vision" \r
         // P.Felzenszwalb\r
+\r
         class CV_EXPORTS StereoBeliefPropagation\r
         {\r
         public:\r
@@ -504,6 +505,45 @@ namespace cv
 \r
             GpuMat out;\r
         };\r
+\r
+        /////////////////////////// DisparityBilateralFilter ///////////////////////////\r
+        // Disparity map refinement using joint bilateral filtering given a single color image.\r
+        // Qingxiong Yang, Liang Wang\86, Narendra Ahuja         \r
+        // http://vision.ai.uiuc.edu/~qyang6/\r
+\r
+        class CV_EXPORTS DisparityBilateralFilter\r
+        {\r
+        public:\r
+            enum { DEFAULT_NDISP  = 64 };\r
+            enum { DEFAULT_RADIUS = 3 };\r
+            enum { DEFAULT_ITERS  = 1 };\r
+\r
+            //! the default constructor\r
+            explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);\r
+\r
+            //! the full constructor taking the number of disparities, filter radius,\r
+            //! number of iterations, truncation of data continuity, truncation of disparity continuity\r
+            //! and filter range sigma\r
+            DisparityBilateralFilter(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, float sigma_range);\r
+\r
+            //! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.\r
+            //! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.\r
+            void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst);\r
+\r
+            //! Acync version\r
+            void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst, Stream& stream);\r
+\r
+            int ndisp;\r
+            int radius;\r
+            int iters;\r
+\r
+            float edge_threshold;\r
+            float max_disc_threshold;\r
+            float sigma_range;\r
+        private:\r
+            std::vector<float> table_color;\r
+            GpuMat table_space;\r
+        };\r
     }\r
 \r
     //! Speckle filtering - filters small connected components on diparity image.\r
diff --git a/modules/gpu/src/bilateral_filter.cpp b/modules/gpu/src/bilateral_filter.cpp
new file mode 100644 (file)
index 0000000..ffe6a1e
--- /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 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
+using namespace cv;\r
+using namespace cv::gpu;\r
+using namespace std;\r
+\r
+#if !defined (HAVE_CUDA)\r
+\r
+cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int, int, int) { throw_nogpu(); }\r
+cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int, int, int, float, float, float) { throw_nogpu(); }\r
+\r
+void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat&, const GpuMat&, GpuMat&) { throw_nogpu(); }\r
+void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_nogpu(); }\r
+\r
+#else /* !defined (HAVE_CUDA) */\r
+\r
+namespace cv { namespace gpu { namespace bf \r
+{\r
+    void calc_space_weighted_filter_gpu(const DevMem2Df& table_space, int half, float dist_space, cudaStream_t stream);\r
+    void load_constants(float* table_color, const DevMem2Df& table_space, int ndisp, int radius, short edge_disc, short max_disc);\r
+\r
+    void bilateral_filter_gpu(const DevMem2D& disp, const DevMem2D& img, int channels, int iters, cudaStream_t stream);\r
+    void bilateral_filter_gpu(const DevMem2D_<short>& disp, const DevMem2D& img, int channels, int iters, cudaStream_t stream);\r
+}}}\r
+\r
+namespace\r
+{\r
+    const float DEFAULT_EDGE_THRESHOLD = 0.1f;\r
+    const float DEFAULT_MAX_DISC_THRESHOLD = 0.2f;\r
+    const float DEFAULT_SIGMA_RANGE = 10.0f;\r
+\r
+    inline void calc_color_weighted_table(vector<float>& table_color, float sigma_range, int len)\r
+    {\r
+        float* color_table_x;\r
+        \r
+        table_color.resize(len);\r
+        color_table_x = &table_color[0];\r
+\r
+        for(int y = 0; y < len; y++) \r
+            table_color[y] = static_cast<float>(std::exp(-double(y * y) / (2 * sigma_range * sigma_range)));\r
+    }\r
+\r
+    inline void calc_space_weighted_filter(GpuMat& table_space, int win_size, float dist_space, cudaStream_t stream)\r
+    {\r
+        int half = (win_size >> 1);\r
+        table_space.create(half + 1, half + 1, CV_32F);\r
+\r
+        bf::calc_space_weighted_filter_gpu(table_space, half, dist_space, stream);\r
+    }\r
+\r
+    template <typename T>\r
+    void bilateral_filter_operator(DisparityBilateralFilter& rthis, vector<float>& table_color, GpuMat& table_space, \r
+                                   const GpuMat& disp, const GpuMat& img, GpuMat& dst, cudaStream_t stream)\r
+    {\r
+        calc_color_weighted_table(table_color, rthis.sigma_range, 255);\r
+        calc_space_weighted_filter(table_space, rthis.radius * 2 + 1, rthis.radius + 1.0f, stream);\r
+\r
+        short edge_disc = max<short>(short(1), short(rthis.ndisp * rthis.edge_threshold + 0.5));\r
+        short max_disc = short(rthis.ndisp * rthis.max_disc_threshold + 0.5);\r
+\r
+        bf::load_constants(&table_color[0], table_space, rthis.ndisp, rthis.radius, edge_disc, max_disc);\r
+\r
+        if (&dst != &disp)\r
+            disp.copyTo(dst);\r
+\r
+        bf::bilateral_filter_gpu((DevMem2D_<T>)dst, img, img.channels(), rthis.iters, stream);\r
+    }\r
+\r
+    typedef void (*bilateral_filter_operator_t)(DisparityBilateralFilter& rthis, vector<float>& table_color, GpuMat& table_space, \r
+                                   const GpuMat& disp, const GpuMat& img, GpuMat& dst, cudaStream_t stream);\r
+    \r
+    const bilateral_filter_operator_t operators[] = \r
+        {bilateral_filter_operator<unsigned char>, 0, 0, bilateral_filter_operator<short>, 0, 0, 0, 0};\r
+}\r
+\r
+cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp_, int radius_, int iters_)\r
+    : ndisp(ndisp_), radius(radius_), iters(iters_), edge_threshold(DEFAULT_EDGE_THRESHOLD), max_disc_threshold(DEFAULT_MAX_DISC_THRESHOLD),\r
+      sigma_range(DEFAULT_SIGMA_RANGE)\r
+{\r
+}\r
+\r
+cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp_, int radius_, int iters_, float edge_threshold_, \r
+                                                     float max_disc_threshold_, float sigma_range_)\r
+    : ndisp(ndisp_), radius(radius_), iters(iters_), edge_threshold(edge_threshold_), max_disc_threshold(max_disc_threshold_), \r
+      sigma_range(sigma_range_)\r
+{\r
+}\r
+\r
+void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat& disp, const GpuMat& img, GpuMat& dst)\r
+{\r
+    CV_DbgAssert(0 < ndisp && 0 < radius && 0 < iters);\r
+    CV_Assert(disp.rows == img.rows && disp.cols == img.cols && (disp.type() == CV_8U || disp.type() == CV_16S) && (img.type() == CV_8UC1 || img.type() == CV_8UC3));\r
+    operators[disp.type()](*this, table_color, table_space, disp, img, dst, 0);\r
+}\r
+\r
+void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream)\r
+{\r
+    CV_DbgAssert(0 < ndisp && 0 < radius && 0 < iters);\r
+    CV_Assert(disp.rows == img.rows && disp.cols == img.cols && (disp.type() == CV_8U || disp.type() == CV_16S) && (img.type() == CV_8UC1 || img.type() == CV_8UC3));\r
+    operators[disp.type()](*this, table_color, table_space, disp, img, dst, StreamAccessor::getStream(stream));\r
+}\r
+\r
+#endif /* !defined (HAVE_CUDA) */\r
diff --git a/modules/gpu/src/cuda/bilateral_filter.cu b/modules/gpu/src/cuda/bilateral_filter.cu
new file mode 100644 (file)
index 0000000..f53d4c2
--- /dev/null
@@ -0,0 +1,262 @@
+/*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 "opencv2/gpu/devmem2d.hpp"\r
+#include "saturate_cast.hpp"\r
+#include "safe_call.hpp"\r
+\r
+using namespace cv::gpu;\r
+using namespace cv::gpu::impl;\r
+\r
+#ifndef FLT_MAX\r
+#define FLT_MAX 3.402823466e+30F\r
+#endif\r
+\r
+namespace bf_krnls\r
+{\r
+    __global__ void calc_space_weighted_filter(float* table_space, size_t step, int half, float dist_space)\r
+    {\r
+        int x = blockIdx.x * blockDim.x + threadIdx.x;\r
+        int y = blockIdx.y * blockDim.y + threadIdx.y;\r
+\r
+        if (y <= half && x <= half)\r
+            *(table_space + y * step + x) = expf(-sqrtf(float(y * y) + float(x * x)) / dist_space);\r
+    }\r
+}\r
+\r
+namespace cv { namespace gpu { namespace bf \r
+{\r
+    void calc_space_weighted_filter_gpu(const DevMem2Df& table_space, int half, float dist_space, cudaStream_t stream)\r
+    {\r
+        dim3 threads(32, 8, 1);\r
+        dim3 grid(1, 1, 1);\r
+        grid.x = divUp(half + 1, threads.x);\r
+        grid.x = divUp(half + 1, threads.y);\r
+\r
+        bf_krnls::calc_space_weighted_filter<<<grid, threads, 0, stream>>>(table_space.ptr, table_space.step/sizeof(float), half, dist_space);\r
+\r
+        if (stream != 0)\r
+            cudaSafeCall( cudaThreadSynchronize() );\r
+    }\r
+}}}\r
+\r
+namespace bf_krnls\r
+{\r
+    __constant__ float* ctable_color;\r
+    __constant__ float* ctable_space;\r
+    __constant__ size_t ctable_space_step;\r
+\r
+    __constant__ int cndisp;\r
+    __constant__ int cradius;\r
+\r
+    __constant__ short cedge_disc;\r
+    __constant__ short cmax_disc;\r
+}\r
+\r
+namespace cv { namespace gpu { namespace bf \r
+{\r
+    void load_constants(float* table_color, const DevMem2Df& table_space, int ndisp, int radius, short edge_disc, short max_disc)\r
+    {\r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::ctable_color, &table_color, sizeof(table_color)) );\r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::ctable_space, &table_space.ptr, sizeof(table_space.ptr)) );\r
+        size_t table_space_step = table_space.step / sizeof(float);\r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::ctable_space_step, &table_space_step, sizeof(size_t)) );\r
+        \r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::cndisp, &ndisp, sizeof(int)) );\r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::cradius, &radius, sizeof(int)) );\r
+        \r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::cedge_disc, &edge_disc, sizeof(short)) );\r
+        cudaSafeCall( cudaMemcpyToSymbol(bf_krnls::cmax_disc, &max_disc, sizeof(short)) );\r
+    }\r
+}}}\r
+\r
+namespace bf_krnls\r
+{\r
+    template <int channels>\r
+    struct DistRgbMax\r
+    {\r
+        static __device__ uchar calc(const uchar* a, const uchar* b)\r
+        {\r
+            uchar x = abs(a[0] - b[0]);\r
+            uchar y = abs(a[1] - b[1]);\r
+            uchar z = abs(a[2] - b[2]);\r
+            return (max(max(x, y), z));\r
+        }\r
+    };\r
+\r
+    template <>\r
+    struct DistRgbMax<1>\r
+    {\r
+        static __device__ uchar calc(const uchar* a, const uchar* b)\r
+        {\r
+            return abs(a[0] - b[0]);\r
+        }\r
+    };\r
+\r
+    template <int channels, typename T>\r
+    __global__ void bilateral_filter(int t, T* disp, size_t disp_step, const uchar* img, size_t img_step, int h, int w)\r
+    {\r
+        const int y = blockIdx.y * blockDim.y + threadIdx.y;\r
+        const int x = ((blockIdx.x * blockDim.x + threadIdx.x) << 1) + ((y + t) & 1);\r
+\r
+        T dp[5];\r
+\r
+        if (y > 0 && y < h - 1 && x > 0 && x < w - 1)\r
+        {\r
+            dp[0] = *(disp + (y  ) * disp_step + x + 0);\r
+            dp[1] = *(disp + (y-1) * disp_step + x + 0);\r
+            dp[2] = *(disp + (y  ) * disp_step + x - 1);\r
+            dp[3] = *(disp + (y+1) * disp_step + x + 0);\r
+            dp[4] = *(disp + (y  ) * disp_step + x + 1);\r
+\r
+            if(abs(dp[1] - dp[0]) >= cedge_disc || abs(dp[2] - dp[0]) >= cedge_disc || abs(dp[3] - dp[0]) >= cedge_disc || abs(dp[4] - dp[0]) >= cedge_disc)            \r
+            {\r
+                const int ymin = max(0, y - cradius);\r
+                const int xmin = max(0, x - cradius);\r
+                const int ymax = min(h - 1, y + cradius);\r
+                const int xmax = min(w - 1, x + cradius);\r
+\r
+                float cost[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};\r
+\r
+                const uchar* ic = img + y * img_step + channels * x;\r
+\r
+                for(int yi = ymin; yi <= ymax; yi++)\r
+                {\r
+                    const T* disp_y = disp + yi * disp_step;\r
+\r
+                    for(int xi = xmin; xi <= xmax; xi++)\r
+                    {\r
+                        const uchar* in = img + yi * img_step + channels * xi;\r
+\r
+                        uchar dist_rgb = DistRgbMax<channels>::calc(in, ic);\r
+\r
+                        const float weight = ctable_color[dist_rgb] * (ctable_space + abs(y-yi)* ctable_space_step)[abs(x-xi)];\r
+\r
+                        const T disp_reg = disp_y[xi];\r
+\r
+                        cost[0] += min(cmax_disc, abs(disp_reg - dp[0])) * weight;\r
+                        cost[1] += min(cmax_disc, abs(disp_reg - dp[1])) * weight;\r
+                        cost[2] += min(cmax_disc, abs(disp_reg - dp[2])) * weight;\r
+                        cost[3] += min(cmax_disc, abs(disp_reg - dp[3])) * weight;\r
+                        cost[4] += min(cmax_disc, abs(disp_reg - dp[4])) * weight;\r
+                    }\r
+                }\r
+\r
+                float minimum = FLT_MAX;\r
+                int id = 0;\r
+\r
+                if (cost[0] < minimum)\r
+                {\r
+                    minimum = cost[0];\r
+                    id = 0;\r
+                }\r
+                if (cost[1] < minimum)\r
+                {\r
+                    minimum = cost[1];\r
+                    id = 1;\r
+                }\r
+                if (cost[2] < minimum)\r
+                {\r
+                    minimum = cost[2];\r
+                    id = 2;\r
+                }\r
+                if (cost[3] < minimum)\r
+                {\r
+                    minimum = cost[3];\r
+                    id = 3;\r
+                }\r
+                if (cost[4] < minimum)\r
+                {\r
+                    minimum = cost[4];\r
+                    id = 4;\r
+                }\r
+\r
+                *(disp + y * disp_step + x) = dp[id];\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
+namespace cv { namespace gpu { namespace bf \r
+{\r
+    template <typename T>     \r
+    void bilateral_filter_caller(const DevMem2D_<T>& disp, const DevMem2D& img, int channels, int iters, cudaStream_t stream)\r
+    {\r
+        dim3 threads(32, 8, 1);\r
+        dim3 grid(1, 1, 1);\r
+        grid.x = divUp(disp.cols, threads.x << 1);\r
+        grid.y = divUp(disp.rows, threads.y);\r
+\r
+        switch (channels)\r
+        {\r
+        case 1:\r
+            for (int i = 0; i < iters; ++i)\r
+            {\r
+                bf_krnls::bilateral_filter<1><<<grid, threads, 0, stream>>>(0, disp.ptr, disp.step/sizeof(T), img.ptr, img.step, disp.rows, disp.cols);\r
+                bf_krnls::bilateral_filter<1><<<grid, threads, 0, stream>>>(1, disp.ptr, disp.step/sizeof(T), img.ptr, img.step, disp.rows, disp.cols);\r
+            }\r
+            break;\r
+        case 3:\r
+            for (int i = 0; i < iters; ++i)\r
+            {\r
+                bf_krnls::bilateral_filter<3><<<grid, threads, 0, stream>>>(0, disp.ptr, disp.step/sizeof(T), img.ptr, img.step, disp.rows, disp.cols);\r
+                bf_krnls::bilateral_filter<3><<<grid, threads, 0, stream>>>(1, disp.ptr, disp.step/sizeof(T), img.ptr, img.step, disp.rows, disp.cols);\r
+            }\r
+            break;\r
+        default:\r
+            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);\r
+        }        \r
+\r
+        if (stream != 0)\r
+            cudaSafeCall( cudaThreadSynchronize() );\r
+    }\r
+\r
+    void bilateral_filter_gpu(const DevMem2D& disp, const DevMem2D& img, int channels, int iters, cudaStream_t stream)\r
+    {\r
+        bilateral_filter_caller(disp, img, channels, iters, stream);\r
+    }\r
+\r
+    void bilateral_filter_gpu(const DevMem2D_<short>& disp, const DevMem2D& img, int channels, int iters, cudaStream_t stream)\r
+    {\r
+        bilateral_filter_caller(disp, img, channels, iters, stream);\r
+    }\r
+}}}\r