opencv求两张图像光流_OpenCV中的光流颜色图

本文介绍了在OpenCV中利用farneback方法计算密集光流并显示颜色图的过程。作者遇到的编译异常是由于在处理光流数据时指针未正确设置。通过修正指针问题并使用cv::cvtColor进行HSV到RGB的转换解决了问题。
摘要由CSDN通过智能技术生成

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

{

for(x=0;x

{

const Point2f& fxy=flow.at(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

{

hsv_ptr=imgColorHSV.ptr(y);

color_ptr=imgColor.ptr(y);

for(x=0;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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值