Skip to content

Commit a97a061

Browse files
ytyytyytalalek
authored andcommitted
Merge pull request #1906 from ytyytyyt:3.4
* fbs_filter add lambda & revise imtypes * fix warnings * fix fbs function prototypes
1 parent 567bcc9 commit a97a061

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void rollingGuidanceFilter(InputArray src, OutputArray dst, int d = -1, double s
375375
//////////////////////////////////////////////////////////////////////////
376376
//////////////////////////////////////////////////////////////////////////
377377

378-
/** @brief Interface for implementations of The Fast Bilateral Solver.
378+
/** @brief Interface for implementations of Fast Bilateral Solver.
379379
380380
For more details about this solver see @cite BarronPoole2016 .
381381
*/
@@ -389,6 +389,8 @@ class CV_EXPORTS_W FastBilateralSolverFilter : public Algorithm
389389
@param confidence confidence image with unsigned 8-bit or floating-point 32-bit confidence and 1 channel.
390390
391391
@param dst destination image.
392+
393+
@note Confidence images with CV_8U depth are expected to in [0, 255] and CV_32F in [0, 1] range.
392394
*/
393395
CV_WRAP virtual void filter(InputArray src, InputArray confidence, OutputArray dst) = 0;
394396
};
@@ -397,20 +399,23 @@ class CV_EXPORTS_W FastBilateralSolverFilter : public Algorithm
397399
398400
@param guide image serving as guide for filtering. It should have 8-bit depth and either 1 or 3 channels.
399401
400-
@param sigma_spatial parameter, that is similar to spatial space sigma in bilateralFilter.
402+
@param sigma_spatial parameter, that is similar to spatial space sigma (bandwidth) in bilateralFilter.
403+
404+
@param sigma_luma parameter, that is similar to luma space sigma (bandwidth) in bilateralFilter.
401405
402-
@param sigma_luma parameter, that is similar to luma space sigma in bilateralFilter.
406+
@param sigma_chroma parameter, that is similar to chroma space sigma (bandwidth) in bilateralFilter.
403407
404-
@param sigma_chroma parameter, that is similar to chroma space sigma in bilateralFilter.
408+
@param lambda smoothness strength parameter for solver.
405409
406-
@param num_iter number of iterations used for solving, 25 is usually enough.
410+
@param num_iter number of iterations used for solver, 25 is usually enough.
407411
408-
@param max_tol solving tolerance used for solving.
412+
@param max_tol convergence tolerance used for solver.
409413
410414
For more details about the Fast Bilateral Solver parameters, see the original paper @cite BarronPoole2016.
411415
412416
*/
413-
CV_EXPORTS_W Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter = 25, double max_tol = 1e-5);
417+
CV_EXPORTS_W Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda = 128.0, int num_iter = 25, double max_tol = 1e-5);
418+
414419

415420
/** @brief Simple one-line Fast Bilateral Solver filter call. If you have multiple images to filter with the same
416421
guide then use FastBilateralSolverFilter interface to avoid extra computations.
@@ -423,24 +428,27 @@ guide then use FastBilateralSolverFilter interface to avoid extra computations.
423428
424429
@param dst destination image.
425430
426-
@param sigma_spatial parameter, that is similar to spatial space sigma in bilateralFilter.
431+
@param sigma_spatial parameter, that is similar to spatial space sigma (bandwidth) in bilateralFilter.
432+
433+
@param sigma_luma parameter, that is similar to luma space sigma (bandwidth) in bilateralFilter.
427434
428-
@param sigma_luma parameter, that is similar to luma space sigma in bilateralFilter.
435+
@param sigma_chroma parameter, that is similar to chroma space sigma (bandwidth) in bilateralFilter.
429436
430-
@param sigma_chroma parameter, that is similar to chroma space sigma in bilateralFilter.
437+
@param lambda smoothness strength parameter for solver.
431438
432-
@param num_iter number of iterations used for solving, 25 is usually enough.
439+
@param num_iter number of iterations used for solver, 25 is usually enough.
433440
434-
@param max_tol solving tolerance used for solving.
441+
@param max_tol convergence tolerance used for solver.
442+
443+
For more details about the Fast Bilateral Solver parameters, see the original paper @cite BarronPoole2016.
435444
436445
@note Confidence images with CV_8U depth are expected to in [0, 255] and CV_32F in [0, 1] range.
437446
*/
438-
CV_EXPORTS_W void fastBilateralSolverFilter(InputArray guide, InputArray src, InputArray confidence, OutputArray dst, double sigma_spatial = 8, double sigma_luma = 8, double sigma_chroma = 8, int num_iter = 25, double max_tol = 1e-5);
447+
CV_EXPORTS_W void fastBilateralSolverFilter(InputArray guide, InputArray src, InputArray confidence, OutputArray dst, double sigma_spatial = 8, double sigma_luma = 8, double sigma_chroma = 8, double lambda = 128.0, int num_iter = 25, double max_tol = 1e-5);
439448

440449
//////////////////////////////////////////////////////////////////////////
441450
//////////////////////////////////////////////////////////////////////////
442451

443-
444452
/** @brief Interface for implementations of Fast Global Smoother filter.
445453
446454
For more details about this filter see @cite Min2014 and @cite Farbman2008 .

modules/ximgproc/samples/disparity_filtering.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const String keys =
3434
"{fbs_spatial |16.0 | parameter of fbs post-filtering }"
3535
"{fbs_luma |8.0 | parameter of fbs post-filtering }"
3636
"{fbs_chroma |8.0 | parameter of fbs post-filtering }"
37+
"{fbs_lambda |128.0 | parameter of fbs post-filtering }"
3738
;
3839

3940
int main(int argc, char** argv)
@@ -63,6 +64,7 @@ int main(int argc, char** argv)
6364
double fbs_spatial = parser.get<double>("fbs_spatial");
6465
double fbs_luma = parser.get<double>("fbs_luma");
6566
double fbs_chroma = parser.get<double>("fbs_chroma");
67+
double fbs_lambda = parser.get<double>("fbs_lambda");
6668
double vis_mult = parser.get<double>("vis_mult");
6769

6870
int wsize;
@@ -294,17 +296,18 @@ int main(int argc, char** argv)
294296
#ifdef HAVE_EIGEN
295297
//! [filtering_fbs]
296298
solving_time = (double)getTickCount();
297-
fastBilateralSolverFilter(left, left_disp_resized, conf_map/255.0f, solved_disp, fbs_spatial, fbs_luma, fbs_chroma);
299+
fastBilateralSolverFilter(left, left_disp_resized, conf_map/255.0f, solved_disp, fbs_spatial, fbs_luma, fbs_chroma, fbs_lambda);
298300
solving_time = ((double)getTickCount() - solving_time)/getTickFrequency();
299301
//! [filtering_fbs]
300302

301303
//! [filtering_wls2fbs]
302-
fastBilateralSolverFilter(left, filtered_disp, conf_map/255.0f, solved_filtered_disp, fbs_spatial, fbs_luma, fbs_chroma);
304+
fastBilateralSolverFilter(left, filtered_disp, conf_map/255.0f, solved_filtered_disp, fbs_spatial, fbs_luma, fbs_chroma, fbs_lambda);
303305
//! [filtering_wls2fbs]
304306
#else
305307
(void)fbs_spatial;
306308
(void)fbs_luma;
307309
(void)fbs_chroma;
310+
(void)fbs_lambda;
308311

309312
#endif
310313
}

modules/ximgproc/src/fbs_filter.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ namespace ximgproc
7777
{
7878
public:
7979

80-
static Ptr<FastBilateralSolverFilterImpl> create(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol)
80+
static Ptr<FastBilateralSolverFilterImpl> create(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
8181
{
8282
CV_Assert(guide.type() == CV_8UC1 || guide.type() == CV_8UC3);
8383
FastBilateralSolverFilterImpl *fbs = new FastBilateralSolverFilterImpl();
8484
Mat gui = guide.getMat();
85-
fbs->init(gui,sigma_spatial,sigma_luma,sigma_chroma,num_iter,max_tol);
85+
fbs->init(gui,sigma_spatial,sigma_luma,sigma_chroma,lambda,num_iter,max_tol);
8686
return Ptr<FastBilateralSolverFilterImpl>(fbs);
8787
}
8888

8989
void filter(InputArray src, InputArray confidence, OutputArray dst) CV_OVERRIDE
9090
{
9191

92-
CV_Assert(!src.empty() && (src.depth() == CV_8U || src.depth() == CV_16S || src.depth() == CV_32F) && src.channels()<=4);
92+
CV_Assert(!src.empty() && (src.depth() == CV_8U || src.depth() == CV_16S || src.depth() == CV_16U || src.depth() == CV_32F) && src.channels()<=4);
9393
CV_Assert(!confidence.empty() && (confidence.depth() == CV_8U || confidence.depth() == CV_32F) && confidence.channels()==1);
9494
if (src.rows() != rows || src.cols() != cols)
9595
{
@@ -133,7 +133,7 @@ namespace ximgproc
133133

134134
// protected:
135135
void solve(cv::Mat& src, cv::Mat& confidence, cv::Mat& dst);
136-
void init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol);
136+
void init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol);
137137

138138
void Splat(Eigen::VectorXf& input, Eigen::VectorXf& dst);
139139
void Blur(Eigen::VectorXf& input, Eigen::VectorXf& dst);
@@ -174,8 +174,8 @@ namespace ximgproc
174174
grid_params()
175175
{
176176
spatialSigma = 8.0;
177-
lumaSigma = 4.0;
178-
chromaSigma = 4.0;
177+
lumaSigma = 8.0;
178+
chromaSigma = 8.0;
179179
}
180180
};
181181

@@ -201,9 +201,10 @@ namespace ximgproc
201201

202202

203203

204-
void FastBilateralSolverFilterImpl::init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol)
204+
void FastBilateralSolverFilterImpl::init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
205205
{
206206

207+
bs_param.lam = lambda;
207208
bs_param.cg_maxiter = num_iter;
208209
bs_param.cg_tol = max_tol;
209210

@@ -266,7 +267,6 @@ namespace ximgproc
266267

267268
// construct Blur matrices
268269
Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices);
269-
Eigen::VectorXf ones_npixels = Eigen::VectorXf::Ones(npixels);
270270
diagonal(ones_nvertices,blurs);
271271
blurs *= 10;
272272
for(int offset = -1; offset <= 1;++offset)
@@ -379,7 +379,6 @@ namespace ximgproc
379379

380380
// construct Blur matrices
381381
Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices);
382-
Eigen::VectorXf ones_npixels = Eigen::VectorXf::Ones(npixels);
383382
diagonal(ones_nvertices,blurs);
384383
blurs *= 10;
385384
for(int offset = -1; offset <= 1;++offset)
@@ -486,6 +485,14 @@ namespace ximgproc
486485
x(i) = (cv::saturate_cast<float>(pft[i])+32768.0f)/65535.0f;
487486
}
488487
}
488+
else if(target.depth() == CV_16U)
489+
{
490+
const uint16_t *pft = reinterpret_cast<const uint16_t*>(target.data);
491+
for (int i = 0; i < npixels; i++)
492+
{
493+
x(i) = cv::saturate_cast<float>(pft[i])/65535.0f;
494+
}
495+
}
489496
else if(target.depth() == CV_8U)
490497
{
491498
const uchar *pft = reinterpret_cast<const uchar*>(target.data);
@@ -566,7 +573,15 @@ namespace ximgproc
566573
int16_t *pftar = (int16_t*) output.data;
567574
for (int i = 0; i < int(splat_idx.size()); i++)
568575
{
569-
pftar[i] = cv::saturate_cast<ushort>(y(splat_idx[i]) * 65535.0f - 32768.0f);
576+
pftar[i] = cv::saturate_cast<short>(y(splat_idx[i]) * 65535.0f - 32768.0f);
577+
}
578+
}
579+
else if(target.depth() == CV_16U)
580+
{
581+
uint16_t *pftar = (uint16_t*) output.data;
582+
for (int i = 0; i < int(splat_idx.size()); i++)
583+
{
584+
pftar[i] = cv::saturate_cast<ushort>(y(splat_idx[i]) * 65535.0f);
570585
}
571586
}
572587
else if (target.depth() == CV_8U)
@@ -592,14 +607,14 @@ namespace ximgproc
592607

593608
////////////////////////////////////////////////////////////////////////////
594609
////////////////////////////////////////////////////////////////////////////
595-
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol)
610+
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
596611
{
597-
return Ptr<FastBilateralSolverFilter>(FastBilateralSolverFilterImpl::create(guide, sigma_spatial, sigma_luma, sigma_chroma, num_iter, max_tol));
612+
return Ptr<FastBilateralSolverFilter>(FastBilateralSolverFilterImpl::create(guide, sigma_spatial, sigma_luma, sigma_chroma, lambda, num_iter, max_tol));
598613
}
599614

600-
void fastBilateralSolverFilter(InputArray guide, InputArray src, InputArray confidence, OutputArray dst, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol)
615+
void fastBilateralSolverFilter(InputArray guide, InputArray src, InputArray confidence, OutputArray dst, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
601616
{
602-
Ptr<FastBilateralSolverFilter> fbs = createFastBilateralSolverFilter(guide, sigma_spatial, sigma_luma, sigma_chroma, num_iter, max_tol);
617+
Ptr<FastBilateralSolverFilter> fbs = createFastBilateralSolverFilter(guide, sigma_spatial, sigma_luma, sigma_chroma, lambda, num_iter, max_tol);
603618
fbs->filter(src, confidence, dst);
604619
}
605620

@@ -614,12 +629,12 @@ namespace cv
614629
namespace ximgproc
615630
{
616631

617-
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray, double, double, double, int, double)
632+
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray, double, double, double, double, int, double)
618633
{
619634
CV_Error(Error::StsNotImplemented, "createFastBilateralSolverFilter : needs to be compiled with EIGEN");
620635
}
621636

622-
void fastBilateralSolverFilter(InputArray, InputArray, InputArray, OutputArray, double, double, double, int, double)
637+
void fastBilateralSolverFilter(InputArray, InputArray, InputArray, OutputArray, double, double, double, double, int, double)
623638
{
624639
CV_Error(Error::StsNotImplemented, "fastBilateralSolverFilter : needs to be compiled with EIGEN");
625640
}

0 commit comments

Comments
 (0)