Ask Your Question
0

cvCalcOpticalFlowLK issue

asked 2012-12-03 03:54:01 -0500

aliciadominica gravatar image

So, I'm running a simple cvCalcOpticalFlowLK code but I can't seem to get any flow information nor I can visualize it. Can anyone help me? My code is below:

int main()
{
//First get two images from a AVI file.
CvCapture* pCapture= cvCaptureFromFile("C:\\Users\\Elvan\\Desktop\\UCF Crowd Dataset\\861-13_70.mov");
IplImage *src1=cvQueryFrame(pCapture);

while(IplImage *src2=cvQueryFrame(pCapture))
{

CvSize cvsize;
cvsize.width=src1->width;
cvsize.height=src1->height;


IplImage* Graysrc1=cvCreateImage(cvsize,IPL_DEPTH_8U,1);
cvCvtColor(src1,Graysrc1,CV_RGB2GRAY);

IplImage* Graysrc2=cvCreateImage(cvsize,IPL_DEPTH_8U,1);
cvCvtColor(src2,Graysrc2,CV_RGB2GRAY);


IplImage* flowX=cvCreateImage(cvsize,IPL_DEPTH_32F,1);
IplImage* flowY=cvCreateImage(cvsize,IPL_DEPTH_32F,1);

cvsize.width=3;
cvsize.height=3;

cvCalcOpticalFlowLK(Graysrc1,Graysrc2,cvsize,flowX,flowY);

for (int x=0; x<cvsize.width; x=x+10) {
for (int y=0; y<cvsize.width; y=y+10) {
int vel_x_here = (int)cvGetReal2D( flowX, y, x);
int vel_y_here = (int)cvGetReal2D( flowY, y, x);
cvLine( Graysrc2, cvPoint(x, y), cvPoint(x+vel_x_here,
y+vel_y_here), cvScalarAll(255));
}
}



cvShowImage("Velx",flowX);
cvShowImage("Vely",flowY);
src1=src2;
cvWaitKey(1);

}


return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-12-03 04:13:16 -0500

Vladislav Vinogradov gravatar image

Output of optical flow doesn't contain BGR color values, so it can be used in cvShowImage. To vizualize optical flow, you can draw arrows (as you did before cvShowImage) or convert optical flow to color image (here is a sample code, http://pastebin.com/0hXzFbbY)

edit flag offensive delete link more

Comments

When I show the Graysrc2 image, I can't see the arrows :/

aliciadominica gravatar imagealiciadominica ( 2012-12-03 04:27:28 -0500 )edit

You assign cvsize.width = 3 and cvsize.height = 3 before cvCalcOpticalFlowLK and than you use it like image size. You should add cvsize.width=src1->width; cvsize.height=src1->height; after cvCalcOpticalFlowLK.

Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2012-12-03 04:44:22 -0500 )edit

I am getting an unhandled exception when I do that.

aliciadominica gravatar imagealiciadominica ( 2012-12-03 04:49:31 -0500 )edit

replace y<cvsize.width with y<cvsize.height

Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2012-12-03 04:52:37 -0500 )edit

Thanks, I get the optical flow field now. Still no arrows to mark motion but at least now I know it works.

aliciadominica gravatar imagealiciadominica ( 2012-12-03 04:58:57 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2012-12-03 03:54:01 -0500

Seen: 1,148 times

Last updated: Dec 03 '12