OpenCV 将相同的图像放入容器中

OpenCV placing the same image into the container

我正在尝试编写一个程序,从视频中捕获 n 帧并将它们放入容器中以供进一步工作(之后我正在制作拼贴画)。但是我遇到了一个问题,当容器中的所有图像都相同时(它只填充了最后捕获的图​​像)。我已经检查图像是否被正确捕获,因为我也在保存它们并且可以清楚地看到它们是不同的。

这是我的代码:

std::vector<cv::Mat> MY_framelist; //container for captured frames

cv::VideoCapture myvid; 
cv::Mat MY_frame; //will capture frames here
myvid.open(pass_filename); //open video file(char* pass_filename=12.mp4)

if (!myvid.isOpened()) {
    printf("Capture not open \n");
}

double x_length = myvid.get(CV_CAP_PROP_FRAME_COUNT); //get maxlength of the video

uint each_frame = uint(x_length) / 16; //capture every 16 frames

    for (uint j = 0, current_frame = 1; (current_frame < x_length) && (j < 16); current_frame += each_frame, j++)
{

        myvid.set(CV_CAP_PROP_POS_FRAMES, current_frame); //set frame
        myvid.read(MY_frame);  // then capture the next one
        MY_framelist.push_back(MY_frame); //place it into the container


        std::stringstream frameNum; //generating name for saved images
        frameNum << j + 1;
        if (j + 1 <= 9)
            my_filename += "0";
        my_filename += frameNum.str();
        my_filename += ".jpg";

        cv::imwrite(my_filename.c_str(), MY_frame); //saving images to prove that they are captured correctly

        my_filename = "test";

        printf(" and Image # ");
        printf("%d", j + 1);
        printf(" saved \n");

    }

因此 MY_framelist 将包含 16 张上次拍摄的相同图像。 我做错了什么 here? 我在这里看到了一些解决方法,但我并不是真的很想这样做,因为这会导致结果不那么准确。 提前致谢!

OpenCV Mat 副本是 浅拷贝,即只复制 header,不复制数据。所以在这里:

MY_framelist.push_back(MY_frame); //place it into the container

您将得到一个始终具有相同图像的容器。

你还需要做一个深拷贝复制数据:

MY_framelist.push_back(MY_frame.clone()); //place it into the container