我正在尝试可视化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,mc
为vector<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);
}
如果我更新用于calcOpticalFlowPyrLK()
功能的前一帧
CentroidFrOld = CentroidFrNow.clone();
我得到了这个输出(线很短,每10帧移动一次 - 设置为获得新点)
如果前面的点恰好也是下一个点
CentroidFrOld = CentroidFrNow.clone();
mc = CornersCentroidNow;
我无法实现的所需输出是
我是否需要手动拉长线?没有人在类似的光流实现实例中这样做
答案 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);
}