I’m new to OpenCV and C++. Currently, I’m learning OpenCV and there’s something about its compilation that confuses me. I came across a trace function, and when I looked at its definition in the source code, it was defined as cv::Scalar cv::trace( InputArray _m ) in the modules/core/src/matrix_operations.cpp file.
However, after compiling the library, when I use cv::trace in my own code, I find that its definition changes to double trace(const Matx<_Tp, m, n>& a) in the include/opencv2/core/matx.hpp file. Furthermore, when I debug using gdb, the debugger steps into the cv::Scalar cv::trace( InputArray _m ) function.
This has left me a bit puzzled. Which is the actual code that gets called when I use cv::trace? If I want to modify the implementation, should I edit the source code in the .cpp file and recompile, or should I make modifications in the .hpp file?
I mean, what’s the actual code get executed? There a function named trace in source, but there’s a different function named trace in include location after compiling the source.
It looks like this, why I’m linked back to here after I have already compiled the code?
(gdb) s
cv::debug_build_guard::_InputArray::_InputArray (this=0xffffffffed90, m=...) at /home/jx23014/opencv347/include/opencv2/core/mat.inl.hpp:88
88 inline _InputArray::_InputArray(const Mat& m) { init(MAT+ACCESS_READ, &m); }
(gdb) s
cv::Size_<int>::Size_ (this=0xffffffffeda0) at /home/jx23014/opencv347/include/opencv2/core/types.hpp:1656
1656 : width(0), height(0) {}
(gdb) s
cv::debug_build_guard::_InputArray::init (this=0xffffffffed90, _flags=16842752, _obj=0xffffffffecd8) at /home/jx23014/opencv347/include/opencv2/core/mat.inl.hpp:77
77 { flags = _flags; obj = (void*)_obj; }
(gdb) s
cv::trace (_m=...) at /home/jx23014/opencv-3.4.7/modules/core/src/matrix_operations.cpp:229
229 CV_INSTRUMENT_REGION();
(gdb) backtrace
#0 0x0000ffffbca7896c in cv::trace(cv::debug_build_guard::_InputArray const&) (_m=...) at /home/jx23014/opencv-3.4.7/modules/core/src/matrix_operations.cpp:229
#1 0x0000000000402990 in main() () at /home/jx23014/Dev/src/opencvWatch.cpp:55
(gdb) bt
#0 0x0000ffffbca7896c in cv::trace(cv::debug_build_guard::_InputArray const&) (_m=...) at /home/jx23014/opencv-3.4.7/modules/core/src/matrix_operations.cpp:229
#1 0x0000000000402990 in main() () at /home/jx23014/Dev/src/opencvWatch.cpp:55
(gdb) n
231 Mat m = _m.getMat();
For example in
(gdb) s
cv::Size_<int>::Size_ (this=0xffffffffeda0) at /home/jx23014/opencv347/include/opencv2/core/types.hpp:1656
1656 : width(0), height(0) {}
I’m calling function in /home/jx23014/opencv347/include/opencv2/core/types.hpp, which is my after-compiled library. But keep stepping to
(gdb) s
cv::trace (_m=...) at /home/jx23014/opencv-3.4.7/modules/core/src/matrix_operations.cpp:229
229 CV_INSTRUMENT_REGION();
It’s now in (gdb) s cv::trace (_m=...) at /home/jx23014/opencv-3.4.7/modules/core/src/matrix_operations.cpp:229 229 CV_INSTRUMENT_REGION();
The double trace(const Matx<_Tp, m, n>& a function mentioned before is a function found in /home/jx23014/opencv347/include/opencv2/core/matx.hpp. This function seems to have a same name as “trace” while not called in gdb. It looks like this
template<typename _Tp, int m, int n> static inline
double trace(const Matx<_Tp, m, n>& a)
{
_Tp s = 0;
for( int i = 0; i < std::min(m, n); i++ )
s += a(i,i);
return s;
}
I’m sorry, I know it’s a outdated one. Our course project asked us to rewritten some opencv function in assembly for performance improvement. It specified the trace function in this version.