我正在尝试使用farneback方法计算并显示OpenCV中的密集光流。我找到了一个使用CUDA函数生成它并显示我用作自己代码基础的颜色映射的示例。 光流计算:
calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
drawField(flow,cflow);
imshow("flows",cflow);
显示功能:
void drawField(const Mat& flow, Mat& imgColor){
Mat imgColorHSV = cv::Mat::zeros(Size(imgColor.cols,imgColor.rows),CV_32FC3);
float max_s = 0;
float *hsv_ptr;
unsigned char *color_ptr;
unsigned char r = 0, g = 0, b = 0;
float angle = 0.0;
float h = 0.0, s = 0.0, v = 0.0;
float deltaX = 0.0, deltaY = 0.0;
int x = 0, y = 0;
for(y=0;y<imgColor.rows;y++)
{
for(x=0;x<imgColor.cols;x++)
{
const Point2f& fxy=flow.at<Point2f>(y,x);
deltaX=fxy.x;
deltaY=fxy.y;
angle=atan2(deltaX,deltaY);
if(angle<0)
angle+=2*M_PI;
hsv_ptr[3*x]=angle*180/M_PI;
hsv_ptr[3*x+1]=sqrt(deltaX*deltaX+deltaY*deltaY);
hsv_ptr[3*x+2]=0.9;
if(hsv_ptr[3*x+1]>max_s)
max_s=hsv_ptr[3*x+1];
}
}
for(y=0;y<imgColor.rows;y++)
{
hsv_ptr=imgColorHSV.ptr<float>(y);
color_ptr=imgColor.ptr<unsigned char>(y);
for(x=0;x<imgColor.cols;x++)
{
h=hsv_ptr[3*x];
s=hsv_ptr[3*x+1]/max_s;
v=hsv_ptr[3*x+2];
hsv2rgb(h,s,v,r,g,b);
color_ptr[3*x]=b;
color_ptr[3*x+1]=g;
color_ptr[3*x+2]=r;
}
}
drawLegendHSV(imgColor,15,25,15);
}
问题是编译器每次到达imshow时都会抛出异常。
Unhandled exception at at 0x757A4B32 in advection2.exe: Microsoft C++ exception: cv::Exception at memory location 0x00ADF4C8.
观察窗口说明了
flow identifier "flow" is undefined
非常感谢任何帮助/替代解决方案。