Skip to content

Commit a6e5eba

Browse files
committed
calib3d: fix cornerSubPix memory error
1 parent e5175db commit a6e5eba

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

modules/calib3d/test/test_cornerssubpix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,18 @@ void CV_ChessboardSubpixelTest::generateIntrinsicParams()
239239

240240
TEST(Calib3d_ChessboardSubPixDetector, accuracy) { CV_ChessboardSubpixelTest test; test.safe_run(); }
241241

242+
TEST(Calib3d_CornerSubPix, regression_7204)
243+
{
244+
cv::Mat image(cv::Size(70, 38), CV_8UC1, cv::Scalar::all(0));
245+
image(cv::Rect(65, 26, 5, 5)).setTo(cv::Scalar::all(255));
246+
image(cv::Rect(55, 31, 8, 1)).setTo(cv::Scalar::all(255));
247+
image(cv::Rect(56, 35, 14, 2)).setTo(cv::Scalar::all(255));
248+
image(cv::Rect(66, 24, 4, 2)).setTo(cv::Scalar::all(255));
249+
image.at<uchar>(24, 69) = 0;
250+
std::vector<cv::Point2f> corners;
251+
corners.push_back(cv::Point2f(65, 30));
252+
cv::cornerSubPix(image, corners, cv::Size(3, 3), cv::Size(-1, -1),
253+
cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
254+
}
255+
242256
/* End of file. */

modules/imgproc/src/cornersubpix.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ cvFindCornerSubPix( const void* srcarr, CvPoint2D32f* corners,
234234

235235
err = (cI2.x - cI.x) * (cI2.x - cI.x) + (cI2.y - cI.y) * (cI2.y - cI.y);
236236
cI = cI2;
237+
238+
/* if new point is too far from initial, it means poor convergence. */
239+
if (fabs(cI.x - cT.x) > win.width || fabs(cI.y - cT.y) > win.height)
240+
{
241+
cI = cT;
242+
break;
243+
}
244+
cI.x = std::max(0.0f, std::min((float)size.width, cI.x));
245+
cI.y = std::max(0.0f, std::min((float)size.height, cI.y));
237246
}
238247
while( ++iter < max_iters && err > eps );
239248

modules/imgproc/src/samplers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ CvStatus CV_STDCALL icvGetRectSubPix_8u32f_C1R
392392
ip.x = cvFloor( center.x );
393393
ip.y = cvFloor( center.y );
394394

395+
CV_DbgAssert(fabs(center.x - ip.x) <= 1.0f && fabs(center.y - ip.y) <= 1.0f);
396+
395397
if( win_size.width <= 0 || win_size.height <= 0 )
396398
return CV_BADRANGE_ERR;
397399

0 commit comments

Comments
 (0)