使用opencv:从视频中获取每一帧图片
★ C实现
当前目录:~/test/opencv/getframe
,在此目录中创建getframe.cpp
和CMakeLists.txt
- getframe.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
using namespace cv;
int main(int argc, char** argv ) {
if ( argc != 2 ) {
printf("usage: ./getframe <Video_Path>\n");
return -1;
}
CvCapture* cvCap = cvCaptureFromFile(argv[1]);// argv[1] is video name
const char *winName = "Show Frames";
cvNamedWindow(winName,CV_WINDOW_AUTOSIZE);
// output directory
char dirName[512] = {0};
char *p = strrchr(argv[1], '/');
strcpy(dirName, p+1);
p = strchr(dirName, '.');
*p = '_';
printf("output dir: %s\n", dirName);
struct stat dirInfo = {0};
int statRet = stat(dirName, &dirInfo);
if (statRet == -1 && errno == ENOENT) {
int ret = mkdir(dirName, 0775);
if (ret == 0) {
printf("mkdir success: %s\n", dirName);
} else {
printf("mkdir failed: %s\n", dirName);
return -1;
}
}
printf("press any key to stop.\n");
int id = 1;
while(1) {
IplImage* img = cvQueryFrame(cvCap);
if (img == NULL) {
printf("No more frames, press any key to exit.\n");
break;
}
cvShowImage(winName, img);
char name[512] = {0};
sprintf(name, "./%s/%04d.jpg", dirName, id);
cvSaveImage(name, img);
id++;
int key = cvWaitKey(1000/33);// 30 frame/second for displaying in window
}
cvReleaseCapture(&cvCap);
waitKey(0);
cvDestroyWindow(winName);
return 0;
}
- CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( getframe )
find_package( OpenCV REQUIRED )
add_executable( getframe getframe.cpp )
target_link_libraries( getframe ${OpenCV_LIBS} )
编译:(当前目录:~/test/opencv/getframe
)
mkdir build
cd build
cmake ..
make
生成getframe可执行程序。
运行:
./getframe ../Megamind.avi
在build目录中的Megamind_avi
目录中是获取的视频的每一帧图片。
★ C++实现
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
using namespace cv;
int main(int argc, char** argv ) {
if ( argc != 2 ) {
printf("usage: getframe <Video_Path>\n");
return -1;
}
VideoCapture cap(argv[1]);
if(!cap.isOpened()) {
printf("video is not ready\n");
return -1;
}
// output directory
char dirName[512] = {0};
char *p = strrchr(argv[1], '/');
strcpy(dirName, p+1);
p = strchr(dirName, '.');
*p = '_';
printf("output dir: %s\n", dirName);
struct stat dirInfo = {0};
int statRet = stat(dirName, &dirInfo);
if (statRet == -1 && errno == ENOENT) {
int ret = mkdir(dirName, 0775);
if (ret == 0) {
printf("mkdir success: %s\n", dirName);
} else {
printf("mkdir failed: %s\n", dirName);
return -1;
}
}
printf("press any key to stop.\n");
const char *winName = "Show Frames";
namedWindow(winName, 1);
Mat frame;
int id = 1;
while(1) {
cap.grab();
if (!cap.retrieve(frame)) {
printf("No more frames, press any key to exit.\n");
break;
}
char name[512] = {0};
sprintf(name, "%s/%04d.jpg", dirName, id);
imshow(winName, frame);
imwrite(name, frame);
id++;
if(waitKey(30) >= 0) break;
}
cap.release();
waitKey(0);
cvDestroyWindow(winName);
return 0;
}
★ 参考
https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html
python版的,可以参考:
https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html