Skip to content

Commit c61fbc7

Browse files
committed
Merge pull request #2832 from tsenst:Resolve_Issue_2796_RICInterpolation
2 parents 461b56f + c56f818 commit c61fbc7

File tree

4 files changed

+130
-18
lines changed

4 files changed

+130
-18
lines changed

modules/ximgproc/include/opencv2/ximgproc/sparse_match_interpolator.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class CV_EXPORTS_W SparseMatchInterpolator : public Algorithm
5757
@param from_image first of the two matched images, 8-bit single-channel or three-channel.
5858
5959
@param from_points points of the from_image for which there are correspondences in the
60-
to_image (Point2f vector, size shouldn't exceed 32767)
60+
to_image (Point2f vector or Mat of depth CV_32F)
6161
6262
@param to_image second of the two matched images, 8-bit single-channel or three-channel.
6363
6464
@param to_points points in the to_image corresponding to from_points
65-
(Point2f vector, size shouldn't exceed 32767)
65+
(Point2f vector or Mat of depth CV_32F)
6666
6767
@param dense_flow output dense matching (two-channel CV_32F image)
6868
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
import numpy as np
3+
import cv2 as cv
4+
5+
from tests_common import NewOpenCVTests
6+
7+
class Interpolator_test(NewOpenCVTests):
8+
def test_edgeaware_interpolator(self):
9+
# readGT
10+
MAX_DIF = 1.0
11+
MAX_MEAN_DIF = 1.0 / 256.0
12+
13+
src = cv.imread(self.find_file("cv/optflow/RubberWhale1.png"), cv.IMREAD_COLOR)
14+
self.assertFalse(src is None)
15+
16+
ref_flow = cv.readOpticalFlow(self.find_file("cv/sparse_match_interpolator/RubberWhale_reference_result.flo"))
17+
self.assertFalse(ref_flow is None)
18+
19+
matches = np.genfromtxt(self.find_file("cv/sparse_match_interpolator/RubberWhale_sparse_matches.txt")).astype(np.float32)
20+
from_points = matches[:,0:2]
21+
to_points = matches[:,2:4]
22+
interpolator = cv.ximgproc.createEdgeAwareInterpolator()
23+
interpolator.setK(128)
24+
interpolator.setSigma(0.05)
25+
interpolator.setUsePostProcessing(True)
26+
interpolator.setFGSLambda(500.0)
27+
interpolator.setFGSSigma(1.5)
28+
29+
dense_flow = interpolator.interpolate(src, from_points, src, to_points)
30+
31+
self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_INF) <= MAX_DIF)
32+
self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_L1) <= (MAX_MEAN_DIF * dense_flow.shape[0] * dense_flow.shape[1]))
33+
34+
def test_ric_interpolator(self):
35+
# readGT
36+
MAX_DIF = 6.0
37+
MAX_MEAN_DIF = 60.0 / 256.0
38+
39+
src0 = cv.imread(self.find_file("cv/optflow/RubberWhale1.png"), cv.IMREAD_COLOR)
40+
self.assertFalse(src0 is None)
41+
42+
src1 = cv.imread(self.find_file("cv/optflow/RubberWhale2.png"), cv.IMREAD_COLOR)
43+
self.assertFalse(src1 is None)
44+
45+
ref_flow = cv.readOpticalFlow(self.find_file("cv/sparse_match_interpolator/RubberWhale_reference_result.flo"))
46+
self.assertFalse(ref_flow is None)
47+
48+
matches = np.genfromtxt(self.find_file("cv/sparse_match_interpolator/RubberWhale_sparse_matches.txt")).astype(np.float32)
49+
from_points = matches[:,0:2]
50+
to_points = matches[:,2:4]
51+
52+
interpolator = cv.ximgproc.createRICInterpolator()
53+
interpolator.setK(32)
54+
interpolator.setSuperpixelSize(15)
55+
interpolator.setSuperpixelNNCnt(150)
56+
interpolator.setSuperpixelRuler(15.0)
57+
interpolator.setSuperpixelMode(cv.ximgproc.SLIC)
58+
interpolator.setAlpha(0.7)
59+
interpolator.setModelIter(4)
60+
interpolator.setRefineModels(True)
61+
interpolator.setMaxFlow(250)
62+
interpolator.setUseVariationalRefinement(True)
63+
interpolator.setUseGlobalSmootherFilter(True)
64+
interpolator.setFGSLambda(500.0)
65+
interpolator.setFGSSigma(1.5)
66+
dense_flow = interpolator.interpolate(src0, from_points, src1, to_points)
67+
self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_INF) <= MAX_DIF)
68+
self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_L1) <= (MAX_MEAN_DIF * dense_flow.shape[0] * dense_flow.shape[1]))
69+
70+
if __name__ == '__main__':
71+
NewOpenCVTests.bootstrap()

modules/ximgproc/src/sparse_match_interpolators.cpp

+42-16
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,33 @@ Ptr<EdgeAwareInterpolatorImpl> EdgeAwareInterpolatorImpl::create()
173173
void EdgeAwareInterpolatorImpl::interpolate(InputArray from_image, InputArray from_points, InputArray, InputArray to_points, OutputArray dense_flow)
174174
{
175175
CV_Assert( !from_image.empty() && (from_image.depth() == CV_8U) && (from_image.channels() == 3 || from_image.channels() == 1) );
176-
CV_Assert( !from_points.empty() && from_points.isVector() &&
177-
!to_points .empty() && to_points .isVector() &&
178-
from_points.sameSize(to_points) );
176+
CV_Assert( !from_points.empty() && !to_points.empty() && from_points.sameSize(to_points) );
177+
CV_Assert((from_points.isVector() || from_points.isMat()) && from_points.depth() == CV_32F);
178+
CV_Assert((to_points.isVector() || to_points.isMat()) && to_points.depth() == CV_32F);
179+
CV_Assert(from_points.sameSize(to_points));
179180

180181
w = from_image.cols();
181182
h = from_image.rows();
182183

183-
vector<Point2f> from_vector = *(const vector<Point2f>*)from_points.getObj();
184-
vector<Point2f> to_vector = *(const vector<Point2f>*)to_points .getObj();
185-
vector<SparseMatch> matches_vector(from_vector.size());
186-
for(unsigned int i=0;i<from_vector.size();i++)
187-
matches_vector[i] = SparseMatch(from_vector[i],to_vector[i]);
184+
Mat from_mat = from_points.getMat();
185+
Mat to_mat = to_points.getMat();
186+
int npoints = from_mat.checkVector(2, CV_32F, false);
187+
if (from_mat.channels() != 2)
188+
from_mat = from_mat.reshape(2, npoints);
189+
190+
if (to_mat.channels() != 2){
191+
to_mat = to_mat.reshape(2, npoints);
192+
npoints = from_mat.checkVector(2, CV_32F, false);
193+
}
194+
195+
196+
vector<SparseMatch> matches_vector(npoints);
197+
for(unsigned int i=0;i<matches_vector.size();i++)
198+
matches_vector[i] = SparseMatch(from_mat.at<Point2f>(i), to_mat.at<Point2f>(i));
199+
200+
188201
sort(matches_vector.begin(),matches_vector.end());
202+
189203
match_num = (int)matches_vector.size();
190204
CV_Assert(match_num<SHRT_MAX);
191205

@@ -1102,17 +1116,29 @@ void RICInterpolatorImpl::interpolate(InputArray from_image, InputArray from_poi
11021116
CV_Assert(use_variational_refinement == false || !to_image.empty());
11031117
CV_Assert(use_variational_refinement == false || to_image.depth() == CV_8U);
11041118
CV_Assert(use_variational_refinement == false || to_image.channels() == 3 || to_image.channels() == 1);
1105-
CV_Assert(!from_points.empty() && from_points.isVector());
1106-
CV_Assert(!to_points.empty() && to_points.isVector());
1119+
CV_Assert(!from_points.empty());
1120+
CV_Assert(!to_points.empty());
1121+
CV_Assert((from_points.isVector() || from_points.isMat()) && from_points.depth() == CV_32F);
1122+
CV_Assert((to_points.isVector() || to_points.isMat()) && to_points.depth() == CV_32F);
11071123
CV_Assert(from_points.sameSize(to_points));
11081124

1109-
vector<Point2f> from_vector = *(const vector<Point2f>*)from_points.getObj();
1110-
vector<Point2f> to_vector = *(const vector<Point2f>*)to_points.getObj();
1111-
vector<SparseMatch> matches_vector(from_vector.size());
1112-
for (unsigned int i = 0; i < from_vector.size(); i++)
1113-
matches_vector[i] = SparseMatch(from_vector[i], to_vector[i]);
1125+
Mat from_mat = from_points.getMat();
1126+
Mat to_mat = to_points.getMat();
1127+
int npoints = from_mat.checkVector(2, CV_32F, false);
1128+
1129+
if (from_mat.channels() != 2)
1130+
from_mat = from_mat.reshape(2, npoints);
1131+
1132+
if (to_mat.channels() != 2 ){
1133+
to_mat = to_mat.reshape(2, npoints);
1134+
npoints = from_mat.checkVector(2, CV_32F, false);
1135+
}
1136+
1137+
vector<SparseMatch> matches_vector(npoints);
1138+
for(unsigned int i=0;i<matches_vector.size();i++)
1139+
matches_vector[i] = SparseMatch(from_mat.at<Point2f>(i),to_mat.at<Point2f>(i));
11141140

1115-
match_num = static_cast<int>(from_vector.size());
1141+
match_num = static_cast<int>(matches_vector.size());
11161142

11171143
Mat src = from_image.getMat();
11181144
Size src_size = src.size();

modules/ximgproc/test/test_sparse_match_interpolator.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ TEST(InterpolatorTest, ReferenceAccuracy)
9393

9494
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
9595
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
96+
97+
Mat from_point_mat(from_points);
98+
Mat to_points_mat(to_points);
99+
interpolator->interpolate(src,from_point_mat,Mat(),to_points_mat,res_flow);
100+
101+
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
102+
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
103+
96104
}
97105

98106
TEST(InterpolatorTest, RICReferenceAccuracy)
@@ -141,6 +149,13 @@ TEST(InterpolatorTest, RICReferenceAccuracy)
141149

142150
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
143151
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1), MAX_MEAN_DIF*res_flow.total());
152+
153+
Mat from_point_mat(from_points);
154+
Mat to_points_mat(to_points);
155+
interpolator->interpolate(src, from_point_mat, src1, to_points_mat, res_flow);
156+
157+
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
158+
EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
144159
}
145160

146161
TEST_P(InterpolatorTest, MultiThreadReproducibility)

0 commit comments

Comments
 (0)