简体   繁体   English

OpenCV使用步幅从数组生成cv :: Mat

[英]OpenCV generate cv::Mat from array using stride

I have an array of pixel data in RGBA format. 我有一个RGBA格式的像素数据数组。 Although I have already converted this data to grayscale using the GPU (thus all 4 channels are identical). 尽管我已经使用GPU将这些数据转换为灰度(因此所有4个通道都是相同的)。

I now want to use this grayscale data in OpenCV, and I don't want to store 4 copies of the same data. 我现在想在OpenCV中使用此灰度数据,并且我不想存储4个相同数据的副本。 Is it possible to create a cv::Mat structure from this pixel array by specifying a stride. 是否可以通过指定步幅从此像素数组创建cv :: Mat结构。 (ie only read out every 4th byte) (即,仅每4个字节读出一次)

I am currently using 我目前正在使用

GLubyte* Img = stuff from GPU;
cv::Mat tmp(height, width, CV_8UC4, Img);

But this copies all the data, or does it wrap the existing pointer into a cv::Mat without copying it? 但这会复制所有数据,还是将现有指针包装到cv :: Mat中而不进行复制? If it wraps without copy then I will be happy to use standard c++ routines to copy only the data I want from Img into a new section of memory and then wrap this as cv::Mat. 如果它不复制而包装,那么我很乐意使用标准的c ++例程将我想要的数据仅从Img复制到内存的新部分,然后将其包装为cv :: Mat。

Otherwise how would you suggest doing this to reduce the amount of data being copied. 否则,您将建议如何这样做以减少要复制的数据量。

Thanks 谢谢

The code that you are using 您正在使用的代码

cv::Mat tmp(rows, cols, CV_8UC4, dataPointer);

does not perform any copy but only assign the data field of the Mat instance. 不执行任何复制,而仅分配Mat实例的data字段。

If it's ok for you to work with a matrix of 4 channels, then just go on. 如果可以使用4个通道的矩阵,则继续。 Otherwise, if you prefer working with a 1-channel matrix, then just use the function cv::cvtColor() to create a new image with a single channel (but then you will get one additional image in memory and pay the CPU cycles for the conversion): 否则,如果您更喜欢使用1通道矩阵,则只需使用函数cv::cvtColor()即可创建一个具有单个通道的新图像(但是您将在内存中获得一个额外的图像并为此付出CPU周期)转换):

cv::Mat grey;
cv::cvtColor(tmp, grey, CV_BGR2GRAY);

Finally, one last thing: if you can deinterlace the colorplanes beforehand (for example on the GPU) and get some image with [blue plane, green plane, red plane], then you can pass CV_8UC1 as image type in the construction of tmp and you get a single channel grey image without any data copy. 最后,最后一两件事:如果你可以去隔行的colorplanes事先(例如在GPU),并获得与[蓝色小面,绿面,红面]一些图像,那么你可以通过CV_8UC1作为图像类型的建筑tmp和您将获得一个单通道灰度图像,而没有任何数据副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM