| | |
CalcOpticalFlowBM function in OpenCv
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 23
Reputation:
Solved Threads: 2
I am trying to use the CalcOpticalFlowBM function but I don't understand hot it works. I want to find a motion vector for every blocks but I don't understand that the CalcOpticalFlowBM yields velX and velY. I wrote below code .
C++ Syntax (Toggle Plain Text)
/* Create an object that decodes the input video stream. */ CvCapture * input_video = cvCaptureFromFile( "optical_flow_input.avi"); if (input_video == NULL) { fprintf(stderr, "Error: Can't open video.\n"); return -1; } CvSize frame_size; frame_size.height = (int) cvGetCaptureProperty ( input_video, CV_CAP_PROP_FRAME_HEIGHT ); frame_size.width = (int) cvGetCaptureProperty ( input_video, CV_CAP_PROP_FRAME_WIDTH ); /* Determine the number of frames in the AVI. */ long number_of_frames; /* Go to the end of the AVI (ie: the fraction is "1") */ cvSetCaptureProperty (input_video, CV_CAP_PROP_POS_AVI_RATIO, 1.); /* Now that we're at the end, read the AVI position in frames */ number_of_frames = ( int ) cvGetCaptureProperty (input_video, CV_CAP_PROP_POS_FRAMES); /* Return to the beginning */ cvSetCaptureProperty (input_video, CV_CAP_PROP_POS_FRAMES, 0.); IplImage * source[ 2 ], * image[ 2 ]; source[ 0 ] = cvCreateImage (frame_size, IPL_DEPTH_8U, 1); source[ 1 ] = cvCreateImage (frame_size, IPL_DEPTH_8U, 1); image[ 0 ] = cvCreateImage(frame_size, IPL_DEPTH_8U, 3); image[ 1 ] = cvCreateImage(frame_size, IPL_DEPTH_8U, 3); int current_frame = 300; cvSetCaptureProperty ( input_video, CV_CAP_PROP_POS_FRAMES, current_frame); IplImage * frame = cvQueryFrame( input_video ); if (frame == NULL) { /* Why did we get a NULL frame? We shouldn't be at the end. */ fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n"); return -1; } cvConvertImage(frame, image[ 0 ], CV_CVTIMG_FLIP); ++current_frame; cvSetCaptureProperty ( input_video, CV_CAP_PROP_POS_FRAMES, current_frame); frame = cvQueryFrame( input_video ); if (frame == NULL) { fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n"); return -1; } cvConvertImage(frame, image[ 1 ], CV_CVTIMG_FLIP); // cvNamedWindow ("frm", CV_WINDOW_AUTOSIZE); // cvShowImage ("frm", image[ 0 ]); cvCvtColor (image[ 0 ], source[ 0 ], CV_BGR2GRAY); cvCvtColor (image[ 1 ], source[ 1 ], CV_BGR2GRAY); cvNamedWindow("Pic1", CV_WINDOW_AUTOSIZE); cvShowImage("Pic1", source[ 0 ]); cvNamedWindow("Pic2", CV_WINDOW_AUTOSIZE); cvShowImage("Pic2", source[ 1 ]); CvSize size; size = cvGetSize( source[ 0 ] ); size.height /= 16; size.width /= 16; IplImage * velocityX = cvCreateImage(size, IPL_DEPTH_32F, 1), * velocityY = cvCreateImage(size, IPL_DEPTH_32F, 1); cvCalcOpticalFlowBM (source[ 0 ], source[ 1 ], cvSize(16, 16), cvSize(1, 1), cvSize(3, 2), 0, velocityX, velocityY); cvNamedWindow ("Horizontal", CV_WINDOW_AUTOSIZE); cvShowImage ("Horizontal", velocityX); cout << velocityX->height; cout << endl << velocityX->width; cvNamedWindow ("Vertical", CV_WINDOW_AUTOSIZE); cvShowImage ("Vertical", velocityY); cvWaitKey( 0 ); cvDestroyWindow( "Pic1" ); cvDestroyWindow( "Pic2" ); cvDestroyWindow( "Horizontal" ); cvDestroyWindow( "Vertical" ); cvReleaseImage( &source[ 1 ] ); cvReleaseImage( &source[ 0 ] ); cvReleaseImage( &image[ 1 ] ); cvReleaseImage( &image[ 0 ] ); cvReleaseImage( &velocityY ); cvReleaseImage( &velocityX );
A.K.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
I googled the function name and came up with this description:
So it would seem that the function is trying to measure motion between two pictures. It looks for matching blocks in each picture and returns the speed at which the block(s) were moving.
It is returning how far in x and how far in y the block moved. If you need a vector for the motion, you will probably have to calculate it from the velX and velY.
•
•
•
•
The function cvCalcOpticalFlowBM calculates optical flow for overlapped blocks blockSize.width×blockSize.height pixels each, thus the velocity fields are smaller than the original images. For every block in imgA the functions tries to find a similar block in imgB in some neighborhood of the original block or shifted by (velx(x0,y0),vely(x0,y0)) block as has been calculated by previous function call (if usePrevious=1)
It is returning how far in x and how far in y the block moved. If you need a vector for the motion, you will probably have to calculate it from the velX and velY.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How do I flush the input stream?
- Next Thread: Some Questions in C++
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





