Linux下OpenCV摄像头录像(c++)

opencv 从摄像头中读取视频并保存(c++版)_YouthDance-CSDN博客_opencv 保存摄像头视频

 参考如上

CMakeLists.txt

# cmake版本
cmake_minimum_required(VERSION 2.8.3) 
# c++ 11
set(CMAKE_CXX_FLAGS "-std=c++11")
# 项目名
project(test_opencv)
# 使用OpenCV
find_package(OpenCV REQUIRED)
# 添加头文件
include_directories(${OpenCV_INCLUDE_DIRS})
# 查找指定目录下的所有源文件,然后将结果存进指定变量名。语法:
# aux_source_directory(<dir> <variable>)
# 查找当前目录下的所有源文件
# 并将名称保存到SRC_LIST变量
aux_source_directory(. SRC_LIST)
# 生成可执行文件
add_executable(test_opencv ${SRC_LIST})
# 生成可执行文件后,为生成文件target添加库
target_link_libraries(test_opencv ${OpenCV_LIBS})

test_opencv.cpp   OpenCV 3版本

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    // 视频保存位置
    string outputVideoPath = "./test.avi";  

    // 打开摄像头
    VideoCapture capture0(0);  

    VideoWriter outputVideo;
    
    // 获取摄像机帧率
    int fps = capture0.get(CAP_PROP_FPS);  

    // 获取当前摄像头的视频信息
    cv::Size S = cv::Size((int)capture0.get(CV_CAP_PROP_FRAME_WIDTH),
                          (int)capture0.get(CV_CAP_PROP_FRAME_HEIGHT));
    // 打开视频路径,设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
    outputVideo.open(outputVideoPath, CV_FOURCC('X', 'V', 'I', 'D'), fps, S, true);

    if (!outputVideo.isOpened()) {
        cout << "fail to open!" << endl;
        return -1;
    }

    // 图片帧
    cv::Mat frameImage;
    int count = 0;

    while(true){
        // 读取当前帧
        capture0 >> frameImage;
        if(frameImage.empty()) break;
        ++count;
        // 输出当前帧
        cv::imshow("output", frameImage);
        // 保存当前帧
        outputVideo << frameImage;
        if (char(waitKey(1)) == 'q') break;
    }
	return 0;
}

 test_opencv.cpp   OpenCV 4版本

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main()
{
    // 视频保存位置
    string outputVideoPath = "./test.avi";  
 
    // 打开摄像头
    VideoCapture capture0(0);  
 
    VideoWriter outputVideo;
    
    // 获取摄像机帧率
    int fps = capture0.get(CAP_PROP_FPS);  
 
    // 获取当前摄像头的视频信息
    cv::Size S = cv::Size((int)capture0.get(CAP_PROP_FRAME_WIDTH),
                          (int)capture0.get(CAP_PROP_FRAME_HEIGHT));
    // 打开视频路径,设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
    outputVideo.open(outputVideoPath, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, S, true);
 
    if (!outputVideo.isOpened()) {
        cout << "fail to open!" << endl;
        return -1;
    }
 
    // 图片帧
    cv::Mat frameImage;
    int count = 0;
 
    while(true){
        // 读取当前帧
        capture0 >> frameImage;
        if(frameImage.empty()) break;
        ++count;
        // 输出当前帧
        cv::imshow("output", frameImage);
        // 保存当前帧
        outputVideo << frameImage;
        if (char(waitKey(1)) == 'q') break;
    }
	return 0;
}

执行:

  • cmake .
  • make
  • ./test_opencv
  • 生成录像视频test.avi
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奶油芝士汉堡包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值