使用Opencv C ++跨越图像(Stride on image using Opencv C++)

我在Windows上并在C ++上使用opencv 2.4.2。 我用imread读取了一个图像并将其放在Mat对象中。 之后,我使用Mat类的函数ptr获取原始数据的指针。 我想知道当图像包含步幅时如何工作。 Mat的数据或像素数据是否有进步?

附加信息:我需要原始数据,因为我使用的是用数组编写的代码,我不想为Mat类更改它。

I am on windows and use opencv 2.4.2 on C++. I read an image with imread and put it in a Mat object. After that, i get a pointer on the raw data using the function ptr of the Mat class. I would like to know how works imread when the image contains stride. Is there stride in the data of the Mat or only data of pixels ?

Additional information: I need the raw data because I am using a code written with array and I don't want to change it for the Mat class.

最满意答案

大步是cv :: Mat中的每个新像素行都不会在最后一行结束后开始。 这是因为如果以4bytes的倍数开始,内存会更快 - 所以只有行中的字节数(像素数*颜色数)不是4的倍数才有意义。

如果没有步幅,你可以检查.isContinuous()将返回true

访问映像的最安全方法是遍历所有行,并使用.ptr(row)获取指向行开头的指针,然后对该行执行任何处理。

如果你需要将opencv与其他lib混合使用,你可以创建一个cv :: mat,它将使用你自己的内存来存储数据,你可以告诉opencv这个没有任何进步

有关详细信息,请参阅http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html

The stride is that each new row of pixels in the cv::Mat doesn't start after the end of the last row. This is because the memory is faster if it starts on a multiple of 4bytes - so it only matters if the number of bytes in a row (number of pixels * number of colours) isn't a multiple of 4.

You can check .isContinuous() will return true if there is no stride

The safest way of accessing an image is to loop over all the rows and use .ptr(row) to get a pointer to the start of the row, then do any processing on that row.

If you need to mix opencv with other libs you can create a cv::mat that will use your own memory for the data and you can tell opencv that there is no stride on this

For lots of details see http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html

更多推荐