简体   繁体   中英

Video files in opencv

我想读取视频文件(.avi或.mov)并使用Opencv检测运动和边缘。可以帮我提供代码吗?我想创建一个GUI,我们可以在其中选择视频文件,然后我们就可以进行图像处理了opencv中的函数?

This works for me, I'm using AVI files. 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

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

The shortest example for reading a frame from a video :

cap = cv::VideoCapture("foo.avi");
frame = cv::Mat;
cap >> frame;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM