]> rtime.felk.cvut.cz Git - opencv.git/commitdiff
added C++ & Python demos for the Farneback's optical flow
authorvp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Thu, 5 Nov 2009 21:41:09 +0000 (21:41 +0000)
committervp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Thu, 5 Nov 2009 21:41:09 +0000 (21:41 +0000)
git-svn-id: https://code.ros.org/svn/opencv/trunk@2271 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08

opencv/include/opencv/cv.h
opencv/interfaces/python/CMakeLists.txt
opencv/interfaces/python/api
opencv/interfaces/python/cv.cpp
opencv/samples/c/CMakeLists.txt
opencv/samples/c/fback.cpp [new file with mode: 0644]
opencv/samples/python/fback.py [new file with mode: 0644]
opencv/src/cv/cvoptflowgf.cpp

index 2965aa1109816b25f9638242a28249869173bd3f..84020bb5a13d0caff3d6ffd1f6fb2a150c41bb30 100644 (file)
@@ -518,6 +518,12 @@ CVAPI(void)  cvCalcAffineFlowPyrLK( const CvArr*  prev, const CvArr*  curr,
 CVAPI(int)  cvEstimateRigidTransform( const CvArr* A, const CvArr* B,
                                       CvMat* M, int full_affine );
 
+/* Estimate optical flow for each pixel using the two-frame G. Farneback algorithm */
+CVAPI(void) cvCalcOpticalFlowFarneback( const CvArr* prev, const CvArr* next,
+                                        CvArr* flow, double pyr_scale, int levels,
+                                        int winsize, int iterations, int poly_n,
+                                        double poly_sigma, int flags );
+
 /********************************* motion templates *************************************/
 
 /****************************************************************************************\
index d56669c6c54622ce28ad1cda5735ea7e5cf35aef..813a2bd2597c3623371de081ec871ecb05fc2b21 100644 (file)
@@ -22,7 +22,7 @@ add_custom_command(
 
 set(the_target "cvpy")
 add_library(${the_target} SHARED ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${CMAKE_CURRENT_BINARY_DIR}/generated0.i)
-target_link_libraries(${the_target} ${PYTHON_LIBRARIES} cxcore cv highgui)
+target_link_libraries(${the_target} ${PYTHON_LIBRARIES} cxcore cv cvaux highgui)
 set_target_properties(${the_target} PROPERTIES PREFIX "")
 set_target_properties(${the_target} PROPERTIES OUTPUT_NAME "cv")
 if(WIN32)
index 3d75d552154ae5c4eef997084c824fed3a483bdd..d3569b2e590afec1abc4c6fc4b32ce54e2e07328 100644 (file)
@@ -1452,6 +1452,17 @@ CalcOpticalFlowHS
   CvArr vely        /ch_vel_64
   double lambda
   CvTermCriteria criteria
+CalcOpticalFlowFarneback
+  CvArr prev /ch_image8
+  CvArr curr /ch_image8
+  CvArr flow
+  double pyr_scale 0.5
+  int levels 3
+  int winsize 15
+  int iterations 3
+  int poly_n 7
+  double poly_sigma 1.5
+  int flags 0
 
 # Highgui
 NamedWindow
index 4d4f52b1684d8f3f5b24c25d4ce7b78f4e040d1e..72878c85b271f2039f41a7bd14564facf5c4ce70 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <opencv/cxcore.h>
 #include <opencv/cv.h>
+#include <opencv/cvaux.h>
 #include <opencv/cvwimage.h>
 #include <opencv/highgui.h>
 
@@ -3391,6 +3392,30 @@ static PyObject *pycvGetMinMaxHistValue(PyObject *self, PyObject *args, PyObject
   return Py_BuildValue("ffNN", min_val, max_val, pminloc, pmaxloc);
 }
 
+/*static PyObject *pycvGetMinMaxHistValue(PyObject *self, PyObject *args, PyObject *kw)
+{
+  CvHistogram* hist;
+  PyObject *pyobj_hist = NULL;
+  float min_val;
+  float max_val;
+  int min_loc[CV_MAX_DIM];
+  int max_loc[CV_MAX_DIM];
+
+  if (!PyArg_ParseTuple(args, "O", &pyobj_hist))
+    return NULL;
+  if (!convert_to_CvHistogram(pyobj_hist, &hist, "hist")) return NULL;
+  ERRWRAP(cvGetMinMaxHistValue(hist, &min_val, &max_val, min_loc, max_loc));
+  int d = cvGetDims(hist->bins);
+  PyObject *pminloc = PyTuple_New(d), *pmaxloc = PyTuple_New(d);
+  for (int i = 0; i < d; i++) {
+    PyTuple_SetItem(pminloc, i, PyInt_FromLong(min_loc[i]));
+    PyTuple_SetItem(pmaxloc, i, PyInt_FromLong(max_loc[i]));
+  }
+  return Py_BuildValue("ffNN", min_val, max_val, pminloc, pmaxloc);
+}*/
+
+
+
 static int zero = 0;
 
 /************************************************************************/
@@ -3429,6 +3454,10 @@ static PyMethodDef methods[] = {
   {"GetHuMoments", (PyCFunction)pycvGetHuMoments, METH_KEYWORDS, "GetHuMoments(cvmoments) -> (h1, h2, h3, h4, h5, h5, h7)"},
   {"GetMinMaxHistValue", (PyCFunction)pycvGetMinMaxHistValue, METH_KEYWORDS, "GetMinMaxHistValue(hist) -> min_val,max_val,min_loc,max_loc"},
   {"WaitKey", (PyCFunction)pycvWaitKey, METH_KEYWORDS, "WaitKey(delay=0) -> int"},
+  //{"CalcOpticalFlowFarneback", (PyCFunction)pycvCalcOpticalFlowFarneback, METH_KEYWORDS, "CalcOpticalFlowFarneback(prev, next, flow, pyr_scale=0.5, levels=3, win_size=15, iterations=3, poly_n=7, poly_sigma=1.5, flags=0) -> None"},
+  //{"_HOGComputeDescriptors", (PyCFunction)pycvHOGComputeDescriptors, METH_KEYWORDS, "_HOGComputeDescriptors(image, win_stride=block_stride, locations=None, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_descriptors"},
+  //{"_HOGDetect", (PyCFunction)pycvHOGDetect, METH_KEYWORDS, "_HOGDetect(image, svm_classifier, win_stride=block_stride, locations=None, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_points"},
+  //{"_HOGDetectMultiScale", (PyCFunction)pycvHOGDetectMultiScale, METH_KEYWORDS, "_HOGDetectMultiScale(image, svm_classifier, win_stride=block_stride, scale=1.05, group_threshold=2, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_points"},
 
   {"temp_test", temp_test, METH_VARARGS},
 
index 5da3bf21e28b7e936c192c2e0ce3b0877ae7e2d6..27ce9e18bc48477f0eff6f3e17739d8d9f7c28ca 100644 (file)
@@ -41,6 +41,7 @@ if (BUILD_EXAMPLES)
     MY_DEFINE_EXAMPLE(edge                             edge.c)
     MY_DEFINE_EXAMPLE(facedetect               facedetect.cpp)
     MY_DEFINE_EXAMPLE(ffilldemo                        ffilldemo.c)
+    MY_DEFINE_EXAMPLE(fback                        fback.cpp)
     MY_DEFINE_EXAMPLE(find_obj                 find_obj.cpp)
     MY_DEFINE_EXAMPLE(fitellipse               fitellipse.cpp)
     MY_DEFINE_EXAMPLE(houghlines               houghlines.c)
diff --git a/opencv/samples/c/fback.cpp b/opencv/samples/c/fback.cpp
new file mode 100644 (file)
index 0000000..a6d0788
--- /dev/null
@@ -0,0 +1,48 @@
+#undef _GLIBCXX_DEBUG
+
+#include "cv.h"
+#include "highgui.h"
+
+using namespace cv;
+
+void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
+                    double scale, const Scalar& color)
+{
+    for(int y = 0; y < cflowmap.rows; y += step)
+        for(int x = 0; x < cflowmap.cols; x += step)
+        {
+            const Point2f& fxy = flow.at<Point2f>(y, x);
+            line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
+                 color);
+            circle(cflowmap, Point(x,y), 2, color, -1);
+        }
+}
+
+int main(int argc, char** argv)
+{
+    VideoCapture cap(0);
+    
+    if( !cap.isOpened() )
+        return -1;
+    
+    Mat prevgray, gray, flow, cflow, frame;
+    namedWindow("flow", 1);
+    
+    for(;;)
+    {
+        cap >> frame;
+        cvtColor(frame, gray, CV_BGR2GRAY);
+        
+        if( prevgray.data )
+        {
+            calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
+            cvtColor(prevgray, cflow, CV_GRAY2BGR);
+            drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
+            imshow("flow", cflow);
+        }
+        if(waitKey(30)>=0)
+            break;
+        std::swap(prevgray, gray);
+    }
+    return 0;
+}
diff --git a/opencv/samples/python/fback.py b/opencv/samples/python/fback.py
new file mode 100644 (file)
index 0000000..f11a70f
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+from cv import *
+
+class FBackDemo:
+    def __init__(self):
+        self.capture = CaptureFromCAM(0)
+        self.mv_step = 16
+        self.mv_scale = 1.5
+        self.mv_color = (0, 255, 0)
+        self.cflow = None
+        self.flow = None
+        
+        NamedWindow( "Optical Flow", 1 )
+
+        print( "Press ESC - quit the program\n" )
+
+    def draw_flow(self, flow, prevgray):
+        """ Returns a nice representation of a hue histogram """
+
+        CvtColor(prevgray, self.cflow, CV_GRAY2BGR)
+        for y in range(0, flow.height, self.mv_step):
+            for x in range(0, flow.width, self.mv_step):
+                fx, fy = flow[y, x]
+                Line(self.cflow, (x,y), (x+fx,y+fy), self.mv_color)
+                Circle(self.cflow, (x,y), 2, self.mv_color, -1)
+        ShowImage("Optical Flow", self.cflow)
+
+    def run(self):
+        first_frame = True
+        
+        while True:
+            frame = QueryFrame( self.capture )
+
+            if first_frame:
+                gray = CreateImage(GetSize(frame), 8, 1)
+                prev_gray = CreateImage(GetSize(frame), 8, 1)
+                flow = CreateImage(GetSize(frame), 32, 2)
+                self.cflow = CreateImage(GetSize(frame), 8, 3)
+                
+            CvtColor(frame, gray, CV_BGR2GRAY)
+            if not first_frame:
+                CalcOpticalFlowFarneback(prev_gray, gray, flow,
+                    pyr_scale=0.5, levels=3, winsize=15,
+                    iterations=3, poly_n=5, poly_sigma=1.2, flags=0)
+                self.draw_flow(flow, prev_gray)
+                c = WaitKey(7)
+                if c == ord("q"):
+                    break
+            prev_gray, gray = gray, prev_gray        
+            first_frame = False
+
+if __name__=="__main__":
+    demo = FBackDemo()
+    demo.run()
index 1e9e6987362added7d20ce3d8b81dbedc1090162..43f95941f022314ab7ed3c85ea026f1d270fb99f 100644 (file)
@@ -565,7 +565,7 @@ void calcOpticalFlowFarneback( const Mat& prev0, const Mat& next0,
     Mat prevFlow, flow;
 
     CV_Assert( prev0.size() == next0.size() && prev0.channels() == next0.channels() &&
-        prev0.channels() == 1 );
+        prev0.channels() == 1 && pyr_scale < 1 );
     flow0.create( prev0.size(), CV_32FC2 );
 
     for( k = 0, scale = 1; k < levels; k++ )
@@ -634,3 +634,17 @@ void calcOpticalFlowFarneback( const Mat& prev0, const Mat& next0,
 }
 
 }
+
+CV_IMPL void cvCalcOpticalFlowFarneback(
+            const CvArr* _prev, const CvArr* _next,
+            CvArr* _flow, double pyr_scale, int levels,
+            int winsize, int iterations, int poly_n,
+            double poly_sigma, int flags )
+{
+    cv::Mat prev = cv::cvarrToMat(_prev), next = cv::cvarrToMat(_next);
+    cv::Mat flow = cv::cvarrToMat(_flow);
+    CV_Assert( flow.size() == prev.size() && flow.type() == CV_32FC2 );
+    cv::calcOpticalFlowFarneback( prev, next, flow, pyr_scale, levels,
+        winsize, iterations, poly_n, poly_sigma, flags );
+}
+