호 함수 자체의 변경 필요 없다 예를 들어, 코드에 이어 frame1_1C의 왼쪽 절반에 속하는 행렬을 초기화한다. 옵티컬 플로우를 계산하려는 이미지 부분 만 함수에 전달하면됩니다.
당신은 당신이 광학 흐름 계산을 수행하고자하는 이미지의 범위를 정의 할 수 있습니다.
wanted_image = 이미지 (범위 (X1, Y1), 거리 (X2, Y2))를 사용
다음 샘플 폴더에 lkdemo.cpp에 기초하여 작동하는 코드이다. 가치있는 변경 만이
회색 = 회색 (범위 (1,480), 범위 (1,320)); // 이미지의 왼쪽 절반을 지정합니다.
관심 영역을 정의합니다.
#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;
}