Commit 30f1ab05 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

make Mat::at<>(i) work with 2d matrices (to retrieve all elements in row-major…

make Mat::at<>(i) work with 2d matrices (to retrieve all elements in row-major order) (reported by Kurt) and fixed bug #1804
parent a227746d
...@@ -561,18 +561,28 @@ template<typename _Tp> inline const _Tp& Mat::at(Point pt) const ...@@ -561,18 +561,28 @@ template<typename _Tp> inline const _Tp& Mat::at(Point pt) const
template<typename _Tp> inline _Tp& Mat::at(int i0) template<typename _Tp> inline _Tp& Mat::at(int i0)
{ {
CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && CV_DbgAssert( dims <= 2 && data &&
(unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 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<typename _Tp> inline const _Tp& Mat::at(int i0) const template<typename _Tp> inline const _Tp& Mat::at(int i0) const
{ {
CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && CV_DbgAssert( dims <= 2 && data &&
(unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 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<typename _Tp> inline _Tp& Mat::at(int i0, int i1, int i2) template<typename _Tp> inline _Tp& Mat::at(int i0, int i1, int i2)
......
...@@ -74,11 +74,17 @@ protected: ...@@ -74,11 +74,17 @@ protected:
bool TestSparseMat(); bool TestSparseMat();
bool TestVec(); bool TestVec();
bool TestMatxMultiplication(); bool TestMatxMultiplication();
bool TestSubMatAccess();
bool operations1(); 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 checkDiff(const Mat& m1, const Mat& m2, const string& s)
void checkDiffF(const Mat& m1, const Mat& m2, const string& s) { if (norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(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() CV_OperationsTest::CV_OperationsTest()
...@@ -438,6 +444,41 @@ bool CV_OperationsTest::SomeMatFunctions() ...@@ -438,6 +444,41 @@ bool CV_OperationsTest::SomeMatFunctions()
} }
bool CV_OperationsTest::TestSubMatAccess()
{
try
{
Mat_<float> 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::vector<float>coords;
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() bool CV_OperationsTest::TestTemplateMat()
{ {
try try
...@@ -884,6 +925,9 @@ void CV_OperationsTest::run( int /* start_from */) ...@@ -884,6 +925,9 @@ void CV_OperationsTest::run( int /* start_from */)
if (!TestMatxMultiplication()) if (!TestMatxMultiplication())
return; return;
if (!TestSubMatAccess())
return;
if (!operations1()) if (!operations1())
return; return;
......
...@@ -1010,7 +1010,7 @@ void initcv2() ...@@ -1010,7 +1010,7 @@ void initcv2()
PyObject* m = Py_InitModule(MODULESTR, methods); PyObject* m = Py_InitModule(MODULESTR, methods);
PyObject* d = PyModule_GetDict(m); 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); opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL);
PyDict_SetItemString(d, "error", opencv_error); PyDict_SetItemString(d, "error", opencv_error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment