我试图找到两个图像序列之间的光流,当我运行程序时,它会在calcOpticalFlowFarneback()函数中显示断言失败。任何人都可以帮我解决这个问题。
#include "stdafx.h"
#include<opencv\cv.h>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\calib3d\calib3d.hpp>
#include<opencv2\core\core.hpp>
using namespace cv;
using namespace std;
void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, double scale, 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);
}
}
int main()
{
Mat flow,cflow;
Mat Previous_Gray=imread("image2.png");
Mat Current_Gray=imread("image3.png");
cvtColor(Current_Gray,Current_Gray,CV_BGR2GRAY);
cvNamedWindow("optical Flow", CV_WINDOW_NORMAL);
calcOpticalFlowFarneback(Previous_Gray,Current_Gray,flow,0.5, 3, 15, 3, 5, 1.2, 0);
cvtColor(Previous_Gray, cflow, CV_GRAY2BGR);
drawOptFlowMap(flow, cflow, 32, 50, CV_RGB(0, 255, 0));
imshow("optical Flow", cflow);
return 0;
}
答案 0 :(得分:2)
Mat imread(const string& filename, int flags=1)
,你在Mat Previous_Gray=imread("image2.png");
行中使用的电话会返回3频道的rgb图片。因此断言的一部分是
prev0.channels()== next0.channels()== 1
是假的。您是否忘记在调用calcOpticalFlowFarneback之前转换PreviousGray ?
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread