2011-08-08 48 views
2

請幫我完成上面的任務。我是新手openCV。我的系統中安裝了OpenCV 2.2,並使用VC++ 2010 Express作爲IDE。我沒有內置攝像頭在我的筆記本電腦... 只是我學會了如何加載圖像。我非常渴望從我的磁盤加載視頻文件(最好是mp4,flv格式),並希望使用openCV播放它。如何使用OpenCV從我的磁盤播放視頻文件。

+0

如果你可以編輯這個問題,表示您的任何企圖,隨意標記,讓管理員注意進行審查。 –

回答

1

使用OpenCV的接口(在Windows上對我來說工作更好),加載視頻文件的函數是cvCaptureFromAVI()。在此之後,你需要通過cvQueryFrame()使用傳統的循環來檢索幀,然後cvShowImage()cvNamedWindow()創建的窗口上顯示它們。

CvCapture *capture = cvCaptureFromAVI("video.avi"); 
if(!capture) 
{ 
    printf("!!! cvCaptureFromAVI failed (file not found?)\n"); 
    return -1; 
} 

int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); 
printf("* FPS: %d\n", fps); 

cvNamedWindow("display_video", CV_WINDOW_AUTOSIZE); 

IplImage* frame = NULL; 
char key = 0; 

while (key != 'q') 
{ 
    frame = cvQueryFrame(capture); 
    if (!frame) 
    { 
     printf("!!! cvQueryFrame failed: no frame\n"); 
     break; 
    } 

    cvShowImage("display_video", frame); 

    key = cvWaitKey(1000/fps); 
} 

cvReleaseCapture(&capture); 
cvDestroyWindow("display_video"); 

This blog post爲您正在嘗試完成的任務帶來一些額外信息。

+0

非常感謝KarlPhilip ,,,,上面的代碼工作正常....我是新來的OpenCV但想知道更多。請引導我從哪裏開始?我可以得到一些幫助文件,因爲我們使用得到MATLAB ..再次感謝... – Easyboy

+0

@mahesh http://stackoverflow.com/questions/5679909/looking-for-opencv-tutorial – karlphillip

0

(Hummm ......你似乎並沒有試圖通過自己做一些事,但無論如何)

docs

#include "opencv2/opencv.hpp" 

using namespace cv; 

int main(int, char**) 
{ 
    VideoCapture cap(0); // open the default camera 
    if(!cap.isOpened()) // check if we succeeded 
     return -1; 

    //Mat edges; 
    namedWindow("frames",1); 
    for(;;) 
    { 
     Mat frame; 
     cap >> frame; // get a new frame from camera 
     //ignore below sample, since you only want to play 
     //cvtColor(frame, edges, CV_BGR2GRAY); 
     //GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); 
     //Canny(edges, edges, 0, 30, 3); 
     //imshow("edges", edges); 
     imshow("frames", frame); 
     if(waitKey(30) >= 0) break; 
    } 
    // the camera will be deinitialized automatically in VideoCapture destructor 
    return 0; 
} 

這是使用OpenCV的1 old way。 x apis。

+0

的OP想的從視頻文件加載框架,而不是一個相機 – karlphillip