使用OpenCV将视频帧读取到指定的指针(C ++)

时间:2018-05-08 22:35:46

标签: c++ opencv pointers memory-management video-capture

有没有办法用视频捕捉读取视频帧并指定它应该在内存中的位置?让我们说,假设我有一个指针char * p,并为帧预先分配了正确的内存量,我可以在帧中读到p的地址吗?

1 个答案:

答案 0 :(得分:1)

cv::Mat is designed to handle memory management of images automatically, but it can also be used with externally allocated buffers. To do this, pass in a pointer to the external buffer to the Mat constructor.

Make sure the buffer size, Mat size, and type (channels, depth, etc.) match the output coming out of VideoCapture.

Example:

unsigned char *data; // Points to buffer of appropriate size.
cv::VideoCapture cap; // A valid capture

// This only allocates the Mat header with a reference to "data"
cv::Mat frame {
    rows,
    cols,
    CV_8UC3, // image type, here 3-channel, 8 bits per channel, unsigned
    data
};
cap >> frame;  // Image data stored into buffer at "data"