speckle filtering added
authorAnatoly Baksheev <no@email>
Thu, 29 Jul 2010 08:50:19 +0000 (08:50 +0000)
committerAnatoly Baksheev <no@email>
Thu, 29 Jul 2010 08:50:19 +0000 (08:50 +0000)
modules/gpu/include/opencv2/gpu/gpu.hpp
modules/gpu/src/matrix_operations.cpp
modules/gpu/src/speckle_filtering.cpp [new file with mode: 0644]

index 038f00f..df2935d 100644 (file)
@@ -413,6 +413,12 @@ namespace cv
             GpuMat out;\r
         };        \r
     }\r
+\r
+    //! Speckle filtering - filters small connected components on diparity image.\r
+    //! It sets pixel (x,y) to newVal if it coresponds to small CC with size < maxSpeckleSize. \r
+    //! Threshold for border between CC is diffThreshold;\r
+    void filterSpeckles( Mat& img, uchar newVal, int maxSpeckleSize, uchar diffThreshold, Mat& buf);\r
+\r
 }\r
 #include "opencv2/gpu/matrix_operations.hpp"\r
 \r
index f0e0e7e..88d2e21 100644 (file)
@@ -164,7 +164,7 @@ GpuMat& GpuMat::setTo(const Scalar& s, const GpuMat& mask)
     else\r
         impl::set_to_with_mask( *this, depth(), s.val, mask, channels());\r
 \r
-    return *this;\r
+    return *this;   \r
 }\r
 \r
 \r
diff --git a/modules/gpu/src/speckle_filtering.cpp b/modules/gpu/src/speckle_filtering.cpp
new file mode 100644 (file)
index 0000000..628e11b
--- /dev/null
@@ -0,0 +1,159 @@
+/*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
+using namespace cv;\r
+\r
+typedef Point_<short> Point2s;\r
+\r
+void cv::filterSpeckles( Mat& img, uchar newVal, int maxSpeckleSize, uchar maxDiff, Mat& _buf)\r
+{\r
+    int MaxD = 1024;\r
+    int WinSz = 64;\r
+\r
+    int bufSize0 = (MaxD + 2)*sizeof(int) + (img.rows+WinSz+2)*MaxD*sizeof(int) +\r
+        (img.rows + WinSz + 2)*sizeof(int) +\r
+        (img.rows+WinSz+2)*MaxD*(WinSz+1)*sizeof(uchar) + 256;\r
+    int bufSize1 = (img.cols + 9 + 2) * sizeof(int) + 256;\r
+    int bufSz = max(bufSize0 * 1, bufSize1 * 2);\r
+\r
+    _buf.create(1, bufSz, CV_8U);\r
+\r
+    CV_Assert( img.type() == CV_8U );\r
+\r
+    int width = img.cols, height = img.rows, npixels = width*height;\r
+    size_t bufSize = npixels*(int)(sizeof(Point2s) + sizeof(int) + sizeof(uchar));\r
+    if( !_buf.isContinuous() || !_buf.data || _buf.cols*_buf.rows*_buf.elemSize() < bufSize )\r
+        _buf.create(1, bufSize, CV_8U);\r
+\r
+    uchar* buf = _buf.data;\r
+    int i, j, dstep = img.step/sizeof(uchar);\r
+    int* labels = (int*)buf;\r
+    buf += npixels*sizeof(labels[0]);\r
+    Point2s* wbuf = (Point2s*)buf;\r
+    buf += npixels*sizeof(wbuf[0]);\r
+    uchar* rtype = (uchar*)buf;\r
+    int curlabel = 0;\r
+\r
+    // clear out label assignments\r
+    memset(labels, 0, npixels*sizeof(labels[0]));\r
+\r
+    for( i = 0; i < height; i++ )\r
+    {\r
+        uchar* ds = img.ptr<uchar>(i);\r
+        int* ls = labels + width*i;\r
+\r
+        for( j = 0; j < width; j++ )\r
+        {\r
+            if( ds[j] != newVal )      // not a bad disparity\r
+            {\r
+                if( ls[j] )            // has a label, check for bad label\r
+                {  \r
+                    if( rtype[ls[j]] ) // small region, zero out disparity\r
+                        ds[j] = (uchar)newVal;\r
+                }\r
+                // no label, assign and propagate\r
+                else\r
+                {\r
+                    Point2s* ws = wbuf;        // initialize wavefront\r
+                    Point2s p((short)j, (short)i);     // current pixel\r
+                    curlabel++;        // next label\r
+                    int count = 0;     // current region size\r
+                    ls[j] = curlabel;\r
+\r
+                    // wavefront propagation\r
+                    while( ws >= wbuf ) // wavefront not empty\r
+                    {\r
+                        count++;\r
+                        // put neighbors onto wavefront\r
+                        uchar* dpp = &img.at<uchar>(p.y, p.x);\r
+                        uchar dp = *dpp;\r
+                        int* lpp = labels + width*p.y + p.x;\r
+\r
+                        if( p.x < width-1 && !lpp[+1] && dpp[+1] != newVal && std::abs(dp - dpp[+1]) <= maxDiff )\r
+                        {\r
+                            lpp[+1] = curlabel;\r
+                            *ws++ = Point2s(p.x+1, p.y);\r
+                        }\r
+\r
+                        if( p.x > 0 && !lpp[-1] && dpp[-1] != newVal && std::abs(dp - dpp[-1]) <= maxDiff )\r
+                        {\r
+                            lpp[-1] = curlabel;\r
+                            *ws++ = Point2s(p.x-1, p.y);\r
+                        }\r
+\r
+                        if( p.y < height-1 && !lpp[+width] && dpp[+dstep] != newVal && std::abs(dp - dpp[+dstep]) <= maxDiff )\r
+                        {\r
+                            lpp[+width] = curlabel;\r
+                            *ws++ = Point2s(p.x, p.y+1);\r
+                        }\r
+\r
+                        if( p.y > 0 && !lpp[-width] && dpp[-dstep] != newVal && std::abs(dp - dpp[-dstep]) <= maxDiff )\r
+                        {\r
+                            lpp[-width] = curlabel;\r
+                            *ws++ = Point2s(p.x, p.y-1);\r
+                        }\r
+\r
+                        // pop most recent and propagate\r
+                        // NB: could try least recent, maybe better convergence\r
+                        p = *--ws;\r
+                    }\r
+\r
+                    // assign label type\r
+                    if( count <= maxSpeckleSize )      // speckle region\r
+                    {\r
+                        //printf("count = %d\n", count);\r
+                        rtype[ls[j]] = 1;      // small region label\r
+                        ds[j] = (uchar)newVal;\r
+                    }\r
+                    else\r
+                    {\r
+                        //printf("count = %d\n", count);\r
+                        rtype[ls[j]] = 0;      // large region label\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
+}    \r
+\r