[英]Reading data cv::Mat
I am looking for a better way to get data from a cv::Mat 32F3C
, i was using this : Vec3f c1 = _image.at<Vec3f>(i,j);
我正在寻找一种更好的方法来从
cv::Mat 32F3C
获取数据,我使用的是: Vec3f c1 = _image.at<Vec3f>(i,j);
it works fine, but not very quick, i saw that this way is better : 它工作正常,但不是很快,我看到这种方法更好:
Vec3f c1(_image.data[step[0]*i + step[1]*j + 0],_image.data[step[0]*i + step[1]*j + 1],_image.data[step[0]*i + step[1]*j + 2]);
it compiles but it's not correct, i also tried this : 它可以编译,但是不正确,我也尝试过这个:
Vec3f c1(_image.data[step*i + channels*j + 0],_image.data[step*i + channels*j + 1],_image.data[step*i + channels*j + 2]);
but same thing, it compile but gives me an other absurd result. 但同样的事情,它可以编译,但给了我另一个荒谬的结果。
I must be missing a factor or something. 我一定缺少一个因素。
Thanks ! 谢谢 !
You can surpass the at() function call and access the data of directly. 您可以超越at()函数调用并直接访问的数据。 Use cv::Mat::ptr ( http://docs.opencv.org/modules/core/doc/basic_structures.html ) to get a pointer to the first element of every row of your image.
使用cv :: Mat :: ptr( http://docs.opencv.org/modules/core/doc/basic_structures.html )获取指向图像每行第一元素的指针。 Next you can access all the elements in that row using the array[index]-operator.
接下来,您可以使用array [index] -operator访问该行中的所有元素。
Image data is most of the time continuous in memory, but not always (think about selecting region of interest in another cv::Mat). 图像数据在内存中大部分时间是连续的,但并不总是连续的(考虑在另一个cv :: Mat中选择感兴趣的区域)。 You can find an example here: OpenCV C++: how access pixel value CV_32F through uchar data pointer .
您可以在此处找到一个示例: OpenCV C ++:如何通过uchar数据指针访问像素值CV_32F 。 The image rows are continuous, which is why you ask a pointer to the first element in each iteration and can then safely access the other elements.
图像行是连续的,这就是为什么您在每次迭代中都要求一个指向第一个元素的指针,然后可以安全地访问其他元素的原因。
You may find this article in the OpenCV-Documentation useful. 您可能会在OpenCV文档中找到这篇文章很有用。 However, I would first write code that is easy to understand, and only optimize it, if it is apparent that I have to optimize exactly that portion of the source code.
但是,如果很明显我必须精确地优化源代码的那一部分,那么我将首先编写易于理解的代码,然后对其进行优化。
The gist of the article is, that there are three methods for accessing the pixels: 本文的要旨是,有三种访问像素的方法:
On-the-fly address calculation: 动态地址计算:
const cv::Vec3f& pixel = img.at< cv::Vec3f >(y, x)
Iterator: 迭代器:
cv::MatIterator< cv::Vec3f > it=img.begin(), end=img.end(); for ( ; it != end ; ++it ) (*it)[0] = (*it)[1]
Pointer: 指针:
cv::Vec3f* pixel_ptr; for (int y = 0; y < img.rows; ++y) { pixel_ptr = img.ptr<cv::Vec3f>(y); for (int x = 0; x < img.cols; ++x) { (*pixel_ptr)[0] = (*pixel_ptr)[1] ++pixel_ptr; } }
Based on the efficient way 基于高效的方式
cv::Mat _image = something;
for(int y = 0; y < _image.rows; ++y)
{
cv::Vec3f* _image_row = _image.ptr<cv::Vec3f>(y);
for(int x = 0; x< _image.cols; ++x)
{
cout << _image_row[x] << " ";
}
cout << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.