gpt4 book ai didi

c++ - 我实际上如何在捕获的一半图像上设置光流?

转载 作者:行者123 更新时间:2023-11-28 07:40:29 25 4
gpt4 key购买 nike

我已经在 OpenCV 中使用 C++ 实现了一个光流代码。但是,我想检测一半图像帧中的光流。我应该编辑哪一部分?是下面这个函数吗?

cvCalcOpticalFlowPyrLK(
frame1_1C, frame2_1C,
pyramid1, pyramid2,
frame1_features,
frame2_features,
number_of_features,
optical_flow_window,
5,
optical_flow_found_feature,
optical_flow_feature_error,
optical_flow_termination_criteria,
0 );

最佳答案

没有。函数本身无需更改。您需要做的就是仅将要计算光流的图像部分传递给该函数。

您可以定义要对其执行光流计算的图像范围。使用

wanted_image=image(Range(x1,y1), Range(x2,y2))

以下是基于示例文件夹中的 lkdemo.cpp 的工作代码。唯一有值(value)的改变是

gray = gray(Range(1,480), Range(1,320)); //Gives the left half of the image

它定义了感兴趣的区域。

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
cout << "*** Using OpenCV version " << CV_VERSION <<" ***"<< endl;
cout << "\n\nUsage: \n"
"\tESC - quit the program\n"
"\tr - auto-initialize tracking\n"
"\tc - delete all the points\n"
"\tn - switch the \"night\" mode on/off\n"<< endl;
}

int main( int argc, char** argv )
{
help();
//Termination of the algo after 20 iterations or accuracy going under 0.03
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.3);
Size subPixWinSize(10,10), winSize(31,31);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
//Video capture is from the default device i.e. the webcam
VideoCapture cap(0);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}

namedWindow( "Half screen Optical flow Demo!", 1 );
Mat gray, prevGray, image;
vector<Point2f> points[2];

for(;;)
{
Mat frame;

//Output from the Videocapture is piped to 'frame'
cap >> frame;
if( frame.empty() )
break;

frame.copyTo(image);
cvtColor(image, gray, COLOR_BGR2GRAY);

// Night mode not disabled
if( nightMode )
image = Scalar::all(0);
gray = gray(Range(1,480), Range(1,320));
if( needToInit || points[0].size()<=5)
{
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.4);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);

}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001);
size_t i, k;

for( i = k = 0; i < points[1].size(); i++ )
{
if( !status[i] )
continue;

points[1][k++] = points[1][i];
circle(image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
points[1].resize(k);
}
needToInit = false;
imshow("Half screen Optical flow Demo!", image);

char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
break;
}
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
cap.release();
return 0;
}

关于c++ - 我实际上如何在捕获的一半图像上设置光流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15922239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com