1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| void drawOpticalFlow(const cv::Mat& oflow, cv::Mat& flowImage, int stride, float scale, const cv::Scalar& color) {
if (flowImage.size() != oflow.size()) { flowImage.create(oflow.size(), CV_8UC3); flowImage = cv::Vec3i(255,255,255); }
for (int y = 0; y < oflow.rows; y += stride) for (int x = 0; x < oflow.cols; x += stride) { cv::Point2f vector = oflow.at< cv::Point2f>(y, x); cv::line(flowImage, cv::Point(x, y), cv::Point(static_cast<int>(x + scale*vector.x + 0.5), static_cast<int>(y + scale*vector.y + 0.5)), color); cv::circle(flowImage, cv::Point(static_cast<int>(x + scale*vector.x + 0.5), static_cast<int>(y + scale*vector.y + 0.5)), 1, color, -1); } }
int main() { cv::Mat frame1= cv::imread("goose/goose230.bmp", 0); cv::Mat frame2= cv::imread("goose/goose237.bmp", 0);
cv::Mat combined(frame1.rows, frame1.cols + frame2.cols, CV_8U); frame1.copyTo(combined.colRange(0, frame1.cols)); frame2.copyTo(combined.colRange(frame1.cols, frame1.cols+frame2.cols)); cv::imshow("Frames", combined);
cv::Ptr<cv::DualTVL1OpticalFlow> tvl1 = cv::createOptFlow_DualTVL1();
std::cout << "regularization coeeficient: " << tvl1->getLambda() << std::endl; std::cout << "Number of scales: " << tvl1->getScalesNumber() << std::endl; std::cout << "Scale step: " << tvl1->getScaleStep() << std::endl; std::cout << "Number of warpings: " << tvl1->getWarpingsNumber() << std::endl; std::cout << "Stopping criteria: " << tvl1->getEpsilon() << " and " << tvl1->getOuterIterations() << std::endl; cv::Mat oflow; tvl1->calc(frame1, frame2, oflow);
cv::Mat flowImage; drawOpticalFlow(oflow, flowImage, 8, 2, cv::Scalar(0, 0, 0));
cv::imshow("Optical Flow", flowImage);
tvl1->setLambda(0.075); tvl1->calc(frame1, frame2, oflow);
cv::Mat flowImage2; drawOpticalFlow(oflow, flowImage2, 8, 2, cv::Scalar(0, 0, 0));
cv::imshow("Smoother Optical Flow", flowImage2); cv::waitKey(); }
|