我正在研究使用背景減法檢測感興趣對象的項目,並使用OpenCV C++中的光流跟蹤它們。我能夠使用背景扣除來檢測感興趣的對象。我能夠在單獨的程序中實現OpenCV Lucas Kanade光流。但是,我堅持如何在一個程序中使用這兩個程序。 frame1
保存來自視頻的實際幀,contours2
是從前景對象中選擇的輪廓。總之,如何將從背景減法方法獲得的forground對象輸入calcOpticalFlowPyrLK
?或者,如果我的方法是錯誤的,請幫助我。先謝謝你。在OpenCV C++中用於跟蹤對象的背景減法和光流
Mat mask = Mat::zeros(fore.rows, fore.cols, CV_8UC1);
drawContours(mask, contours2, -1, Scalar(255), 4, CV_FILLED);
if (first_frame)
{
goodFeaturesToTrack(mask, features_next, 1000, 0.01, 10, noArray(), 3, false, 0.04);
fm0 = mask.clone();
features_prev = features_next;
first_frame = false;
}
else
{
features_next.clear();
if (!features_prev.empty())
{
calcOpticalFlowPyrLK(fm0, mask, features_prev, features_next, featuresFound, err, winSize, 3, termcrit, 0, 0.001);
for (int i = 0; i < features_prev.size(); i++)
line(frame1, features_prev[i], features_next[i], CV_RGB(0, 0, 255), 1, 8);
imshow("final optical", frame1);
waitKey(1);
}
goodFeaturesToTrack(mask, features_next, 1000, 0.01, 10, noArray(), 3, false, 0.04);
features_prev = features_next;
fm0 = mask.clone();
}