【问题标题】:OpenCV access current and previous frameOpenCV 访问当前和上一帧
【发布时间】:2021-03-28 15:55:29
【问题描述】:

我需要当前帧和前一帧在 OpenCV C++ 中进行一些计算。

到目前为止,这是我的代码:

    vector <cv::Mat> frames;
    // Current frame
    Mat M = current_frame; 
    Mat F; // previous frame
    // set Previous Frame
    if (frames.empty()) {
        F = M;
        frames.push_back(M);
        cout << "empty" << endl;
    }
    else {
        F = frames.back();
        frames.push_back(M);
        cout << " NOTempty" << endl;
    }

    // print frame Mat means to see if things look okay
    cout << mean(M) << endl;
    cout << mean(F) << endl;

这个想法是M 是当前帧,F 是前一帧。帧存储在frames,一个矩阵向量。如果frames 为空,则让F = M 因为没有前一帧。然后,将M 添加到frames。如果frames 不为空,则M 是当前帧,F 是向量frames 中的最后一个Mat。提取F后,将当前帧M添加到frames的末尾。

我正在打印出MF 的平均值,以便在终端正常工作时我可以在终端中获得一些易于阅读的参考。不幸的是,它看起来不对。

这是打印出来的:

empty
[5.09352, 6.60551, 8.54364, 0]
[5.09352, 6.60551, 8.54364, 0]

 NOTempty
[5.02325, 6.46646, 8.39534, 0]
[92.0037, 97.9186, 106.677, 0]

 NOTempty
[4.94272, 6.38162, 8.32141, 0]
[91.7741, 97.7845, 106.555, 0]

应该是这样的:

empty
[5.09352, 6.60551, 8.54364, 0]
[5.09352, 6.60551, 8.54364, 0]

 NOTempty
[5.02325, 6.46646, 8.39534, 0]
[5.09352, 6.60551, 8.54364, 0]

 NOTempty
[4.94272, 6.38162, 8.32141, 0]
[5.02325, 6.46646, 8.39534, 0]

这应该很简单,我做错了什么?

【问题讨论】:

  • 为什么还要使用向量。您只需要两个变量,用于上一帧和当前帧。每当出现新框架时,分配previous_frame = current_frame; current_frame = new_frame; 并检查if (previous_frame.empty() || current_frame.empty()) continue; 或其他内容

标签: c++ opencv matrix vector computer-vision


【解决方案1】:

你确定你没有在那个 sn-p 的某个地方丢失一个循环吗?如果我尝试这样的事情:

//create a dummy vector of mats:
std::vector<cv::Mat> sourceMats;
sourceMats.push_back( cv::Mat(1, 4, CV_32F, {1, 1, 1, 1}) );
sourceMats.push_back( cv::Mat(1, 4, CV_32F, {2, 2, 2, 2}) );
sourceMats.push_back( cv::Mat(1, 4, CV_32F, {3, 3, 3, 3}) );

//frames buffer:
std::vector <cv::Mat> frames;

//loop thru all frames:
for( int i = 0; i < (int)sourceMats.size(); i++ ){

    // Current frame
    cv::Mat M = sourceMats[i];
    cv::Mat F; // previous frame

    // set Previous Frame
    if (frames.empty()) {
        F = M;
        frames.push_back(M);
        std::cout << "empty" << std::endl;
    }
    else {
        F = frames.back();
        frames.push_back(M);
        std::cout << " NOTempty" << std::endl;
    }

    // do not compute the mean, I just want to check out the FIFO contents:
    std::cout << M << std::endl;
    std::cout << F << std::endl;
}

我得到了这个输出(请注意,我删除了 mean 函数 - 只是为了检查原始数据):

empty
[1, 1, 1, 1]
[1, 1, 1, 1]
NOTempty
[2, 2, 2, 2]
[1, 1, 1, 1]
NOTempty
[3, 3, 3, 3]
[2, 2, 2, 2]

您的FIFO 似乎按预期工作...您对mean 函数有什么期望?

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多