CalcOpticalFlowBM function in OpenCv

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 23
Reputation: stonerain is an unknown quantity at this point 
Solved Threads: 2
stonerain stonerain is offline Offline
Newbie Poster

CalcOpticalFlowBM function in OpenCv

 
0
  #1
Dec 27th, 2008
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 .

  1. /* Create an object that decodes the input video stream. */
  2. CvCapture * input_video = cvCaptureFromFile(
  3. "optical_flow_input.avi");
  4. if (input_video == NULL)
  5. {
  6. fprintf(stderr, "Error: Can't open video.\n");
  7. return -1;
  8. }
  9.  
  10. CvSize frame_size;
  11. frame_size.height =
  12. (int) cvGetCaptureProperty
  13. ( input_video, CV_CAP_PROP_FRAME_HEIGHT );
  14. frame_size.width =
  15. (int) cvGetCaptureProperty
  16. ( input_video, CV_CAP_PROP_FRAME_WIDTH );
  17.  
  18. /* Determine the number of frames in the AVI. */
  19. long number_of_frames;
  20. /* Go to the end of the AVI (ie: the fraction is "1") */
  21. cvSetCaptureProperty
  22. (input_video, CV_CAP_PROP_POS_AVI_RATIO, 1.);
  23. /* Now that we're at the end, read the AVI position in frames */
  24. number_of_frames = ( int )
  25. cvGetCaptureProperty (input_video, CV_CAP_PROP_POS_FRAMES);
  26. /* Return to the beginning */
  27. cvSetCaptureProperty
  28. (input_video, CV_CAP_PROP_POS_FRAMES, 0.);
  29.  
  30. IplImage * source[ 2 ], * image[ 2 ];
  31. source[ 0 ] = cvCreateImage (frame_size, IPL_DEPTH_8U, 1);
  32. source[ 1 ] = cvCreateImage (frame_size, IPL_DEPTH_8U, 1);
  33. image[ 0 ] = cvCreateImage(frame_size, IPL_DEPTH_8U, 3);
  34. image[ 1 ] = cvCreateImage(frame_size, IPL_DEPTH_8U, 3);
  35.  
  36. int current_frame = 300;
  37. cvSetCaptureProperty
  38. ( input_video, CV_CAP_PROP_POS_FRAMES, current_frame);
  39.  
  40. IplImage * frame = cvQueryFrame( input_video );
  41. if (frame == NULL) {
  42. /* Why did we get a NULL frame? We shouldn't be at the end. */
  43. fprintf(stderr,
  44. "Error: Hmm. The end came sooner than we thought.\n");
  45. return -1;
  46. }
  47.  
  48. cvConvertImage(frame, image[ 0 ], CV_CVTIMG_FLIP);
  49. ++current_frame;
  50. cvSetCaptureProperty
  51. ( input_video, CV_CAP_PROP_POS_FRAMES, current_frame);
  52.  
  53. frame = cvQueryFrame( input_video );
  54. if (frame == NULL) {
  55. fprintf(stderr,
  56. "Error: Hmm. The end came sooner than we thought.\n");
  57. return -1;
  58. }
  59.  
  60. cvConvertImage(frame, image[ 1 ], CV_CVTIMG_FLIP);
  61. // cvNamedWindow ("frm", CV_WINDOW_AUTOSIZE);
  62. // cvShowImage ("frm", image[ 0 ]);
  63.  
  64. cvCvtColor (image[ 0 ], source[ 0 ], CV_BGR2GRAY);
  65. cvCvtColor (image[ 1 ], source[ 1 ], CV_BGR2GRAY);
  66.  
  67. cvNamedWindow("Pic1", CV_WINDOW_AUTOSIZE);
  68. cvShowImage("Pic1", source[ 0 ]);
  69.  
  70. cvNamedWindow("Pic2", CV_WINDOW_AUTOSIZE);
  71. cvShowImage("Pic2", source[ 1 ]);
  72.  
  73. CvSize size;
  74. size = cvGetSize( source[ 0 ] );
  75.  
  76. size.height /= 16;
  77. size.width /= 16;
  78.  
  79. IplImage * velocityX = cvCreateImage(size, IPL_DEPTH_32F, 1),
  80. * velocityY = cvCreateImage(size, IPL_DEPTH_32F, 1);
  81.  
  82. cvCalcOpticalFlowBM (source[ 0 ], source[ 1 ],
  83. cvSize(16, 16), cvSize(1, 1), cvSize(3, 2), 0,
  84. velocityX, velocityY);
  85.  
  86.  
  87. cvNamedWindow ("Horizontal", CV_WINDOW_AUTOSIZE);
  88. cvShowImage ("Horizontal", velocityX);
  89.  
  90. cout << velocityX->height;
  91. cout << endl << velocityX->width;
  92.  
  93. cvNamedWindow ("Vertical", CV_WINDOW_AUTOSIZE);
  94. cvShowImage ("Vertical", velocityY);
  95.  
  96. cvWaitKey( 0 );
  97.  
  98. cvDestroyWindow( "Pic1" );
  99. cvDestroyWindow( "Pic2" );
  100. cvDestroyWindow( "Horizontal" );
  101. cvDestroyWindow( "Vertical" );
  102.  
  103. cvReleaseImage( &source[ 1 ] );
  104. cvReleaseImage( &source[ 0 ] );
  105. cvReleaseImage( &image[ 1 ] );
  106. cvReleaseImage( &image[ 0 ] );
  107. cvReleaseImage( &velocityY );
  108. cvReleaseImage( &velocityX );
A.K.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #2
Dec 28th, 2008
I googled the function name and came up with this description:

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)
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: stonerain is an unknown quantity at this point 
Solved Threads: 2
stonerain stonerain is offline Offline
Newbie Poster

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #3
Dec 28th, 2008
Thank you for your answer but actually I want to learn how I can calculate the blocks move from the velX and velY. This is my problem. Thank you a lot...
A.K.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #4
Dec 28th, 2008
That sounds like basic Geometry or Physics, but let's play pretend.

Pretend you're writing a function to calculate what you want. Write a prototype for the function. (What arguments will it take, what will it return?)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: stonerain is an unknown quantity at this point 
Solved Threads: 2
stonerain stonerain is offline Offline
Newbie Poster

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #5
Dec 28th, 2008
I have no idea. Do you have? I really need some help...
A.K.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #6
Dec 28th, 2008
I was trying to get you to explain what it was you thought you needed. If you don't know what you need, how will you know when you find it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: stonerain is an unknown quantity at this point 
Solved Threads: 2
stonerain stonerain is offline Offline
Newbie Poster

Re: CalcOpticalFlowBM function in OpenCv

 
0
  #7
Dec 29th, 2008
I need a number which shows us to how many pixels a block move in x and y coordinates? But I can't fetch this numbers from velX and velY
A.K.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC