OpenCV访问当前帧和上一帧 我需要OpenCV C++中的一些计算的当前和先前的框架。
以下是我目前的代码:OpenCV访问当前帧和上一帧 我需要OpenCV C++中的一些计算的当前和先前的框架。,c++,opencv,matrix,vector,computer-vision,C++,Opencv,Matrix,Vector,Computer Vision,以下是我目前的代码: 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 << "em
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;
应该是这样的:
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]
//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;
}
这应该很简单,我做错了什么?您确定该代码段中没有遗漏循环吗?如果我尝试这样的方法:
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]
//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;
}
似乎您的
FIFO
正在按预期工作。。。您对函数的期望是什么?为什么还要使用向量呢。您只需要两个变量,用于前一帧和当前帧。每当出现新帧时,分配previous\u frame=current\u frame;当前_帧=新_帧编码>并检查是否(上一个_frame.empty()| |当前_frame.empty())继续代码>还是什么