From 43d61d961e355632b29a209ec9a1f30db7792477 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Fri, 8 Feb 2013 18:11:56 +0400 Subject: [PATCH] fixed problem with Nx2 numpy arrays in geometrical functions (#2783) --- modules/imgproc/src/utils.cpp | 4 ++++ modules/python/test/test2.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/modules/imgproc/src/utils.cpp b/modules/imgproc/src/utils.cpp index 736b7fdabb..72f4978e93 100644 --- a/modules/imgproc/src/utils.cpp +++ b/modules/imgproc/src/utils.cpp @@ -47,11 +47,15 @@ CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr, CV_Assert( arr != 0 && contour_header != 0 && block != 0 ); int eltype; + CvMat hdr; CvMat* mat = (CvMat*)arr; if( !CV_IS_MAT( mat )) CV_Error( CV_StsBadArg, "Input array is not a valid matrix" ); + if( CV_MAT_CN(mat->type) == 1 && mat->width == 2 ) + mat = cvReshape(mat, &hdr, 2); + eltype = CV_MAT_TYPE( mat->type ); if( eltype != CV_32SC2 && eltype != CV_32FC2 ) CV_Error( CV_StsUnsupportedFormat, diff --git a/modules/python/test/test2.py b/modules/python/test/test2.py index 6876d3915b..703d2ed742 100644 --- a/modules/python/test/test2.py +++ b/modules/python/test/test2.py @@ -82,8 +82,39 @@ class Hackathon244Tests(NewOpenCVTests): for kpt in keypoints: self.assertNotEqual(kpt.response, 0) + def check_close_angles(self, a, b, angle_delta): + self.assert_(abs(a - b) <= angle_delta or + abs(360 - abs(a - b)) <= angle_delta) + + def check_close_pairs(self, a, b, delta): + self.assertLessEqual(abs(a[0] - b[0]), delta) + self.assertLessEqual(abs(a[1] - b[1]), delta) + + def check_close_boxes(self, a, b, delta, angle_delta): + self.check_close_pairs(a[0], b[0], delta) + self.check_close_pairs(a[1], b[1], delta) + self.check_close_angles(a[2], b[2], angle_delta) + + def test_geometry(self): + npt = 100 + np.random.seed(244) + a = np.random.randn(npt,2).astype('float32')*50 + 150 + + img = np.zeros((300, 300, 3), dtype='uint8') + be = cv2.fitEllipse(a) + br = cv2.minAreaRect(a) + mc, mr = cv2.minEnclosingCircle(a) + + be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742) + br0 = ((161.2974090576172, 154.41793823242188), (199.2301483154297, 207.7177734375), -9.164555549621582) + mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977 + + self.check_close_boxes(be, be0, 5, 15) + self.check_close_boxes(br, br0, 5, 15) + self.check_close_pairs(mc, mc0, 5) + self.assertLessEqual(abs(mr - mr0), 5) if __name__ == '__main__': - print "testing", cv.__version__ + print "testing", cv2.__version__ random.seed(0) unittest.main()