Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Code not working, not sure why

Hello, I'm trying to implement an Optical Flow Farneback Project that takes random frames of the video and matches them. But for some reason I can't seem to load the frames, it doesn't give me error, but it also doesn't show the images nor it does the computations. Could you take a look at the code and tell me what's wrong? I'm trying to take the first two consequent frames from the video at this point and then I will change the code to take random points when I get the code working. I'm not sure if the problem is with the part that loads the video or with the optical flow code. Code is below. Thanks in advance.

    CvCapture* cap = cvCreateFileCapture("C:\\Users\\Elvan\\Documents\\Panic1000people.mpeg");
          if ( cap == NULL ) 
  {
  fprintf(stderr, "Error: Couldn't open image.");
  system("PAUSE");
  exit(-1);
  }

    int height    = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
    int width    = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
    Mat prevgray, gray, flow, cflow;
        namedWindow("flow", 1);
    IplImage *src1, *src2;


    src1= cvQueryFrame(cap);

    Mat frameprev(src1);
    cvtColor(frameprev, prevgray, CV_BGR2GRAY);

    src2= cvQueryFrame(cap);

    Mat frame(src2);


    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);
        //Draw the optical flow field.
            for(int y = 0; y < cflow.rows; y += step)
            {
                for(int x = 0; x < cflow.cols; x += step)
                {
                    const Point2f& fxy = flow.at<Point2f>(y, x);
                    line(cflow, Point(x,y), Point(cvRound(x+fxy.x),       cvRound(y+fxy.y)), CV_RGB(0, 255, 0),1);
                    circle(cflow, Point(x,y), 0, CV_RGB(0, 255, 0), -1);
                    fprintf(optflow,"%10f %10f ||",fxy.x,fxy.y);
                }
                imshow("flow", cflow);

            }
                //imshow("image",gray);
            fprintf(optflow,"\n\n");
        }

    system("PAUSE");

    return 0;
click to hide/show revision 2
Removed wirte to file part of the code since it's irrelevant.

Code not working, not sure why

Hello, I'm trying to implement an Optical Flow Farneback Project that takes random frames of the video and matches them. But for some reason I can't seem to load the frames, it doesn't give me error, but it also doesn't show the images nor it does the computations. Could you take a look at the code and tell me what's wrong? I'm trying to take the first two consequent frames from the video at this point and then I will change the code to take random points when I get the code working. I'm not sure if the problem is with the part that loads the video or with the optical flow code. Code is below. Thanks in advance.

 

CvCapture* cap = cvCreateFileCapture("C:\\Users\\Elvan\\Documents\\Panic1000people.mpeg"); cvCreateFileCapture("C:\Users\Elvan\Documents\Panic1000people.mpeg"); if ( cap == NULL ) { fprintf(stderr, "Error: Couldn't open image."); system("PAUSE"); exit(-1); } }

    int height    = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
    int width    = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
    Mat prevgray, gray, flow, cflow;
        namedWindow("flow", 1);
    IplImage *src1, *src2;


    src1= cvQueryFrame(cap);

    Mat frameprev(src1);
    cvtColor(frameprev, prevgray, CV_BGR2GRAY);

    src2= cvQueryFrame(cap);

    Mat frame(src2);


    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);
        //Draw the optical flow field.
            for(int y = 0; y < cflow.rows; y += step)
            {
                for(int x = 0; x < cflow.cols; x += step)
                {
                    const Point2f& fxy = flow.at<Point2f>(y, x);
                    line(cflow, Point(x,y), Point(cvRound(x+fxy.x),       cvRound(y+fxy.y)), CV_RGB(0, 255, 0),1);
                    circle(cflow, Point(x,y), 0, CV_RGB(0, 255, 0), -1);
                    fprintf(optflow,"%10f %10f ||",fxy.x,fxy.y);
                 }
                imshow("flow", cflow);

            }
                //imshow("image",gray);
            fprintf(optflow,"\n\n");
         }

    system("PAUSE");

    return 0;
click to hide/show revision 3
No.3 Revision

Code not working, not sure why

Hello, I'm trying to implement an Optical Flow Farneback Project that takes random frames of the video and matches them. But for some reason I can't seem to load the frames, it doesn't give me error, but it also doesn't show the images nor it does the computations. Could you take a look at the code and tell me what's wrong? I'm trying to take the first two consequent frames from the video at this point and then I will change the code to take random points when I get the code working. I'm not sure if the problem is with the part that loads the video or with the optical flow code. Code is below. Thanks in advance.

CvCapture* cap = cvCreateFileCapture("C:\Users\Elvan\Documents\Panic1000people.mpeg");
              cvCreateFileCapture("C:\\Users\\Elvan\\Documents\\Panic1000people.mpeg");
if ( cap (cap == NULL ) 
      NULL) 
{
     fprintf(stderr, "Error: Couldn't open image.");
     system("PAUSE");
     exit(-1);
      }

 }

int height  = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
 int width   = (int) cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
  Mat prevgray, gray, flow, cflow;
 namedWindow("flow", 1);
 IplImage *src1, *src2;


    src1= src1 = cvQueryFrame(cap);

 Mat frameprev(src1);
 cvtColor(frameprev, prevgray, CV_BGR2GRAY);

    src2= src2m = cvQueryFrame(cap);

 Mat frame(src2);

 cvtColor(frame, gray, CV_BGR2GRAY);
     if( prevgray.data )
        {   

        if (prevgray.data)
{
    calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
     cvtColor(prevgray, cflow, CV_GRAY2BGR);
     //Draw the optical flow field.
     for(int y = 0; y < cflow.rows; y += step)
     {
         for(int x = 0; x < cflow.cols; x += step)
         {
             const Point2f& fxy = flow.at<Point2f>(y, x);
             line(cflow, Point(x,y), Point(cvRound(x+fxy.x),       cvRound(y+fxy.y)), CV_RGB(0, 255, 0),1);
             circle(cflow, Point(x,y), 0, CV_RGB(0, 255, 0), -1);
         }
         imshow("flow", cflow);
     }
                //imshow("image",gray);

        //imshow("image",gray); 
}

 system("PAUSE");

 return 0;