Skip to content

Commit c57b27e

Browse files
committed
Add rolling guidance filter
1 parent 1c0cb8b commit c57b27e

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed

modules/ximgproc/include/opencv2/ximgproc/edge_filter.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,38 @@ void jointBilateralFilter(InputArray joint, InputArray src, OutputArray dst, int
319319
//////////////////////////////////////////////////////////////////////////
320320
//////////////////////////////////////////////////////////////////////////
321321

322+
/** @brief Applies the rolling guidance filter to an image.
323+
324+
@param src Source 8-bit or floating-point, 1-channel or 3-channel image.
325+
326+
@param dst Destination image of the same size and type as src.
327+
328+
@param d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,
329+
it is computed from sigmaSpace .
330+
331+
@param sigmaColor Filter sigma in the color space. A larger value of the parameter means that
332+
farther colors within the pixel neighborhood (see sigmaSpace ) will be mixed together, resulting in
333+
larger areas of semi-equal color.
334+
335+
@param sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that
336+
farther pixels will influence each other as long as their colors are close enough (see sigmaColor ).
337+
When d\>0 , it specifies the neighborhood size regardless of sigmaSpace . Otherwise, d is
338+
proportional to sigmaSpace .
339+
340+
@param numOfIter Number of iterations of joint edge-preserving filtering applied on the source image.
341+
342+
@param borderType
343+
344+
@note rollingGuidanceFilter uses jointBilateralFilter as the edge-preserving filter.
345+
346+
@sa jointBilateralFilter, bilateralFilter, amFilter
347+
*/
348+
CV_EXPORTS_W
349+
void rollingGuidanceFilter(InputArray src, OutputArray dst, int d = -1, double sigmaColor = 25, double sigmaSpace = 3, int numOfIter = 4, int borderType = BORDER_DEFAULT);
350+
351+
//////////////////////////////////////////////////////////////////////////
352+
//////////////////////////////////////////////////////////////////////////
353+
322354

323355
/** @brief Interface for implementations of Fast Global Smoother filter.
324356
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* By downloading, copying, installing or using the software you agree to this license.
3+
* If you do not agree to this license, do not download, install,
4+
* copy or use the software.
5+
*
6+
*
7+
* License Agreement
8+
* For Open Source Computer Vision Library
9+
* (3 - clause BSD License)
10+
*
11+
* Redistribution and use in source and binary forms, with or without modification,
12+
* are permitted provided that the following conditions are met :
13+
*
14+
* *Redistributions of source code must retain the above copyright notice,
15+
* this list of conditions and the following disclaimer.
16+
*
17+
* * Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and / or other materials provided with the distribution.
20+
*
21+
* * Neither the names of the copyright holders nor the names of the contributors
22+
* may be used to endorse or promote products derived from this software
23+
* without specific prior written permission.
24+
*
25+
* This software is provided by the copyright holders and contributors "as is" and
26+
* any express or implied warranties, including, but not limited to, the implied
27+
* warranties of merchantability and fitness for a particular purpose are disclaimed.
28+
* In no event shall copyright holders or contributors be liable for any direct,
29+
* indirect, incidental, special, exemplary, or consequential damages
30+
* (including, but not limited to, procurement of substitute goods or services;
31+
* loss of use, data, or profits; or business interruption) however caused
32+
* and on any theory of liability, whether in contract, strict liability,
33+
* or tort(including negligence or otherwise) arising in any way out of
34+
* the use of this software, even if advised of the possibility of such damage.
35+
*/
36+
37+
#include "perf_precomp.hpp"
38+
39+
namespace cvtest
40+
{
41+
42+
using std::tr1::tuple;
43+
using std::tr1::get;
44+
using namespace perf;
45+
using namespace testing;
46+
using namespace cv;
47+
using namespace cv::ximgproc;
48+
49+
typedef tuple<double, Size, MatType, int> RGFTestParam;
50+
typedef TestBaseWithParam<RGFTestParam> RollingGuidanceFilterTest;
51+
52+
PERF_TEST_P(RollingGuidanceFilterTest, perf,
53+
Combine(
54+
Values(2.0, 4.0, 6.0, 10.0),
55+
SZ_TYPICAL,
56+
Values(CV_8U, CV_32F),
57+
Values(1, 3))
58+
)
59+
{
60+
RGFTestParam params = GetParam();
61+
double sigmaS = get<0>(params);
62+
Size sz = get<1>(params);
63+
int depth = get<2>(params);
64+
int srcCn = get<3>(params);
65+
66+
Mat src(sz, CV_MAKE_TYPE(depth, srcCn));
67+
Mat dst(sz, src.type());
68+
69+
cv::setNumThreads(cv::getNumberOfCPUs());
70+
declare.in(src, WARMUP_RNG).out(dst).tbb_threads(cv::getNumberOfCPUs());
71+
72+
RNG rnd(cvRound(10*sigmaS) + sz.height + depth + srcCn);
73+
double sigmaC = rnd.uniform(1.0, 255.0);
74+
int iterNum = int(rnd.uniform(1.0, 5.0));
75+
76+
TEST_CYCLE_N(1)
77+
{
78+
rollingGuidanceFilter(src, dst, -1, sigmaC, sigmaS, iterNum);
79+
}
80+
81+
SANITY_CHECK_NOTHING();
82+
}
83+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* By downloading, copying, installing or using the software you agree to this license.
3+
* If you do not agree to this license, do not download, install,
4+
* copy or use the software.
5+
*
6+
*
7+
* License Agreement
8+
* For Open Source Computer Vision Library
9+
* (3 - clause BSD License)
10+
*
11+
* Redistribution and use in source and binary forms, with or without modification,
12+
* are permitted provided that the following conditions are met :
13+
*
14+
* *Redistributions of source code must retain the above copyright notice,
15+
* this list of conditions and the following disclaimer.
16+
*
17+
* * Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and / or other materials provided with the distribution.
20+
*
21+
* * Neither the names of the copyright holders nor the names of the contributors
22+
* may be used to endorse or promote products derived from this software
23+
* without specific prior written permission.
24+
*
25+
* This software is provided by the copyright holders and contributors "as is" and
26+
* any express or implied warranties, including, but not limited to, the implied
27+
* warranties of merchantability and fitness for a particular purpose are disclaimed.
28+
* In no event shall copyright holders or contributors be liable for any direct,
29+
* indirect, incidental, special, exemplary, or consequential damages
30+
* (including, but not limited to, procurement of substitute goods or services;
31+
* loss of use, data, or profits; or business interruption) however caused
32+
* and on any theory of liability, whether in contract, strict liability,
33+
* or tort(including negligence or otherwise) arising in any way out of
34+
* the use of this software, even if advised of the possibility of such damage.
35+
*/
36+
37+
#include "precomp.hpp"
38+
#include <opencv2/ximgproc.hpp>
39+
#include <opencv2/highgui.hpp>
40+
41+
namespace cv
42+
{
43+
namespace ximgproc
44+
{
45+
void rollingGuidanceFilter(InputArray src_, OutputArray dst_, int d,
46+
double sigmaColor, double sigmaSpace, int numOfIter, int borderType)
47+
{
48+
CV_Assert(!src_.empty());
49+
50+
Mat guidance = src_.getMat();
51+
Mat src = src_.getMat();
52+
53+
CV_Assert(src.size() == guidance.size());
54+
CV_Assert(src.depth() == guidance.depth() && (src.depth() == CV_8U || src.depth() == CV_32F) );
55+
56+
if (sigmaColor <= 0)
57+
sigmaColor = 1;
58+
if (sigmaSpace <= 0)
59+
sigmaSpace = 1;
60+
61+
dst_.create(src.size(), src.type());
62+
Mat dst = dst_.getMat();
63+
64+
if (src.data == guidance.data)
65+
guidance = guidance.clone();
66+
if (dst.data == src.data)
67+
src = src.clone();
68+
69+
int srcCnNum = src.channels();
70+
71+
if (srcCnNum == 1 || srcCnNum == 3)
72+
{
73+
while(numOfIter--){
74+
jointBilateralFilter(guidance, src, guidance, d, sigmaColor, sigmaSpace, borderType);
75+
}
76+
guidance.copyTo(dst_);
77+
}
78+
else
79+
{
80+
CV_Error(Error::BadNumChannels, "Unsupported number of channels");
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)