Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV padded Mat, use with VideoWriter

I have an image represented using a 2D array of 3 byte color values (OpenCV type CV_8UC3). The array is not densely packed, but instead elements are aligned on a 4 byte boundary, i.e. there is 1 byte of padding.

So the array is of the format

RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_RGB_...

I want to access this data using OpenCV, without making a new copy of it. But OpenCV Mat of type CV_8UC3 is packed by default, so I create the Mat with explicit strides/steps, using

cv::Mat mat(
    2,
    sizes, // = (1080, 1920)
    CV_8UC3,
    reinterpret_cast<void*>(data),
    steps // = (7680, 4)
);

data is a pointer to a rgb_color array, defined by

struct alignas(4) rgb_color { std::uint8_t r, g, b; };

However using this mat with cv::VideoWriter still produces incorrect results, and it seems that VideoWriter ignores the strides of the Mat.

Is it possible to use VideoWriter and other OpenCV functionality with matrices of this type?