Ask Your Question
0

openCV, conversion between Mat and array by reading from video

asked 2017-12-18 23:05:50 -0600

opencvddddd gravatar image

updated 2017-12-19 11:45:30 -0600

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.

  1. 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?

  2. 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;
    }
    
  3. 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.

  4. 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.

edit retag flag offensive close merge delete

Comments

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.

berak gravatar imageberak ( 2017-12-19 03:07:14 -0600 )edit

There is another 3rd party API which only accepts raw data, I only call this API without control over it.

opencvddddd gravatar imageopencvddddd ( 2017-12-19 11:43:30 -0600 )edit

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 a std::vector<UINT16>, for example. You pass in a pointer to the beginning of the first element, and the constructor takes care of everything else:

Mat frame_content = Mat(480, 640, CV_16UC1, &depths[0]).clone();

Of course, you don't have to use a std::vector, like if you used a UINT16 *depths pointer in conjunction with new[], the code would be:

Mat frame_content = Mat(480, 640, CV_16UC1, depths).clone();
sjhalayka gravatar imagesjhalayka ( 2017-12-19 14:05:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-12-19 02:12:36 -0600

LBerger gravatar image

You can use cvtColor function to change color space. All color code conversions are here

Now in memory mat structure is here

I don't understand what you want array_buffer. You can use split to get B plane, G plane and R plane in three mat.

if mat is continuous in memory you don't need a loop :

memcpy(array_buffer, Mat_frame.ptr(0), Mat_frame.cols*Mat_frame.rows * sizeof(uint8_t)*3);
edit flag offensive delete link more

Comments

Yes, if it's contiguous, we can copy all once. But I read some answers on stackoverflow, it may not be contiguous.

opencvddddd gravatar imageopencvddddd ( 2017-12-19 11:45:00 -0600 )edit

Just test it

if (Mat_frame.isContinuous()) 
memcpy(array_buffer, Mat_frame.ptr(0), Mat_frame.cols*Mat_frame.rows * sizeof(uint8_t)*3);
else
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;
}

Mat is not continuous when you use ROI or reshape... : you change initial row or column values (not using a deep copy)

LBerger gravatar imageLBerger ( 2017-12-19 11:49:24 -0600 )edit

Thanks for the testing.

  1. If I don't use ROI or reshape, your code above should be work, right?

  2. For if condition, there is no problem, copy all if continuous. But for the else condition, the premise is that each row is continuous, I am reading the link you posted, no statement says memory for each row is continuous.

opencvddddd gravatar imageopencvddddd ( 2017-12-19 12:06:19 -0600 )edit

A row is always continuous and in color data are BGRBGRBGr...

LBerger gravatar imageLBerger ( 2017-12-19 12:14:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-18 23:05:50 -0600

Seen: 7,495 times

Last updated: Dec 19 '17