基于OpenCV的視頻轉(zhuǎn)為圖像序列方法:
基于C++版本
#include <iostream>#include "cv.h"#include "opencv2/opencv.hpp"using namespace std;using namespace cv;void main(){ VideoCapture cap("C://Users//Leo//Desktop//Megamind.avi"); if ( !cap.isOpened() ) { return ; } int imgIndex(0); for ( ; ; ) { Mat frame; cap >> frame; if ( frame.empty() ) { break; } char* imageSaveName = new char[64]; sprintf( imageSaveName, "C://Users//Leo//Desktop//new//%05d.jpg", imgIndex ); imwrite( imageSaveName, frame ); delete[] imageSaveName; imgIndex++; } cout << "total frames: " << imgIndex << endl;}基于C版本
#include <iostream>#include "cv.h"#include "opencv2/opencv.hpp"using namespace std;using namespace cv;void main(){ // video read CvCapture *capture = cvCreateFileCapture("C://Users//Leo//Desktop//Megamind.avi"); IplImage *frame; int imgIndex(0); while(1) { frame = cvQueryFrame(capture); if ( !frame ) { break; } char* imageSaveName = new char[64]; sprintf( imageSaveName, "C://Users//Leo//Desktop//new//%05d.jpg", imgIndex ); cvSaveImage( imageSaveName, frame ); delete[] imageSaveName; imgIndex++; } cout << "total frames: " << imgIndex << endl; cvDestroyWindow( "VideoImage" ); cvReleaseCapture( &capture ); cvReleaseImage( &frame );}測(cè)試數(shù)據(jù)為OpenCV自帶的視頻:Megamind.avi,可以在opencv/sources/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video路徑下查找,共270幀圖像,運(yùn)行結(jié)果部分截圖如下:

以上這篇Opencv 視頻轉(zhuǎn)為圖像序列的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注