【问题标题】:OpenCV placing the same image into the containerOpenCV将相同的图像放入容器中
【发布时间】:2016-06-05 11:09:55
【问题描述】:

我正在尝试编写一个程序,从视频中捕获 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? 我在这里看到了一些解决方法,但我并不是很想这样做,因为它会导致结果不那么准确。 提前致谢!

【问题讨论】:

  • 难道cv::Mat 是一个处理程序,只有那个处理程序被复制到向量中?

标签: c++ opencv vector std opencv3.0


【解决方案1】:

OpenCV Mat 复制是浅复制,即只复制标题,不复制数据。所以在这里:

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

您将获得一个始终包含相同图像的容器。

您也需要进行深拷贝复制数据:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2023-04-07
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多