From 30f1ab059b2c12ff74ef38cf878af6ade8fe22e7 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Wed, 25 Apr 2012 08:39:21 +0000 Subject: [PATCH] make Mat::at<>(i) work with 2d matrices (to retrieve all elements in row-major order) (reported by Kurt) and fixed bug #1804 --- modules/core/include/opencv2/core/mat.hpp | 22 +++++++--- modules/core/test/test_operations.cpp | 50 +++++++++++++++++++++-- modules/python/src2/cv2.cpp | 2 +- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 0b72afff3..d9ab8d777 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -561,18 +561,28 @@ template inline const _Tp& Mat::at(Point pt) const template inline _Tp& Mat::at(int i0) { - CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && - (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && + CV_DbgAssert( dims <= 2 && data && + (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) && elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); - return *(_Tp*)(data + step.p[size.p[0]==1]*i0); + if( isContinuous() || size.p[0] == 1 ) + return ((_Tp*)data)[i0]; + if( size.p[1] == 1 ) + return *(_Tp*)(data + step.p[0]*i0); + int i = i0/cols, j = i0 - i*cols; + return ((_Tp*)(data + step.p[0]*i))[j]; } template inline const _Tp& Mat::at(int i0) const { - CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && - (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && + CV_DbgAssert( dims <= 2 && data && + (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) && elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); - return *(_Tp*)(data + step.p[size.p[0]==1]*i0); + if( isContinuous() || size.p[0] == 1 ) + return ((const _Tp*)data)[i0]; + if( size.p[1] == 1 ) + return *(const _Tp*)(data + step.p[0]*i0); + int i = i0/cols, j = i0 - i*cols; + return ((const _Tp*)(data + step.p[0]*i))[j]; } template inline _Tp& Mat::at(int i0, int i1, int i2) diff --git a/modules/core/test/test_operations.cpp b/modules/core/test/test_operations.cpp index 1b645b515..0675c1f23 100644 --- a/modules/core/test/test_operations.cpp +++ b/modules/core/test/test_operations.cpp @@ -74,11 +74,17 @@ protected: bool TestSparseMat(); bool TestVec(); bool TestMatxMultiplication(); + bool TestSubMatAccess(); bool operations1(); - void checkDiff(const Mat& m1, const Mat& m2, const string& s) { if (norm(m1, m2, NORM_INF) != 0) throw test_excep(s); } - void checkDiffF(const Mat& m1, const Mat& m2, const string& s) { if (norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s); } - + void checkDiff(const Mat& m1, const Mat& m2, const string& s) + { + if (norm(m1, m2, NORM_INF) != 0) throw test_excep(s); + } + void checkDiffF(const Mat& m1, const Mat& m2, const string& s) + { + if (norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s); + } }; CV_OperationsTest::CV_OperationsTest() @@ -438,6 +444,41 @@ bool CV_OperationsTest::SomeMatFunctions() } +bool CV_OperationsTest::TestSubMatAccess() +{ + try + { + Mat_ T_bs(4,4); + Vec3f cdir(1.f, 1.f, 0.f); + Vec3f ydir(1.f, 0.f, 1.f); + Vec3f fpt(0.1f, 0.7f, 0.2f); + T_bs.setTo(0); + T_bs(Range(0,3),Range(2,3)) = 1.0*Mat(cdir); // wierd OpenCV stuff, need to do multiply + T_bs(Range(0,3),Range(1,2)) = 1.0*Mat(ydir); + T_bs(Range(0,3),Range(0,1)) = 1.0*Mat(cdir.cross(ydir)); + T_bs(Range(0,3),Range(3,4)) = 1.0*Mat(fpt); + T_bs(3,3) = 1.0; + //std::cout << "[Nav Grok] S frame =" << std::endl << T_bs << std::endl; + + // set up display coords, really just the S frame + std::vectorcoords; + + for (int i=0; i<16; i++) + { + coords.push_back(T_bs(i)); + //std::cout << T_bs1(i) << std::endl; + } + CV_Assert( norm(coords, T_bs.reshape(1,1), NORM_INF) == 0 ); + } + catch (const test_excep& e) + { + ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str()); + ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); + return false; + } + return true; +} + bool CV_OperationsTest::TestTemplateMat() { try @@ -884,6 +925,9 @@ void CV_OperationsTest::run( int /* start_from */) if (!TestMatxMultiplication()) return; + + if (!TestSubMatAccess()) + return; if (!operations1()) return; diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index c2ab6d5d1..887eb9261 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1010,7 +1010,7 @@ void initcv2() PyObject* m = Py_InitModule(MODULESTR, methods); PyObject* d = PyModule_GetDict(m); - PyDict_SetItemString(d, "__version__", PyString_FromString("$Rev: 4557 $")); + PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION)); opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL); PyDict_SetItemString(d, "error", opencv_error);