光流可视化

时间:2016-02-14 20:42:03

标签: opencv opticalflow

我正在尝试可视化calcOpticalFlowPyrLK()(OpenCv v3.0.0)的输出。我不是试图用光流绘制整个图像,只是方向箭头。问题是,我无法像示例中那样获得输出。每10帧我更新一次点来计算流量。功能本身

calcOpticalFlowPyrLK(CentroidFrOld, CentroidFrNow, mc, CornersCentroidNow, feat_found, feat_errors, Size(15, 15), 2, cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.03), 0);

CentroidFrOld为灰度帧时,CentroidFrNow为灰度帧+ 1,mcvector<Point2f>点数组,CornersCentroidNow为空数组等待充满新的一点。

绘制它们时,我使用简单的代码:

for (size_t i = 0; i < CornersCentroidNow.size(); i++){             
    if (feat_errors[i] > MAX_ERR || feat_found[i] == 0) continue; 
    Point p0(ceil(mc[i].x), ceil(mc[i].y)); // are the points of interest (centroids of contours)
    Point p1(ceil(CornersCentroidNow[i].x), ceil(CornersCentroidNow[i].y));     
    arrowedLine(empty, p0, p1, Scalar(0, 0, 255), 2, 8, 0, 0.2);
}

这段代码之后。当我每帧绘制它时,我得到这个输出: First step

如果我更新用于calcOpticalFlowPyrLK()功能的前一帧

CentroidFrOld = CentroidFrNow.clone();

我得到了这个输出(线很短,每10帧移动一次 - 设置为获得新点) Second step

如果前面的点恰好也是下一个点

CentroidFrOld = CentroidFrNow.clone();
mc = CornersCentroidNow;

我得到这个输出(线很短,但它随着对象一起移动) Third step

我无法实现的所需输出是

Desired out

我是否需要手动拉长线?没有人在类似的光流实现实例中这样做

1 个答案:

答案 0 :(得分:3)

void drawOptFlowMapF(const Mat& flow, Mat& cflowmap, int step, const Scalar& color) {
    for (int y = 0; y < cflowmap.rows; y += step)
        for (int x = 0; x < cflowmap.cols; x += step)
        {
            const Point2f& fxy = flow.at< Point2f>(y, x);
            line(cflowmap, Point(x, y), Point(cvRound(x + fxy.x), cvRound(y + fxy.y)),
                color);
            circle(cflowmap, Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), 1, color, -1);
        }
}
void displayF(Mat flow)
{
    //extraxt x and y channels
    cv::Mat xy[2]; //X,Y
    cv::split(flow, xy);

    //calculate angle and magnitude
    cv::Mat magnitude, angle;
    cv::cartToPolar(xy[0], xy[1], magnitude, angle, true);

    //translate magnitude to range [0;1]
    double mag_max;
    cv::minMaxLoc(magnitude, 0, &mag_max);
    magnitude.convertTo(magnitude, -1, 1.0 / mag_max);

    //build hsv image
    cv::Mat _hsv[3], hsv;
    _hsv[0] = angle;
    _hsv[1] = cv::Mat::ones(angle.size(), CV_32F);
    _hsv[2] = magnitude;
    cv::merge(_hsv, 3, hsv);

    //convert to BGR and show
    cv::Mat bgr;//CV_32FC3 matrix
    cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
    cv::imshow("optical flow", bgr);
    imwrite("c://resultOfOF.jpg", bgr);
    cv::waitKey(0);
}