#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
char image_name[30];
int i=1;
int main()
{
VideoCapture cap;
//cap.open(0); //打开摄像头
cap.open("33.mp4"); //打开视频
int frameRate = static_cast<int>(cap.get(CV_CAP_PROP_FPS)); //帧率 x frames/s
int totalFrames = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_COUNT)); //总帧数
int width = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH)); //帧宽度
int height = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT)); //帧高度
int frameToStart = static_cast<int>(cap.get(CV_CAP_PROP_POS_FRAMES)); //帧开始
cout << "视频宽度=" << width << endl;
cout << "视频高度=" << height << endl;
cout << "视频总帧数=" << totalFrames << endl;
cout << "帧率=" << frameRate << endl;
cout << "从第" << frameToStart << "帧开始读" << endl;
Mat img, imgGray;
int c = 0;
int framenum=frameToStart;
while (c !=27)
{
cap >> img;
framenum++;
if (img.channels() == 3) //三通道红绿蓝,彩色图像
{
cvtColor(img, imgGray, CV_RGB2GRAY); //变成灰色图像
} else {
imgGray = img;
}
cout << "正在读取第" << framenum << "帧" << endl;
if ( framenum % 2 == 0 ) //此处为帧数间隔,修改这里就可以了
{
cout << "正在写第" << framenum << "帧" << endl;
//sprintf(image_name, "%s%d%s", "f", framenum, ".jpg");
sprintf(image_name, "%s%d%s", "f", ++i, ".jpg");
imwrite("face\\" + string(image_name), imgGray);
}
waitKey(1000); //每隔三秒显示
//imag = imread("mv3.jpg",16);
//result = img.clone();
//threshold(img, result, 100, 250, THRESH_OTSU);
//imwrite(i + "hd.jpg",imgGray);
}
return 0;
}
按顺序读取视频
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
char image_name[30];
int i=1;
int main()
{
VideoCapture cap;
//cap.open(0); //打开摄像头
char filename[200];
for(unsigned int k=1;k<200;k++)
{
sprintf(filename,"%d.mp4",k);
cap.open(filename); //打开视频
int frameRate = static_cast<int>(cap.get(CV_CAP_PROP_FPS)); //帧率 x frames/s
int totalFrames = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_COUNT)); //总帧数
int width = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH)); //帧宽度
int height = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT)); //帧高度
int frameToStart = static_cast<int>(cap.get(CV_CAP_PROP_POS_FRAMES)); //帧开始
cout << "视频宽度=" << width << endl;
cout << "视频高度=" << height << endl;
cout << "视频总帧数=" << totalFrames << endl;
cout << "帧率=" << frameRate << endl;
cout << "从第" << frameToStart << "帧开始读" << endl;
Mat img, imgGray;
int c = 0;
int framenum=frameToStart;
//while (c !=27)
//while (framenum !=60)
for(framenum;framenum<60;framenum++)
{
cap >> img;
//framenum++;
if (img.channels() == 3) //三通道红绿蓝,彩色图像
{
cvtColor(img, imgGray, CV_RGB2GRAY); //变成灰色图像
} else {
imgGray = img;
}
cout << "正在读取第" << framenum << "帧" << endl;
if ( framenum % 2 == 0 ) //此处为帧数间隔,修改这里就可以了
{
sprintf(image_name, "%s%d%s", "1", ++i, ".jpg");
imwrite("face\\" + string(image_name), imgGray);
}
waitKey(10); //每隔三秒显示
}
//namedWindow("face", 0);
//imshow("face",imgGray);
//return 0;
}
}
g++ -o hd hd.cpp `pkg-config --cflags --libs opencv`