Commit a97a0612 authored by Jukka Komulainen's avatar Jukka Komulainen Committed by Alexander Alekhin

Merge pull request #1906 from ytyytyyt:3.4

* fbs_filter add lambda & revise imtypes

* fix warnings

* fix fbs function prototypes
parent 567bcc95
...@@ -375,7 +375,7 @@ void rollingGuidanceFilter(InputArray src, OutputArray dst, int d = -1, double s ...@@ -375,7 +375,7 @@ void rollingGuidanceFilter(InputArray src, OutputArray dst, int d = -1, double s
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/** @brief Interface for implementations of The Fast Bilateral Solver. /** @brief Interface for implementations of Fast Bilateral Solver.
For more details about this solver see @cite BarronPoole2016 . For more details about this solver see @cite BarronPoole2016 .
*/ */
...@@ -389,6 +389,8 @@ public: ...@@ -389,6 +389,8 @@ public:
@param confidence confidence image with unsigned 8-bit or floating-point 32-bit confidence and 1 channel. @param confidence confidence image with unsigned 8-bit or floating-point 32-bit confidence and 1 channel.
@param dst destination image. @param dst destination image.
@note Confidence images with CV_8U depth are expected to in [0, 255] and CV_32F in [0, 1] range.
*/ */
CV_WRAP virtual void filter(InputArray src, InputArray confidence, OutputArray dst) = 0; CV_WRAP virtual void filter(InputArray src, InputArray confidence, OutputArray dst) = 0;
}; };
...@@ -397,20 +399,23 @@ public: ...@@ -397,20 +399,23 @@ public:
@param guide image serving as guide for filtering. It should have 8-bit depth and either 1 or 3 channels. @param guide image serving as guide for filtering. It should have 8-bit depth and either 1 or 3 channels.
@param sigma_spatial parameter, that is similar to spatial space sigma in bilateralFilter. @param sigma_spatial parameter, that is similar to spatial space sigma (bandwidth) in bilateralFilter.
@param sigma_luma parameter, that is similar to luma space sigma (bandwidth) in bilateralFilter.
@param sigma_luma parameter, that is similar to luma space sigma in bilateralFilter. @param sigma_chroma parameter, that is similar to chroma space sigma (bandwidth) in bilateralFilter.
@param sigma_chroma parameter, that is similar to chroma space sigma in bilateralFilter. @param lambda smoothness strength parameter for solver.
@param num_iter number of iterations used for solving, 25 is usually enough. @param num_iter number of iterations used for solver, 25 is usually enough.
@param max_tol solving tolerance used for solving. @param max_tol convergence tolerance used for solver.
For more details about the Fast Bilateral Solver parameters, see the original paper @cite BarronPoole2016. For more details about the Fast Bilateral Solver parameters, see the original paper @cite BarronPoole2016.
*/ */
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); 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);
/** @brief Simple one-line Fast Bilateral Solver filter call. If you have multiple images to filter with the same /** @brief Simple one-line Fast Bilateral Solver filter call. If you have multiple images to filter with the same
guide then use FastBilateralSolverFilter interface to avoid extra computations. guide then use FastBilateralSolverFilter interface to avoid extra computations.
...@@ -423,24 +428,27 @@ guide then use FastBilateralSolverFilter interface to avoid extra computations. ...@@ -423,24 +428,27 @@ guide then use FastBilateralSolverFilter interface to avoid extra computations.
@param dst destination image. @param dst destination image.
@param sigma_spatial parameter, that is similar to spatial space sigma in bilateralFilter. @param sigma_spatial parameter, that is similar to spatial space sigma (bandwidth) in bilateralFilter.
@param sigma_luma parameter, that is similar to luma space sigma (bandwidth) in bilateralFilter.
@param sigma_luma parameter, that is similar to luma space sigma in bilateralFilter. @param sigma_chroma parameter, that is similar to chroma space sigma (bandwidth) in bilateralFilter.
@param sigma_chroma parameter, that is similar to chroma space sigma in bilateralFilter. @param lambda smoothness strength parameter for solver.
@param num_iter number of iterations used for solving, 25 is usually enough. @param num_iter number of iterations used for solver, 25 is usually enough.
@param max_tol solving tolerance used for solving. @param max_tol convergence tolerance used for solver.
For more details about the Fast Bilateral Solver parameters, see the original paper @cite BarronPoole2016.
@note Confidence images with CV_8U depth are expected to in [0, 255] and CV_32F in [0, 1] range. @note Confidence images with CV_8U depth are expected to in [0, 255] and CV_32F in [0, 1] range.
*/ */
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); 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);
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/** @brief Interface for implementations of Fast Global Smoother filter. /** @brief Interface for implementations of Fast Global Smoother filter.
For more details about this filter see @cite Min2014 and @cite Farbman2008 . For more details about this filter see @cite Min2014 and @cite Farbman2008 .
......
...@@ -34,6 +34,7 @@ const String keys = ...@@ -34,6 +34,7 @@ const String keys =
"{fbs_spatial |16.0 | parameter of fbs post-filtering }" "{fbs_spatial |16.0 | parameter of fbs post-filtering }"
"{fbs_luma |8.0 | parameter of fbs post-filtering }" "{fbs_luma |8.0 | parameter of fbs post-filtering }"
"{fbs_chroma |8.0 | parameter of fbs post-filtering }" "{fbs_chroma |8.0 | parameter of fbs post-filtering }"
"{fbs_lambda |128.0 | parameter of fbs post-filtering }"
; ;
int main(int argc, char** argv) int main(int argc, char** argv)
...@@ -63,6 +64,7 @@ int main(int argc, char** argv) ...@@ -63,6 +64,7 @@ int main(int argc, char** argv)
double fbs_spatial = parser.get<double>("fbs_spatial"); double fbs_spatial = parser.get<double>("fbs_spatial");
double fbs_luma = parser.get<double>("fbs_luma"); double fbs_luma = parser.get<double>("fbs_luma");
double fbs_chroma = parser.get<double>("fbs_chroma"); double fbs_chroma = parser.get<double>("fbs_chroma");
double fbs_lambda = parser.get<double>("fbs_lambda");
double vis_mult = parser.get<double>("vis_mult"); double vis_mult = parser.get<double>("vis_mult");
int wsize; int wsize;
...@@ -294,17 +296,18 @@ int main(int argc, char** argv) ...@@ -294,17 +296,18 @@ int main(int argc, char** argv)
#ifdef HAVE_EIGEN #ifdef HAVE_EIGEN
//! [filtering_fbs] //! [filtering_fbs]
solving_time = (double)getTickCount(); solving_time = (double)getTickCount();
fastBilateralSolverFilter(left, left_disp_resized, conf_map/255.0f, solved_disp, fbs_spatial, fbs_luma, fbs_chroma); fastBilateralSolverFilter(left, left_disp_resized, conf_map/255.0f, solved_disp, fbs_spatial, fbs_luma, fbs_chroma, fbs_lambda);
solving_time = ((double)getTickCount() - solving_time)/getTickFrequency(); solving_time = ((double)getTickCount() - solving_time)/getTickFrequency();
//! [filtering_fbs] //! [filtering_fbs]
//! [filtering_wls2fbs] //! [filtering_wls2fbs]
fastBilateralSolverFilter(left, filtered_disp, conf_map/255.0f, solved_filtered_disp, fbs_spatial, fbs_luma, fbs_chroma); fastBilateralSolverFilter(left, filtered_disp, conf_map/255.0f, solved_filtered_disp, fbs_spatial, fbs_luma, fbs_chroma, fbs_lambda);
//! [filtering_wls2fbs] //! [filtering_wls2fbs]
#else #else
(void)fbs_spatial; (void)fbs_spatial;
(void)fbs_luma; (void)fbs_luma;
(void)fbs_chroma; (void)fbs_chroma;
(void)fbs_lambda;
#endif #endif
} }
......
...@@ -77,19 +77,19 @@ namespace ximgproc ...@@ -77,19 +77,19 @@ namespace ximgproc
{ {
public: public:
static Ptr<FastBilateralSolverFilterImpl> create(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol) static Ptr<FastBilateralSolverFilterImpl> create(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
{ {
CV_Assert(guide.type() == CV_8UC1 || guide.type() == CV_8UC3); CV_Assert(guide.type() == CV_8UC1 || guide.type() == CV_8UC3);
FastBilateralSolverFilterImpl *fbs = new FastBilateralSolverFilterImpl(); FastBilateralSolverFilterImpl *fbs = new FastBilateralSolverFilterImpl();
Mat gui = guide.getMat(); Mat gui = guide.getMat();
fbs->init(gui,sigma_spatial,sigma_luma,sigma_chroma,num_iter,max_tol); fbs->init(gui,sigma_spatial,sigma_luma,sigma_chroma,lambda,num_iter,max_tol);
return Ptr<FastBilateralSolverFilterImpl>(fbs); return Ptr<FastBilateralSolverFilterImpl>(fbs);
} }
void filter(InputArray src, InputArray confidence, OutputArray dst) CV_OVERRIDE void filter(InputArray src, InputArray confidence, OutputArray dst) CV_OVERRIDE
{ {
CV_Assert(!src.empty() && (src.depth() == CV_8U || src.depth() == CV_16S || src.depth() == CV_32F) && src.channels()<=4); CV_Assert(!src.empty() && (src.depth() == CV_8U || src.depth() == CV_16S || src.depth() == CV_16U || src.depth() == CV_32F) && src.channels()<=4);
CV_Assert(!confidence.empty() && (confidence.depth() == CV_8U || confidence.depth() == CV_32F) && confidence.channels()==1); CV_Assert(!confidence.empty() && (confidence.depth() == CV_8U || confidence.depth() == CV_32F) && confidence.channels()==1);
if (src.rows() != rows || src.cols() != cols) if (src.rows() != rows || src.cols() != cols)
{ {
...@@ -133,7 +133,7 @@ namespace ximgproc ...@@ -133,7 +133,7 @@ namespace ximgproc
// protected: // protected:
void solve(cv::Mat& src, cv::Mat& confidence, cv::Mat& dst); void solve(cv::Mat& src, cv::Mat& confidence, cv::Mat& dst);
void init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol); void init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol);
void Splat(Eigen::VectorXf& input, Eigen::VectorXf& dst); void Splat(Eigen::VectorXf& input, Eigen::VectorXf& dst);
void Blur(Eigen::VectorXf& input, Eigen::VectorXf& dst); void Blur(Eigen::VectorXf& input, Eigen::VectorXf& dst);
...@@ -174,8 +174,8 @@ namespace ximgproc ...@@ -174,8 +174,8 @@ namespace ximgproc
grid_params() grid_params()
{ {
spatialSigma = 8.0; spatialSigma = 8.0;
lumaSigma = 4.0; lumaSigma = 8.0;
chromaSigma = 4.0; chromaSigma = 8.0;
} }
}; };
...@@ -201,9 +201,10 @@ namespace ximgproc ...@@ -201,9 +201,10 @@ namespace ximgproc
void FastBilateralSolverFilterImpl::init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol) void FastBilateralSolverFilterImpl::init(cv::Mat& reference, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
{ {
bs_param.lam = lambda;
bs_param.cg_maxiter = num_iter; bs_param.cg_maxiter = num_iter;
bs_param.cg_tol = max_tol; bs_param.cg_tol = max_tol;
...@@ -266,7 +267,6 @@ namespace ximgproc ...@@ -266,7 +267,6 @@ namespace ximgproc
// construct Blur matrices // construct Blur matrices
Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices); Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices);
Eigen::VectorXf ones_npixels = Eigen::VectorXf::Ones(npixels);
diagonal(ones_nvertices,blurs); diagonal(ones_nvertices,blurs);
blurs *= 10; blurs *= 10;
for(int offset = -1; offset <= 1;++offset) for(int offset = -1; offset <= 1;++offset)
...@@ -379,7 +379,6 @@ namespace ximgproc ...@@ -379,7 +379,6 @@ namespace ximgproc
// construct Blur matrices // construct Blur matrices
Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices); Eigen::VectorXf ones_nvertices = Eigen::VectorXf::Ones(nvertices);
Eigen::VectorXf ones_npixels = Eigen::VectorXf::Ones(npixels);
diagonal(ones_nvertices,blurs); diagonal(ones_nvertices,blurs);
blurs *= 10; blurs *= 10;
for(int offset = -1; offset <= 1;++offset) for(int offset = -1; offset <= 1;++offset)
...@@ -486,6 +485,14 @@ namespace ximgproc ...@@ -486,6 +485,14 @@ namespace ximgproc
x(i) = (cv::saturate_cast<float>(pft[i])+32768.0f)/65535.0f; x(i) = (cv::saturate_cast<float>(pft[i])+32768.0f)/65535.0f;
} }
} }
else if(target.depth() == CV_16U)
{
const uint16_t *pft = reinterpret_cast<const uint16_t*>(target.data);
for (int i = 0; i < npixels; i++)
{
x(i) = cv::saturate_cast<float>(pft[i])/65535.0f;
}
}
else if(target.depth() == CV_8U) else if(target.depth() == CV_8U)
{ {
const uchar *pft = reinterpret_cast<const uchar*>(target.data); const uchar *pft = reinterpret_cast<const uchar*>(target.data);
...@@ -566,7 +573,15 @@ namespace ximgproc ...@@ -566,7 +573,15 @@ namespace ximgproc
int16_t *pftar = (int16_t*) output.data; int16_t *pftar = (int16_t*) output.data;
for (int i = 0; i < int(splat_idx.size()); i++) for (int i = 0; i < int(splat_idx.size()); i++)
{ {
pftar[i] = cv::saturate_cast<ushort>(y(splat_idx[i]) * 65535.0f - 32768.0f); pftar[i] = cv::saturate_cast<short>(y(splat_idx[i]) * 65535.0f - 32768.0f);
}
}
else if(target.depth() == CV_16U)
{
uint16_t *pftar = (uint16_t*) output.data;
for (int i = 0; i < int(splat_idx.size()); i++)
{
pftar[i] = cv::saturate_cast<ushort>(y(splat_idx[i]) * 65535.0f);
} }
} }
else if (target.depth() == CV_8U) else if (target.depth() == CV_8U)
...@@ -592,14 +607,14 @@ namespace ximgproc ...@@ -592,14 +607,14 @@ namespace ximgproc
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, int num_iter, double max_tol) Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray guide, double sigma_spatial, double sigma_luma, double sigma_chroma, double lambda, int num_iter, double max_tol)
{ {
return Ptr<FastBilateralSolverFilter>(FastBilateralSolverFilterImpl::create(guide, sigma_spatial, sigma_luma, sigma_chroma, num_iter, max_tol)); return Ptr<FastBilateralSolverFilter>(FastBilateralSolverFilterImpl::create(guide, sigma_spatial, sigma_luma, sigma_chroma, lambda, num_iter, max_tol));
} }
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) 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)
{ {
Ptr<FastBilateralSolverFilter> fbs = createFastBilateralSolverFilter(guide, sigma_spatial, sigma_luma, sigma_chroma, num_iter, max_tol); Ptr<FastBilateralSolverFilter> fbs = createFastBilateralSolverFilter(guide, sigma_spatial, sigma_luma, sigma_chroma, lambda, num_iter, max_tol);
fbs->filter(src, confidence, dst); fbs->filter(src, confidence, dst);
} }
...@@ -614,12 +629,12 @@ namespace cv ...@@ -614,12 +629,12 @@ namespace cv
namespace ximgproc namespace ximgproc
{ {
Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray, double, double, double, int, double) Ptr<FastBilateralSolverFilter> createFastBilateralSolverFilter(InputArray, double, double, double, double, int, double)
{ {
CV_Error(Error::StsNotImplemented, "createFastBilateralSolverFilter : needs to be compiled with EIGEN"); CV_Error(Error::StsNotImplemented, "createFastBilateralSolverFilter : needs to be compiled with EIGEN");
} }
void fastBilateralSolverFilter(InputArray, InputArray, InputArray, OutputArray, double, double, double, int, double) void fastBilateralSolverFilter(InputArray, InputArray, InputArray, OutputArray, double, double, double, double, int, double)
{ {
CV_Error(Error::StsNotImplemented, "fastBilateralSolverFilter : needs to be compiled with EIGEN"); CV_Error(Error::StsNotImplemented, "fastBilateralSolverFilter : needs to be compiled with EIGEN");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment