OpenCV - Creating an Array of Mat Objects

前端 未结 2 1470
無奈伤痛
無奈伤痛 2021-01-01 05:40

I would have thought this is trivial, but I\'m having some trouble with it.

I want to read a video file into memory and store it in an array. I want the array to be o

相关标签:
2条回答
  • 2021-01-01 06:24

    There are more flaws in your code. At least two of them are:

    1. vidCap.get(CV_CAP_PROP_FRAME_COUNT); does not return the correct number of frames, most of the time. That's it, ffmpeg can't do better. For some codecs it works, for some, in doesn't.

    2. Mat matrices have an interesting behaviour. They are actually pointers to the matrix data, not objects. When you say new Mat you just create a new pointer. And combined with the fact that videoCap returns all the time the same memory area, just with new data, you acutually will have a vector of pointers pointing to the last frame.

    You have to capture the frame in a separate image and copy to the reserved location:

    std::vector<cv::Mat> frames;
    cap >> frame;
    frames.push_back(frame.clone());
    

    Please note the change from array of pointers to a vector of objects. This avoids the need for reading the number of frames beforehand, and also makes the code safer.

    0 讨论(0)
  • 2021-01-01 06:37

    But is there actually a way of creating Mat arrays? I really don't see other options in my case but trying to access an item in the array considers the array as a single Mat and thinks I'm trying to access its data.

    Edit: Found a workaround using a pointer:

    Mat* array = new Mat[arraySize];
    
    0 讨论(0)
提交回复
热议问题