环境安装
保存图片
#include <string>
#include <iostream>
#include <time.h>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
void Video_to_Image(Mat& frame);
int main()
{
//string filename = "Wildlife.wmv";
string filename = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
Mat frame;
VideoCapture cap;
cap.open(filename);
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
time_t start_time = time(NULL);
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
// 每隔2s保存图片
time_t end_time = time(NULL);
if ((end_time - start_time) >=2)
{
cout << "2s capture" << endl;
Video_to_Image(frame);
start_time = time(NULL);
}
if (waitKey(5) >= 0)
break;
}
cap.release();
return 0;
}
void Video_to_Image(Mat& frame)
{
char image_name[PATH_MAX];
sprintf(image_name, "%s%s", "test_image", ".jpg");
imwrite(image_name, frame);
}
获取实时视频
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <unistd.h>
using namespace cv;
int main(int argc, char** argv )
{
cv::VideoCapture cap("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov");
if (!cap.isOpened()) {
std::cout << "" << std::endl;
return -1;
}
cv::Mat frame;
int waitNum = 5;
do {
cap >> frame;
if (!frame.empty()) break;
std::cout << "NOT FRAME: wait " << waitNum << std::endl;
sleep(10); // linux Sleep
} while (--waitNum > 0);
cv::namedWindow("MYLAF RTSP");
while (true) {
cap >> frame;
if (frame.empty()) break;
cv::imshow("MYLAF RTSP", frame);
cv::waitKey(30);
}
cap.release();
waitKey(0);
return 0;
}
保存视频
·Opencv·保存摄像头视频为mp4和flv两种格式
由于我采用的是opencv4.0,在保存视频的地方遇到了一些坑。
首先,原来的VideoWriter_fourcc
没有了,需要使用VideoWriter::fourcc()
来定义编码,
具体的编码类型可以在http://www.fourcc.org/codecs.php中查看
例如
MP4的fourcc编码为int myFourCC = VideoWriter::fourcc('m', 'p', '4', 'v');
FLV的fourcc编码为int myFourCC = VideoWriter::fourcc('X', 'V', 'I', 'D');
写入视频无非是一帧帧写入图像,通过VideoWriter writer("OutFileSavePath", myFourCC, rate, size, true);
然后通过writer的writer.write(frame)
或者writer << frame;
写入视频图像。
VideoWriter实例化中最后一个参数是指定是否彩色写入,true即为彩色。
当实例化VideoWriter出错时,可以根据报错信息改变fourcc的值,或者查看FFmpeg是否安装完善。
可以通过ffmpeg.exe -formats来查看ffmpeg支持的格式
实例程序
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture videoCapture("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov");
Size size = Size(videoCapture.get(CAP_PROP_FRAME_WIDTH), videoCapture.get(CAP_PROP_FRAME_HEIGHT));
if (size.width <= 0 || size.height <= 0)
{
cout << "ERROR while get the width or the height" << endl;
}
else {
cout << "width: " << size.width << "\nheight: " << size.height << endl;
}
int myFourCC = VideoWriter::fourcc('X', 'V', 'I', 'D');//avi
//int myFourCC = VideoWriter::fourcc('m', 'p', '4', 'v');//mp4
//'m', 'p', '4', 'v'//'F','L','V','1'//'m', 'p', '4','2' //'m','p','4','v'//'d','i','v','x'
double rate = videoCapture.get(CAP_PROP_FPS);
VideoWriter writer("/home/oceanstar/CLionProjects/hello.avi", myFourCC, rate, size, true);
if (!videoCapture.isOpened()) {
cout << "Video not open!" << endl;
return 1;
}
Mat frame;
namedWindow("frame", WINDOW_AUTOSIZE);
// 通过总帧数来控制拍摄时间,100 = 4s, 200 = 8s
int count= 100;
while (count > 0){
bool ret = videoCapture.read(frame);
if (ret){
imshow("frame", frame);
//writer.write(frame);
writer << frame;
if (waitKey(20) > 0)break;
}else {
break;
}
count--;
}
cout << "FINISH ALL WORK----------" << endl;
videoCapture.release(); //when everything done, release the capture
writer.release();
destroyAllWindows();
system("pause");
return 0;
}
https://zhuanlan.zhihu.com/p/101383213
https://blog.csdn.net/weixin_40316053/article/details/85256429
在opencv中关于视频的读操作是通过VideoCapture类来完成的;关于视频的写操作是通过VideoWriter类来实现的。