From: Anatoly Baksheev Date: Thu, 29 Jul 2010 08:50:19 +0000 (+0000) Subject: speckle filtering added X-Git-Tag: accepted/2.0/20130307.220821~4646 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9ac17415552ec7757da2ed5ae16c594a6afdaad1;p=profile%2Fivi%2Fopencv.git speckle filtering added --- diff --git a/modules/gpu/include/opencv2/gpu/gpu.hpp b/modules/gpu/include/opencv2/gpu/gpu.hpp index 038f00f..df2935d 100644 --- a/modules/gpu/include/opencv2/gpu/gpu.hpp +++ b/modules/gpu/include/opencv2/gpu/gpu.hpp @@ -413,6 +413,12 @@ namespace cv GpuMat out; }; } + + //! Speckle filtering - filters small connected components on diparity image. + //! It sets pixel (x,y) to newVal if it coresponds to small CC with size < maxSpeckleSize. + //! Threshold for border between CC is diffThreshold; + void filterSpeckles( Mat& img, uchar newVal, int maxSpeckleSize, uchar diffThreshold, Mat& buf); + } #include "opencv2/gpu/matrix_operations.hpp" diff --git a/modules/gpu/src/matrix_operations.cpp b/modules/gpu/src/matrix_operations.cpp index f0e0e7e..88d2e21 100644 --- a/modules/gpu/src/matrix_operations.cpp +++ b/modules/gpu/src/matrix_operations.cpp @@ -164,7 +164,7 @@ GpuMat& GpuMat::setTo(const Scalar& s, const GpuMat& mask) else impl::set_to_with_mask( *this, depth(), s.val, mask, channels()); - return *this; + return *this; } diff --git a/modules/gpu/src/speckle_filtering.cpp b/modules/gpu/src/speckle_filtering.cpp new file mode 100644 index 0000000..628e11b --- /dev/null +++ b/modules/gpu/src/speckle_filtering.cpp @@ -0,0 +1,159 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" + +using namespace cv; + +typedef Point_ Point2s; + +void cv::filterSpeckles( Mat& img, uchar newVal, int maxSpeckleSize, uchar maxDiff, Mat& _buf) +{ + int MaxD = 1024; + int WinSz = 64; + + int bufSize0 = (MaxD + 2)*sizeof(int) + (img.rows+WinSz+2)*MaxD*sizeof(int) + + (img.rows + WinSz + 2)*sizeof(int) + + (img.rows+WinSz+2)*MaxD*(WinSz+1)*sizeof(uchar) + 256; + int bufSize1 = (img.cols + 9 + 2) * sizeof(int) + 256; + int bufSz = max(bufSize0 * 1, bufSize1 * 2); + + _buf.create(1, bufSz, CV_8U); + + CV_Assert( img.type() == CV_8U ); + + int width = img.cols, height = img.rows, npixels = width*height; + size_t bufSize = npixels*(int)(sizeof(Point2s) + sizeof(int) + sizeof(uchar)); + if( !_buf.isContinuous() || !_buf.data || _buf.cols*_buf.rows*_buf.elemSize() < bufSize ) + _buf.create(1, bufSize, CV_8U); + + uchar* buf = _buf.data; + int i, j, dstep = img.step/sizeof(uchar); + int* labels = (int*)buf; + buf += npixels*sizeof(labels[0]); + Point2s* wbuf = (Point2s*)buf; + buf += npixels*sizeof(wbuf[0]); + uchar* rtype = (uchar*)buf; + int curlabel = 0; + + // clear out label assignments + memset(labels, 0, npixels*sizeof(labels[0])); + + for( i = 0; i < height; i++ ) + { + uchar* ds = img.ptr(i); + int* ls = labels + width*i; + + for( j = 0; j < width; j++ ) + { + if( ds[j] != newVal ) // not a bad disparity + { + if( ls[j] ) // has a label, check for bad label + { + if( rtype[ls[j]] ) // small region, zero out disparity + ds[j] = (uchar)newVal; + } + // no label, assign and propagate + else + { + Point2s* ws = wbuf; // initialize wavefront + Point2s p((short)j, (short)i); // current pixel + curlabel++; // next label + int count = 0; // current region size + ls[j] = curlabel; + + // wavefront propagation + while( ws >= wbuf ) // wavefront not empty + { + count++; + // put neighbors onto wavefront + uchar* dpp = &img.at(p.y, p.x); + uchar dp = *dpp; + int* lpp = labels + width*p.y + p.x; + + if( p.x < width-1 && !lpp[+1] && dpp[+1] != newVal && std::abs(dp - dpp[+1]) <= maxDiff ) + { + lpp[+1] = curlabel; + *ws++ = Point2s(p.x+1, p.y); + } + + if( p.x > 0 && !lpp[-1] && dpp[-1] != newVal && std::abs(dp - dpp[-1]) <= maxDiff ) + { + lpp[-1] = curlabel; + *ws++ = Point2s(p.x-1, p.y); + } + + if( p.y < height-1 && !lpp[+width] && dpp[+dstep] != newVal && std::abs(dp - dpp[+dstep]) <= maxDiff ) + { + lpp[+width] = curlabel; + *ws++ = Point2s(p.x, p.y+1); + } + + if( p.y > 0 && !lpp[-width] && dpp[-dstep] != newVal && std::abs(dp - dpp[-dstep]) <= maxDiff ) + { + lpp[-width] = curlabel; + *ws++ = Point2s(p.x, p.y-1); + } + + // pop most recent and propagate + // NB: could try least recent, maybe better convergence + p = *--ws; + } + + // assign label type + if( count <= maxSpeckleSize ) // speckle region + { + //printf("count = %d\n", count); + rtype[ls[j]] = 1; // small region label + ds[j] = (uchar)newVal; + } + else + { + //printf("count = %d\n", count); + rtype[ls[j]] = 0; // large region label + } + } + } + } + } +} +