From 2dc981aaa8a105405ecb77ab8522a284bb23d166 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Wed, 18 May 2011 15:16:12 +0000 Subject: [PATCH] fixed bug with possible memory corruption in CvMat m = iarray.getMat(); expressions (ticket #1054) --- modules/calib3d/src/calibinit.cpp | 4 +-- modules/calib3d/src/calibration.cpp | 39 +++++++++++++++++----------- modules/calib3d/src/fundam.cpp | 4 +-- modules/calib3d/src/solvepnp.cpp | 3 ++- modules/highgui/src/window.cpp | 5 ++-- modules/imgproc/src/cornersubpix.cpp | 3 ++- modules/imgproc/src/hough.cpp | 9 ++++--- modules/imgproc/src/inpaint.cpp | 4 +-- modules/imgproc/src/moments.cpp | 3 ++- modules/imgproc/src/segmentation.cpp | 5 ++-- modules/video/src/camshift.cpp | 8 +++--- modules/video/src/lkpyramid.cpp | 8 +++--- modules/video/src/motempl.cpp | 6 +++-- 13 files changed, 61 insertions(+), 40 deletions(-) diff --git a/modules/calib3d/src/calibinit.cpp b/modules/calib3d/src/calibinit.cpp index 31a0231e99..3093f5342d 100644 --- a/modules/calib3d/src/calibinit.cpp +++ b/modules/calib3d/src/calibinit.cpp @@ -1899,7 +1899,7 @@ bool cv::findChessboardCorners( const InputArray& _image, Size patternSize, { int count = patternSize.area()*2; vector tmpcorners(count+1); - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); CvMat c_image = image; bool ok = cvFindChessboardCorners(&c_image, patternSize, (CvPoint2D32f*)&tmpcorners[0], &count, flags ) > 0; if( count > 0 ) @@ -1919,7 +1919,7 @@ void cv::drawChessboardCorners( InputOutputArray _image, Size patternSize, Mat corners = _corners.getMat(); if( corners.empty() ) return; - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); CvMat c_image = _image.getMat(); int nelems = corners.checkVector(2, CV_32F, true); CV_Assert(nelems >= 0); cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data, diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index eda40ac45d..a91a85ca8f 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -3294,11 +3294,14 @@ void cv::projectPoints( const InputArray& _opoints, CvMat *pdpdrot=0, *pdpdt=0, *pdpdf=0, *pdpdc=0, *pdpddist=0; _ipoints.create(npoints, 1, CV_MAKETYPE(depth, 2), -1, true); - CvMat imagePoints = _ipoints.getMat(); - CvMat objectPoints = opoints; - CvMat cameraMatrix = _cameraMatrix.getMat(); - CvMat rvec = _rvec.getMat(), tvec = _tvec.getMat(); - CvMat distCoeffs = _distCoeffs.getMat(); + CvMat c_imagePoints = _ipoints.getMat(); + CvMat c_objectPoints = opoints; + Mat cameraMatrix = _cameraMatrix.getMat(); + Mat distCoeffs = _distCoeffs.getMat(); + Mat rvec = _rvec.getMat(), tvec = _tvec.getMat(); + CvMat c_cameraMatrix = cameraMatrix; + CvMat c_rvec = rvec, c_tvec = tvec; + CvMat c_distCoeffs = distCoeffs; int ndistCoeffs = distCoeffs.rows + distCoeffs.cols - 1; if( _jacobian.needed() ) @@ -3312,8 +3315,8 @@ void cv::projectPoints( const InputArray& _opoints, pdpddist = &(dpddist = jacobian.colRange(10, 10+ndistCoeffs)); } - cvProjectPoints2( &objectPoints, &rvec, &tvec, &cameraMatrix, &distCoeffs, - &imagePoints, pdpdrot, pdpdt, pdpdf, pdpdc, pdpddist, aspectRatio ); + cvProjectPoints2( &c_objectPoints, &c_rvec, &c_tvec, &c_cameraMatrix, &c_distCoeffs, + &c_imagePoints, pdpdrot, pdpdt, pdpdf, pdpdc, pdpddist, aspectRatio ); } cv::Mat cv::initCameraMatrix2D( const InputArrayOfArrays& objectPoints, @@ -3378,7 +3381,8 @@ void cv::calibrationMatrixValues( const InputArray& _cameraMatrix, Size imageSiz double& fovx, double& fovy, double& focalLength, Point2d& principalPoint, double& aspectRatio ) { - CvMat c_cameraMatrix = _cameraMatrix.getMat(); + Mat cameraMatrix = _cameraMatrix.getMat(); + CvMat c_cameraMatrix = cameraMatrix; cvCalibrationMatrixValues( &c_cameraMatrix, imageSize, apertureWidth, apertureHeight, &fovx, &fovy, &focalLength, (CvPoint2D64f*)&principalPoint, &aspectRatio ); } @@ -3453,11 +3457,14 @@ void cv::stereoRectify( const InputArray& _cameraMatrix1, const InputArray& _dis double alpha, Size newImageSize, Rect* validPixROI1, Rect* validPixROI2 ) { - CvMat c_cameraMatrix1 = _cameraMatrix1.getMat(); - CvMat c_cameraMatrix2 = _cameraMatrix2.getMat(); - CvMat c_distCoeffs1 = _distCoeffs1.getMat(); - CvMat c_distCoeffs2 = _distCoeffs2.getMat(); - CvMat c_R = _Rmat.getMat(), c_T = _Tmat.getMat(); + Mat cameraMatrix1 = _cameraMatrix1.getMat(), cameraMatrix2 = _cameraMatrix2.getMat(); + Mat distCoeffs1 = _distCoeffs1.getMat(), distCoeffs2 = _distCoeffs2.getMat(); + Mat Rmat = _Rmat.getMat(), Tmat = _Tmat.getMat(); + CvMat c_cameraMatrix1 = cameraMatrix1; + CvMat c_cameraMatrix2 = cameraMatrix2; + CvMat c_distCoeffs1 = distCoeffs1; + CvMat c_distCoeffs2 = distCoeffs2; + CvMat c_R = Rmat, c_T = Tmat; int rtype = CV_64F; _Rmat1.create(3, 3, rtype); @@ -3486,7 +3493,8 @@ bool cv::stereoRectifyUncalibrated( const InputArray& _points1, const InputArray _Hmat1.create(3, 3, rtype); _Hmat2.create(3, 3, rtype); Mat F = _Fmat.getMat(); - CvMat c_pt1 = _points1.getMat(), c_pt2 = _points2.getMat(); + Mat points1 = _points1.getMat(), points2 = _points2.getMat(); + CvMat c_pt1 = points1, c_pt2 = points2; CvMat c_F, *p_F=0, c_H1 = _Hmat1.getMat(), c_H2 = _Hmat2.getMat(); if( F.size() == Size(3, 3) ) p_F = &(c_F = F); @@ -3498,7 +3506,8 @@ cv::Mat cv::getOptimalNewCameraMatrix( const InputArray& _cameraMatrix, Size imgSize, double alpha, Size newImgSize, Rect* validPixROI ) { - CvMat c_cameraMatrix = _cameraMatrix.getMat(), c_distCoeffs = _distCoeffs.getMat(); + Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat(); + CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs; Mat newCameraMatrix(3, 3, CV_MAT_TYPE(c_cameraMatrix.type)); CvMat c_newCameraMatrix = newCameraMatrix; diff --git a/modules/calib3d/src/fundam.cpp b/modules/calib3d/src/fundam.cpp index bb8ab92827..7d1ff4724b 100644 --- a/modules/calib3d/src/fundam.cpp +++ b/modules/calib3d/src/fundam.cpp @@ -1106,12 +1106,12 @@ cv::Mat cv::findFundamentalMat( const InputArray& _points1, const InputArray& _p void cv::computeCorrespondEpilines( const InputArray& _points, int whichImage, const InputArray& _Fmat, OutputArray _lines ) { - Mat points = _points.getMat(); + Mat points = _points.getMat(), F = _Fmat.getMat(); int npoints = points.checkVector(2); CV_Assert( npoints >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S)); _lines.create(npoints, 1, CV_32FC3, -1, true); - CvMat c_points = points, c_lines = _lines.getMat(), c_F = _Fmat.getMat(); + CvMat c_points = points, c_lines = _lines.getMat(), c_F = F; cvComputeCorrespondEpilines(&c_points, whichImage, &c_F, &c_lines); } diff --git a/modules/calib3d/src/solvepnp.cpp b/modules/calib3d/src/solvepnp.cpp index b0ef1ee46a..a17a6c1578 100644 --- a/modules/calib3d/src/solvepnp.cpp +++ b/modules/calib3d/src/solvepnp.cpp @@ -53,8 +53,9 @@ void cv::solvePnP( const InputArray& _opoints, const InputArray& _ipoints, _rvec.create(3, 1, CV_64F); _tvec.create(3, 1, CV_64F); + Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat(); CvMat c_objectPoints = opoints, c_imagePoints = ipoints; - CvMat c_cameraMatrix = _cameraMatrix.getMat(), c_distCoeffs = _distCoeffs.getMat(); + CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs; CvMat c_rvec = _rvec.getMat(), c_tvec = _tvec.getMat(); cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix, c_distCoeffs.rows*c_distCoeffs.cols ? &c_distCoeffs : 0, diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index 0fbca111c0..729c83ca97 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -146,9 +146,10 @@ double cv::getWindowProperty(const string& winname, int prop_id) return cvGetWindowProperty(winname.c_str(),prop_id); } -void cv::imshow( const string& winname, const InputArray& img ) +void cv::imshow( const string& winname, const InputArray& _img ) { - CvMat c_img = img.getMat(); + Mat img = _img.getMat(); + CvMat c_img = img; cvShowImage( winname.c_str(), &c_img ); } diff --git a/modules/imgproc/src/cornersubpix.cpp b/modules/imgproc/src/cornersubpix.cpp index 6d32608244..22139a4220 100644 --- a/modules/imgproc/src/cornersubpix.cpp +++ b/modules/imgproc/src/cornersubpix.cpp @@ -261,7 +261,8 @@ void cv::cornerSubPix( const InputArray& _image, InputOutputArray _corners, Mat corners = _corners.getMat(); int ncorners = corners.checkVector(2); CV_Assert( ncorners >= 0 && corners.depth() == CV_32F ); - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); + CvMat c_image = image; cvFindCornerSubPix( &c_image, (CvPoint2D32f*)corners.data, ncorners, winSize, zeroZone, criteria ); diff --git a/modules/imgproc/src/hough.cpp b/modules/imgproc/src/hough.cpp index b1a96715e8..dadd5d100e 100644 --- a/modules/imgproc/src/hough.cpp +++ b/modules/imgproc/src/hough.cpp @@ -1109,7 +1109,8 @@ void cv::HoughLines( const InputArray& _image, OutputArray _lines, double srn, double stn ) { Ptr storage = cvCreateMemStorage(STORAGE_SIZE); - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); + CvMat c_image = image; CvSeq* seq = cvHoughLines2( &c_image, storage, srn == 0 && stn == 0 ? CV_HOUGH_STANDARD : CV_HOUGH_MULTI_SCALE, rho, theta, threshold, srn, stn ); @@ -1121,7 +1122,8 @@ void cv::HoughLinesP( const InputArray& _image, OutputArray _lines, double minLineLength, double maxGap ) { Ptr storage = cvCreateMemStorage(STORAGE_SIZE); - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); + CvMat c_image = image; CvSeq* seq = cvHoughLines2( &c_image, storage, CV_HOUGH_PROBABILISTIC, rho, theta, threshold, minLineLength, maxGap ); seqToMat(seq, _lines); @@ -1133,7 +1135,8 @@ void cv::HoughCircles( const InputArray& _image, OutputArray _circles, int minRadius, int maxRadius ) { Ptr storage = cvCreateMemStorage(STORAGE_SIZE); - CvMat c_image = _image.getMat(); + Mat image = _image.getMat(); + CvMat c_image = image; CvSeq* seq = cvHoughCircles( &c_image, storage, method, dp, min_dist, param1, param2, minRadius, maxRadius ); seqToMat(seq, _circles); diff --git a/modules/imgproc/src/inpaint.cpp b/modules/imgproc/src/inpaint.cpp index 0fb66c257c..06d87495ae 100644 --- a/modules/imgproc/src/inpaint.cpp +++ b/modules/imgproc/src/inpaint.cpp @@ -810,8 +810,8 @@ cvInpaint( const CvArr* _input_img, const CvArr* _inpaint_mask, CvArr* _output_i void cv::inpaint( const InputArray& _src, const InputArray& _mask, OutputArray _dst, double inpaintRange, int flags ) { - Mat src = _src.getMat(); + Mat src = _src.getMat(), mask = _mask.getMat(); _dst.create( src.size(), src.type() ); - CvMat c_src = src, c_mask = _mask.getMat(), c_dst = _dst.getMat(); + CvMat c_src = src, c_mask = mask, c_dst = _dst.getMat(); cvInpaint( &c_src, &c_mask, &c_dst, inpaintRange, flags ); } diff --git a/modules/imgproc/src/moments.cpp b/modules/imgproc/src/moments.cpp index 3e6b4d35c2..f517412c14 100644 --- a/modules/imgproc/src/moments.cpp +++ b/modules/imgproc/src/moments.cpp @@ -607,7 +607,8 @@ Moments::operator CvMoments() const cv::Moments cv::moments( const InputArray& _array, bool binaryImage ) { CvMoments om; - CvMat c_array = _array.getMat(); + Mat arr = _array.getMat(); + CvMat c_array = arr; cvMoments(&c_array, &om, binaryImage); return om; } diff --git a/modules/imgproc/src/segmentation.cpp b/modules/imgproc/src/segmentation.cpp index f7bf82272a..2c48972614 100644 --- a/modules/imgproc/src/segmentation.cpp +++ b/modules/imgproc/src/segmentation.cpp @@ -303,9 +303,10 @@ cvWatershed( const CvArr* srcarr, CvArr* dstarr ) } -void cv::watershed( const InputArray& src, InputOutputArray markers ) +void cv::watershed( const InputArray& _src, InputOutputArray markers ) { - CvMat c_src = src.getMat(), c_markers = markers.getMat(); + Mat src = _src.getMat(); + CvMat c_src = _src.getMat(), c_markers = markers.getMat(); cvWatershed( &c_src, &c_markers ); } diff --git a/modules/video/src/camshift.cpp b/modules/video/src/camshift.cpp index 0472ad87d2..f8a6faee7b 100644 --- a/modules/video/src/camshift.cpp +++ b/modules/video/src/camshift.cpp @@ -291,11 +291,12 @@ cvCamShift( const void* imgProb, CvRect windowIn, cv::RotatedRect cv::CamShift( const InputArray& _probImage, Rect& window, - TermCriteria criteria ) + TermCriteria criteria ) { CvConnectedComp comp; CvBox2D box; - CvMat c_probImage = _probImage.getMat(); + Mat probImage = _probImage.getMat(); + CvMat c_probImage = probImage; cvCamShift(&c_probImage, window, (CvTermCriteria)criteria, &comp, &box); window = comp.rect; return RotatedRect(Point2f(box.center), Size2f(box.size), box.angle); @@ -304,7 +305,8 @@ cv::RotatedRect cv::CamShift( const InputArray& _probImage, Rect& window, int cv::meanShift( const InputArray& _probImage, Rect& window, TermCriteria criteria ) { CvConnectedComp comp; - CvMat c_probImage = _probImage.getMat(); + Mat probImage = _probImage.getMat(); + CvMat c_probImage = probImage; int iters = cvMeanShift(&c_probImage, window, (CvTermCriteria)criteria, &comp ); window = comp.rect; return iters; diff --git a/modules/video/src/lkpyramid.cpp b/modules/video/src/lkpyramid.cpp index bc3f993584..3d5d9a4438 100644 --- a/modules/video/src/lkpyramid.cpp +++ b/modules/video/src/lkpyramid.cpp @@ -1865,12 +1865,12 @@ cvEstimateRigidTransform( const CvArr* matA, const CvArr* matB, CvMat* matM, int return 1; } -cv::Mat cv::estimateRigidTransform( const InputArray& A, - const InputArray& B, +cv::Mat cv::estimateRigidTransform( const InputArray& src1, + const InputArray& src2, bool fullAffine ) { - Mat M(2, 3, CV_64F); - CvMat matA = A.getMat(), matB = B.getMat(), matM = M; + Mat M(2, 3, CV_64F), A = src1.getMat(), B = src2.getMat(); + CvMat matA = A, matB = B, matM = M; cvEstimateRigidTransform(&matA, &matB, &matM, fullAffine); return M; } diff --git a/modules/video/src/motempl.cpp b/modules/video/src/motempl.cpp index 44eaa9dc4a..78c0c71bd7 100644 --- a/modules/video/src/motempl.cpp +++ b/modules/video/src/motempl.cpp @@ -445,7 +445,8 @@ cvSegmentMotion( const CvArr* mhiimg, CvArr* segmask, CvMemStorage* storage, void cv::updateMotionHistory( const InputArray& _silhouette, InputOutputArray _mhi, double timestamp, double duration ) { - CvMat c_silhouette = _silhouette.getMat(), c_mhi = _mhi.getMat(); + Mat silhouette = _silhouette.getMat(); + CvMat c_silhouette = silhouette, c_mhi = _mhi.getMat(); cvUpdateMotionHistory( &c_silhouette, &c_mhi, timestamp, duration ); } @@ -465,7 +466,8 @@ double cv::calcGlobalOrientation( const InputArray& _orientation, const InputArr const InputArray& _mhi, double timestamp, double duration ) { - CvMat c_orientation = _orientation.getMat(), c_mask = _mask.getMat(), c_mhi = _mhi.getMat(); + Mat orientation = _orientation.getMat(), mask = _mask.getMat(), mhi = _mhi.getMat(); + CvMat c_orientation = orientation, c_mask = mask, c_mhi = mhi; return cvCalcGlobalOrientation(&c_orientation, &c_mask, &c_mhi, timestamp, duration); }