Skip to content

Commit 4368f04

Browse files
committed
Merge pull request opencv#14271 from alalek:issue_14259
2 parents 2892557 + b5961cc commit 4368f04

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

modules/calib3d/src/ptsetreg.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,13 @@ Mat estimateAffine2D(InputArray _from, InputArray _to, OutputArray _inliers,
833833
to.convertTo(tmp2, CV_32FC2);
834834
to = tmp2;
835835
}
836+
else
837+
{
838+
// avoid changing of inputs in compressElems() call
839+
from = from.clone();
840+
to = to.clone();
841+
}
842+
836843
// convert to N x 1 vectors
837844
from = from.reshape(2, count);
838845
to = to.reshape(2, count);
@@ -900,6 +907,13 @@ Mat estimateAffinePartial2D(InputArray _from, InputArray _to, OutputArray _inlie
900907
to.convertTo(tmp2, CV_32FC2);
901908
to = tmp2;
902909
}
910+
else
911+
{
912+
// avoid changing of inputs in compressElems() call
913+
from = from.clone();
914+
to = to.clone();
915+
}
916+
903917
// convert to N x 1 vectors
904918
from = from.reshape(2, count);
905919
to = to.reshape(2, count);

modules/calib3d/test/test_affine2d_estimator.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,46 @@ TEST_P(EstimateAffine2D, testConversion)
152152

153153
INSTANTIATE_TEST_CASE_P(Calib3d, EstimateAffine2D, Method::all());
154154

155+
156+
// https://github.com/opencv/opencv/issues/14259
157+
TEST(EstimateAffine2D, issue_14259_dont_change_inputs)
158+
{
159+
/*const static*/ float pts0_[10] = {
160+
0.0f, 0.0f,
161+
0.0f, 8.0f,
162+
4.0f, 0.0f, // outlier
163+
8.0f, 8.0f,
164+
8.0f, 0.0f
165+
};
166+
/*const static*/ float pts1_[10] = {
167+
0.1f, 0.1f,
168+
0.1f, 8.1f,
169+
0.0f, 4.0f, // outlier
170+
8.1f, 8.1f,
171+
8.1f, 0.1f
172+
};
173+
174+
Mat pts0(Size(1, 5), CV_32FC2, (void*)pts0_);
175+
Mat pts1(Size(1, 5), CV_32FC2, (void*)pts1_);
176+
177+
Mat pts0_copy = pts0.clone();
178+
Mat pts1_copy = pts1.clone();
179+
180+
Mat inliers;
181+
182+
cv::Mat A = cv::estimateAffine2D(pts0, pts1, inliers);
183+
184+
for(int i = 0; i < pts0.rows; ++i)
185+
{
186+
EXPECT_EQ(pts0_copy.at<Vec2f>(i), pts0.at<Vec2f>(i)) << "pts0: i=" << i;
187+
}
188+
189+
for(int i = 0; i < pts1.rows; ++i)
190+
{
191+
EXPECT_EQ(pts1_copy.at<Vec2f>(i), pts1.at<Vec2f>(i)) << "pts1: i=" << i;
192+
}
193+
194+
EXPECT_EQ(0, (int)inliers.at<uchar>(2));
195+
}
196+
155197
}} // namespace

modules/calib3d/test/test_affine_partial2d_estimator.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,46 @@ TEST_P(EstimateAffinePartial2D, testConversion)
161161

162162
INSTANTIATE_TEST_CASE_P(Calib3d, EstimateAffinePartial2D, Method::all());
163163

164+
165+
// https://github.com/opencv/opencv/issues/14259
166+
TEST(EstimateAffinePartial2D, issue_14259_dont_change_inputs)
167+
{
168+
/*const static*/ float pts0_[10] = {
169+
0.0f, 0.0f,
170+
0.0f, 8.0f,
171+
4.0f, 0.0f, // outlier
172+
8.0f, 8.0f,
173+
8.0f, 0.0f
174+
};
175+
/*const static*/ float pts1_[10] = {
176+
0.1f, 0.1f,
177+
0.1f, 8.1f,
178+
0.0f, 4.0f, // outlier
179+
8.1f, 8.1f,
180+
8.1f, 0.1f
181+
};
182+
183+
Mat pts0(Size(1, 5), CV_32FC2, (void*)pts0_);
184+
Mat pts1(Size(1, 5), CV_32FC2, (void*)pts1_);
185+
186+
Mat pts0_copy = pts0.clone();
187+
Mat pts1_copy = pts1.clone();
188+
189+
Mat inliers;
190+
191+
cv::Mat A = cv::estimateAffinePartial2D(pts0, pts1, inliers);
192+
193+
for(int i = 0; i < pts0.rows; ++i)
194+
{
195+
EXPECT_EQ(pts0_copy.at<Vec2f>(i), pts0.at<Vec2f>(i)) << "pts0: i=" << i;
196+
}
197+
198+
for(int i = 0; i < pts1.rows; ++i)
199+
{
200+
EXPECT_EQ(pts1_copy.at<Vec2f>(i), pts1.at<Vec2f>(i)) << "pts1: i=" << i;
201+
}
202+
203+
EXPECT_EQ(0, (int)inliers.at<uchar>(2));
204+
}
205+
164206
}} // namespace

0 commit comments

Comments
 (0)