Ask Your Question
0

cvCalcOpticalFlowLK issue

asked Dec 3 '12

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;
}
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Dec 3 '12

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)

Preview: (hide)

Comments

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

aliciadominica gravatar imagealiciadominica (Dec 3 '12)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.

I am getting an unhandled exception when I do that.

aliciadominica gravatar imagealiciadominica (Dec 3 '12)edit

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

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 (Dec 3 '12)edit

Question Tools

Stats

Asked: Dec 3 '12

Seen: 1,452 times

Last updated: Dec 03 '12