openCV, conversion between Mat and array by reading from video
Purpose: read all images from a video, image format should be RGB arrays, add overlay to image(for some reason, I have to do the overlay on RGB array), display image. I did some research online, but there is no official or highly up-voted solution.
Currently what I do is read image as cv::Mat, then convert Mat to array, can I directly read image as RGB arrays from video?
Below is how I convert cv::Mat to RGB arrays, is it correct? Not sure if one row of data is contiguous in memory for cv::Mat? Or is all rowscolumns3 contiguous in memory for cv::Mat? Do you have better solutions?
for (int i = 0; i < Mat_frame; i++) { memcpy(array_buffer, Mat_frame.ptr(i), Mat_frame.cols * sizeof(uint8_t)*3); array_buffer += Mat_frame.cols; }
How can I convert RGB arrays back to cv::Mat? Is there a function I can use directly? If not, I can do the reverse of 2 code to convert.
About display, cv::waitKey(10), this means giving 10ms for image to display. If I want to display images ASAP, what the minimum value I can set? Or is it really depends on my machine's performance?
I really appreciate your comments and time.
please note, that it is BGR order in opencv, not RGB.
also, please explain, why you have to access the raw memory, this is usually a terrible idea.
There is another 3rd party API which only accepts raw data, I only call this API without control over it.
Regarding question #2:
You can obtain a pointer to the Mat's data using the
Mat::data
member variable. There's your BGR array, wrapped in a powerful class. Converting to RGB is pretty simple:cvtColor(image, image, CV_BGR2RGB);
.Regarding question #3:
Here
depths
is astd::vector<UINT16>
, for example. You pass in a pointer to the beginning of the first element, and the constructor takes care of everything else:Of course, you don't have to use a
std::vector
, like if you used aUINT16 *depths
pointer in conjunction withnew[]
, the code would be: