光流可视化 [英] Optical Flow visualization
问题描述
我正在尝试可视化calcOpticalFlowPyrLK()
(OpenCv v3.0.0)的输出.我不是在尝试用光流绘制整个图像,而只是在绘制方向箭头.问题是,我无法获得示例中的输出.每隔10帧,我会更新点以进行流程计算.函数本身
I am trying to visualize the output of calcOpticalFlowPyrLK()
(OpenCv v3.0.0). I am not trying to draw whole image with optical flow, only the direction arrow. The problem is, I can't get to the output as in the examples. Every 10 frames I renew the points for the calculation of the flow. The function itself
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
是一个空数组,等待填充新点.
Where CentroidFrOld
is grayscale frame, CentroidFrNow
is grayscale frame+1, mc
is a vector<Point2f>
array of points and CornersCentroidNow
is an empty array waiting to be filled with new points.
绘制它们时,我使用简单的代码:
When drawing them I use simple code:
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);
}
在此代码块之后的
.当我每帧绘制它们时,我得到以下输出:
after this block of code. When I draw them every frame I get this output:
如果我更新用于calcOpticalFlowPyrLK()
功能的上一帧
If I update the previous frame used for calcOpticalFlowPyrLK()
function
CentroidFrOld = CentroidFrNow.clone();
我得到这个输出(行很短,每10帧向前移动-设置为获得新点)
I get this output (the line is short and it is moving foward every 10 frames - as set to get new points)
如果前面的点也恰好是下一个点
If the previous points happen to be next points as well
CentroidFrOld = CentroidFrNow.clone();
mc = CornersCentroidNow;
我得到此输出(该行很短,但它随对象一起移动)
I get this output (the line is short, but it is moving along with the object)
我无法实现的期望输出是
The desired output I can't achieve is
我需要手动加长线吗?在实现光流"的类似示例中没有人这样做
Do I need to manually lengthen the line? Noone is doing so in similar examples of implemantation of Optical Flow
推荐答案
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);
}
这篇关于光流可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!