OpenCV Mat数组访问,哪种方式是最快的和为什么? [英] OpenCV Mat array access, which way is the fastest for and why?

查看:620
本文介绍了OpenCV Mat数组访问,哪种方式是最快的和为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在OpenCV中访问Mat中的数据的方式。如你所知,我们可以通过多种方式获取数据。我想存储图像(宽x高x 1深度)在Mat和循环访问图像中的每个像素。使用ptr<>(irow)获得行像素,然后访问行中的每一列是最好的方法?或在<>(irow,jcol)下使用是最好的?或者使用直接计算索引通过使用index = irow * Width + jrow是最好的?任何人都知道原因。

I am wondering about the way of accessing data in Mat in OpenCV. As you know, we can access to get data in many ways. I want to store image (Width x Height x 1-depth) in Mat and looping access each pixel in the image. Using ptr<>(irow) to get row-pixel and then access each column in the row is the best way? or using at<>(irow,jcol) is the best? or using directly calculate the index by using index = irow*Width + jrow is the best? Anyone know the reason.

提前感谢

推荐答案

这是一个老问题,但我认为当前的答案是以某种方式误导。

I realize this is an old question, but I think the current answers are somehow misleading.

在< T>(...) code>和 ptr< T>(...)将在调试模式下检查边界。如果未定义 _DEBUG 宏,则它们将基本计算 y * width + x 数据或数据本身。因此在释放模式下在< T>(...)处使用等效于自己计算指针,但更安全,因为计算指针不仅仅是 y * width + x 如果矩阵只是另一个矩阵的子视图。在调试模式下,你得到安全检查。

Calling both at<T>(...) and ptr<T>(...) will check the boundaries in the debug mode. If the _DEBUG macro is not defined, they will basically calculate y * width + x and give you either the pointer to the data or the data itself. So using at<T>(...) in release mode is equivalent to calculating the pointer yourself, but safer because calculating the pointer is not just y * width + x if the matrix is just a sub-view of another matrix. In debug mode, you get the safety checks.

我认为最好的方法是逐行处理图像,获取行指针使用 ptr (y),然后使用 p [x] 。这有一个好处,你不必计数各种数据布局,仍然是内部循环的简单指针。

I think the best way is to process the image row-by-row, getting the row pointer using ptr<T>(y) and then using p[x]. This has the benefit that you don't have to count with various data layouts and still plain pointer for the inner loop.

你可以使用简单的指针,一路,将是最有效的,因为你避免一个乘法每行,但是你需要使用 step1(i)提前的指针。我认为使用 ptr< T>(y)是一个不错的权衡。

You can use plain pointers all the way, which would be most efficient because you avoid one the multiplication per row, but then you need to use step1(i) to advance the pointer. I think that using ptr<T>(y) is a nice trade-off.

这篇关于OpenCV Mat数组访问,哪种方式是最快的和为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆