我想读取视频文件(.avi 或 .mov)并使用 Opencv 检测运动和边缘。你能帮我写代码吗?我想创建一个 GUI,我们可以在其中选择视频文件,然后我们可以进行图像处理opencv中的函数?
问问题
13423 次
4 回答
12
如何读取视频文件:
- 读取视频文件并将其显示在窗口上(C API)
- 读取视频文件并将其显示在窗口上(C++ API)
- 读取视频文件,将其转换为灰度,然后在窗口上显示(C API)
如何跟踪/检测运动:
如何检测边缘/计数:
有关如何检测形状的更多信息,请查看这篇文章。
于 2012-03-30T13:48:46.467 回答
2
这对我有用,我正在使用AVI
文件。使用文件名调用视频,在您的主循环中获取下一帧并在终止或更改为另一个视频之前关闭。
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;
}
注意:Opencv 不支持音频播放关于如何将 ffmmpeg 与 opencv 一起使用,请参阅audio-output-with-video-processing-with-opencv
于 2012-03-30T06:01:17.500 回答
1
您应该查看 opencv 的 python 文件夹中包含的示例。它们可以在这里找到:opencv\samples\python2
在那里你会发现 opencv 的许多基本和高级特性(以 cv2 格式)演示。这里的网站上也有很多教程(主要是c++):http: //opencv.itseez.com/doc/tutorials/tutorials.html
读写视频图像在这里: http: //opencv.itseez.com/trunk/modules/highgui/doc/reading_and_writing_images_and_video.html
对于 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
于 2012-04-04T22:37:16.500 回答
1
从视频中读取帧的最短示例:
cap = cv::VideoCapture("foo.avi");
frame = cv::Mat;
cap >> frame;
于 2012-03-30T06:25:37.093 回答