Ask Your Question

Code not working, not sure why

asked 2012-12-17 02:40:09 -0500

aliciadominica gravatar image

updated 2012-12-17 02:57:13 -0500

Vladislav Vinogradov gravatar image

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.");

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);

src2m = cvQueryFrame(cap);

Mat frame(src2);

cvtColor(frame, gray, CV_BGR2GRAY);
if (
    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 =<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);


return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-12-17 02:59:12 -0500

Vladislav Vinogradov gravatar image

updated 2012-12-17 03:00:37 -0500

You should call waitKey with some delay after imshow:

imshow("flow", cflow);

This function fetch and handle window events including PAINT event.

edit flag offensive delete link more



Yeah that solved it :) Thanks

aliciadominica gravatar imagealiciadominica ( 2012-12-17 03:11:58 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-12-17 02:40:09 -0500

Seen: 285 times

Last updated: Dec 17 '12