OpenCV中的光流颜色图 [英] Optical Flow Color Map in OpenCV

查看:1491
本文介绍了OpenCV中的光流颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用farneback方法计算和显示OpenCV中的密集光流。我发现了一个使用CUDA函数来生成和显示我用作我自己的代码的基础的颜色映射的示例。
光流计算:

I am trying to calculate and display dense optical flow in OpenCV using the farneback method. I found an example that uses CUDA functions to generate that and display the color map which I used as a base for my own code. Optical flow calculation:

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时都会抛出异常。 / p>

The problem is that the compiler throws an exception every time it reaches imshow.

Unhandled exception at at 0x757A4B32 in advection2.exe: Microsoft C++ exception: cv::Exception at memory location 0x00ADF4C8.

观察窗口表示

flow    identifier "flow" is undefined

任何帮助/备用解决方案

Any help/alternate solution would be really appreciated.

推荐答案

Bug是你忘记在你的第一个为imgColorHSV数据指针设置hsv_ptr循环。要将HSV应用于RGB转换,您可以使用OpenCV函数 cvColor

The Bug is that you forgot to set up the hsv_ptr to the imgColorHSV data pointer in your first for loops. To apply the HSV to RGB converting you could use the OpenCV function cvColor

这篇关于OpenCV中的光流颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆