Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions modules/ximgproc/src/sparse_match_interpolators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,28 +1153,31 @@ void RICInterpolatorImpl::interpolate(InputArray from_image, InputArray from_poi
Mat matDistanceMap(src_size, CV_32FC1);
matDistanceMap.setTo(1e10);

Mat workCostMap(src_size, CV_32FC1);
if (costMap.empty())
{
costMap.create(src_size, CV_32FC1);
computeGradientMagnitude(src, costMap);
computeGradientMagnitude(src, workCostMap);
}
else
{
CV_Assert(costMap.rows == src.rows && costMap.cols == src.cols );
costMap.copyTo(workCostMap);
}

costMap = (1000.0f - lambda) + lambda * costMap;
workCostMap = (1000.0f - lambda) + lambda * workCostMap;

for (unsigned int i = 0; i < matches_vector.size(); i++)
{
const SparseMatch & p = matches_vector[i];
Point pos(static_cast<int>(p.reference_image_pos.x), static_cast<int>(p.reference_image_pos.y));
labels.at<int>(pos) = i;
matDistanceMap.at<float>(pos) = costMap.at<float>(pos);
matDistanceMap.at<float>(pos) = workCostMap.at<float>(pos);
}

geodesicDistanceTransform(matDistanceMap, costMap);
geodesicDistanceTransform(matDistanceMap, workCostMap);

g.resize(match_num);
buildGraph(matDistanceMap, costMap);
g.assign(match_num, vector<node>());
buildGraph(matDistanceMap, workCostMap);
parallel_for_(Range(0, getNumThreads()), [&](const Range & range)
{
int stripe_sz = (int)ceil(match_num / (double)getNumThreads());
Expand Down Expand Up @@ -1312,7 +1315,6 @@ void RICInterpolatorImpl::interpolate(InputArray from_image, InputArray from_poi
fastGlobalSmootherFilter(prevGrey, dst, dst, fgs_lambda, fgs_sigma);
}
dst.copyTo(dense_flow.getMat());
costMap.release();
}

void RICInterpolatorImpl::geodesicDistanceTransform(Mat& distances, Mat& cost_map)
Expand Down
64 changes: 64 additions & 0 deletions modules/ximgproc/test/test_sparse_match_interpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,69 @@ TEST_P(InterpolatorTest, MultiThreadReproducibility)
}
INSTANTIATE_TEST_CASE_P(FullSet,InterpolatorTest, Combine(Values(szODD,szVGA), GuideTypes::all()));

// https://github.com/opencv/opencv_contrib/issues/3097
TEST(InterpolatorTest, RICReuseInstance)
{
RNG& rng = theRNG();
const Size size(160, 120);
const int matchCounts[] = { 600, 200, 400 }; // the second call has fewer matches than the first

// one thread, so that the results can be compared bit-exactly;
// the guard restores the count even if interpolate() throws
struct ThreadsGuard
{
const int origThreads;
ThreadsGuard() : origThreads(cv::getNumThreads()) { cv::setNumThreads(1); }
~ThreadsGuard() { cv::setNumThreads(origThreads); }
} threadsGuard;
Ptr<RICInterpolator> reused = createRICInterpolator();
reused->setUseVariationalRefinement(false);
for (size_t c = 0; c < sizeof(matchCounts) / sizeof(matchCounts[0]); ++c)
{
Mat from(size, CV_8UC3), to(size, CV_8UC3);
rng.fill(from, RNG::UNIFORM, 0, 256);
rng.fill(to, RNG::UNIFORM, 0, 256);
vector<Point2f> fromPoints(matchCounts[c]), toPoints(matchCounts[c]);
for (int i = 0; i < matchCounts[c]; ++i)
{
fromPoints[i] = Point2f(rng.uniform(0.f, size.width - 1.f), rng.uniform(0.f, size.height - 1.f));
toPoints[i] = fromPoints[i] + Point2f(rng.uniform(-4.f, 4.f), rng.uniform(-4.f, 4.f));
}

Mat flowReused, flowFresh;
reused->interpolate(from, fromPoints, to, toPoints, flowReused);
Ptr<RICInterpolator> fresh = createRICInterpolator();
fresh->setUseVariationalRefinement(false);
fresh->interpolate(from, fromPoints, to, toPoints, flowFresh);
EXPECT_EQ(0.0, cvtest::norm(flowFresh, flowReused, NORM_INF)) << "call " << c;
}

Mat customCost(size, CV_32FC1);
rng.fill(customCost, RNG::UNIFORM, 0.f, 255.f);
Mat customCostOrig = customCost.clone();
reused->setCostMap(customCost);
for (size_t c = 0; c < 2; ++c)
{
Mat from(size, CV_8UC3), to(size, CV_8UC3);
rng.fill(from, RNG::UNIFORM, 0, 256);
rng.fill(to, RNG::UNIFORM, 0, 256);
vector<Point2f> fromPoints(200), toPoints(200);
for (int i = 0; i < 200; ++i)
{
fromPoints[i] = Point2f(rng.uniform(0.f, size.width - 1.f), rng.uniform(0.f, size.height - 1.f));
toPoints[i] = fromPoints[i] + Point2f(rng.uniform(-4.f, 4.f), rng.uniform(-4.f, 4.f));
}

Mat flowReused, flowFresh;
reused->interpolate(from, fromPoints, to, toPoints, flowReused);
EXPECT_EQ(0.0, cvtest::norm(customCostOrig, customCost, NORM_INF));
Ptr<RICInterpolator> fresh = createRICInterpolator();
fresh->setUseVariationalRefinement(false);
fresh->setCostMap(customCostOrig);
fresh->interpolate(from, fromPoints, to, toPoints, flowFresh);
EXPECT_EQ(0.0, cvtest::norm(flowFresh, flowReused, NORM_INF)) << "customCost call " << c;
}
}


}} // namespace