3

I want to read video file(.avi or .mov) and detect motion and edges using Opencv.Can u help me with code?I want to create a GUI in which we can select the video file,then we can carry out image processing functions in opencv?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
user1237471
  • 31
  • 1
  • 1
  • 3
  • 1
    bro, its not going to be easy. You will still need to learn the basics yourself. –  Mar 30 '12 at 10:50
  • http://www.laganiere.name/opencvCookbook/ Check this book. Simple and concise and will help you getting started with OpenCV –  Mar 30 '12 at 10:51

4 Answers4

12

How to read a video file:

How to track/detect motion:

How to detect edges/countours:

And for more info on how to detect shapes, check this post.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

This works for me, I'm using AVIfiles. Call video withe filename, in your main-loop get the next frame and shutdown before terminating or changing to another video.

IplImage  *videoframe;
int videoFps;    
CvCapture *videoCapture=NULL;

int video(char *videoFile) {
    int       key;
    /* load the AVI file */
    videoCapture = cvCaptureFromAVI( videoFile );
    /* always check */
    if( !videoCapture )
        return 0;    
    /* get fps, needed to set the delay */
    videoFps = ( int )cvGetCaptureProperty( videoCapture, CV_CAP_PROP_FPS );
    /* display video */
    cvNamedWindow( "video", 0 );
}

void videoNext() {
        if ( ! videoCapture ) return;
        videoframe = cvQueryFrame( videoCapture );
        if( !videoframe ) return;
        cvShowImage( "video", videoframe );
        /* quit if user press 'q' */
        int key = cvWaitKey( 1000 / videoFps );
}

void videoShutdown() {
    /* free memory */
    cvReleaseCapture( &videoCapture );
    cvDestroyWindow( "video" );
    return;
}

Note: Opencv doesn't support audio playback On how to use ffmmpeg with opencv see audio-output-with-video-processing-with-opencv

Community
  • 1
  • 1
stacker
  • 68,052
  • 28
  • 140
  • 210
1

You should look at the samples included in the python folder for opencv. They will be found here: opencv\samples\python2

There you will find many of the basic and advanced features of opencv (in the cv2 format) demonstrated. There are also many tutorials (mainly in c++) on the website here: http://opencv.itseez.com/doc/tutorials/tutorials.html

Reading and writing video images is here: http://opencv.itseez.com/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html

For initial video capture from AVI:

import cv2
import cv2.cv as cv
import numpy as np

cap = cv2.VideoCapture(filename)
img = cv2.VideoCapture.read()
if img:
   print img.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
   print type(img)
# loop through rest of frames reading one at a time
Neon22
  • 590
  • 5
  • 17
  • Are you sure about that code? According to the doc, `cv2.VideoCapture.read()` returns a tuple: RetVal, Image. – LarsH Sep 26 '13 at 13:50
1

The shortest example for reading a frame from a video :

cap = cv::VideoCapture("foo.avi");
frame = cv::Mat;
cap >> frame;
Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53