访问“Mat”中的矩阵元素OpenCV C++ 中的对象(不是 CvMat 对象)
如何在 OpenCV 2.0 的新“Mat”类中按行、列访问元素?该文档链接如下,但我无法理解它。 http://opencv.willowgarage.com/documentation/cpp/basic_structs.html#mat
How to access elements by row, col in OpenCV 2.0's new "Mat" class? The documentation is linked below, but I have not been able to make any sense of it.
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#mat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于文档:
http://docs.opencv.org/2.4 /modules/core/doc/basic_structs.html#mat
它说:
即可以使用:
也许使用
Mat_
类更容易。它是Mat
的模板包装器。Mat_
重载了operator()
以访问元素。On the documentation:
http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat
It says:
That is, you can use:
Maybe it is easier to use the
Mat_
class. It is a template wrapper forMat
.Mat_
has theoperator()
overloaded in order to access the elements.上面提供的想法很好。为了快速访问(如果您想制作实时应用程序),您可以尝试以下操作:
指针访问比 Mat.at<> 快得多。访问。希望有帮助!
The ideas provided above are good. For fast access (in case you would like to make a real time application) you could try the following:
Pointer access is much faster than the Mat.at<> accessing. Hope it helps!
基于@J. Calleja 说,你有两种选择
方法 1 - 随机访问
如果你想随机访问 Mat 的元素,只需使用
方法 2 - 连续访问
如果你想连续访问,OpenCV 提供了与
STL 迭代器兼容的 Mat 迭代器< /code> ,它更具
C++
风格,或者
如果您对理解方法 2 的工作原理有任何困难,我借用了文章 C 语言的动态二维数组,更加直观和易于理解。
见下图。
Based on what @J. Calleja said, you have two choices
Method 1 - Random access
If you want to random access the element of Mat, just simply use
Method 2 - Continuous access
If you want to continuous access, OpenCV provides Mat iterator compatible with
STL iterator
and it's moreC++
styleor
If you have any difficulty to understand how Method 2 works, I borrow the picture from a blog post in the article Dynamic Two-dimensioned Arrays in C, which is much more intuitive and comprehensible.
See the picture below.
OCV 竭尽全力确保您在不知道元素类型的情况下无法执行此操作,但如果您想要一种易于编码但不是很有效的方式来与类型无关地读取它,您可以使用类似的方法
来做到这 一点好吧,你必须知道类型。 at<>方法是安全的方法,但如果操作正确,直接访问数据指针通常会更快。
OCV goes out of its way to make sure you can't do this without knowing the element type, but if you want an easily codable but not-very-efficient way to read it type-agnostically, you can use something like
To do it well, you do have to know the type though. The at<> method is the safe way, but direct access to the data pointer is generally faster if you do it correctly.
对于
cv::Mat_; mat
只需使用mat(row, col)
访问指定类型的矩阵元素 cv::Mat_< _Tp > 更舒服,因为您可以跳过模板规范。 文档中也指出了这一点。
代码:
输出:
For
cv::Mat_<T> mat
just usemat(row, col)
Accessing elements of a matrix with specified type cv::Mat_< _Tp > is more comfortable, as you can skip the template specification. This is pointed out in the documentation as well.
code:
output: