Optical Flow Color Map in OpenCV

南笙酒味 提交于 2019-12-25 02:43:29

问题


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);

Display function:

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);
}

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.

And the watch window says that

flow    identifier "flow" is undefined

Any help/alternate solution would be really appreciated.


回答1:


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



来源:https://stackoverflow.com/questions/20737580/optical-flow-color-map-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!