OpenCV 录制视频、视频分解帧

录制视频【深度相机】

若使用普通免驱摄像头,则直接改成 VideoCapture cap(x) 就可以

#include <iostream>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
// #include "configure.h"
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<librealsense2/rs.hpp>
#include<librealsense2/rsutil.h>

using namespace cv;
using namespace std;
using namespace rs2;


int main()
{
    colorizer color_map;   // 帮助着色深度图像
    pipeline pipe;         //创建数据管道
    config pipe_config;
    // pipe_config.enable_stream(RS2_STREAM_DEPTH,640,480,RS2_FORMAT_Z16,30);
    // pipe_config.enable_stream(RS2_STREAM_COLOR,640,480,RS2_FORMAT_BGR8,30);
    pipeline_profile profile = pipe.start(pipe_config); //start()函数返回数据管道的profile

    // VideoWriter vw;
    // vw.open("1.mp4",VideoWriter::fourcc('M','J','P','G'),30,Size(640,480));
    namedWindow("1",WINDOW_NORMAL);
    setWindowProperty("1", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);    

    while (1)
    {
        frameset frameset = pipe.wait_for_frames();  //堵塞程序直到新的一帧捕获
        //取深度图和彩色图
        frame color_frame = frameset.get_color_frame();
        //获取宽高
        const int color_w=color_frame.as<video_frame>().get_width();
        const int color_h=color_frame.as<video_frame>().get_height();
        cout<<"color_w:"<<color_w<<endl;
        cout<<"color_h:"<<color_h<<endl;

        //创建OPENCV类型 并传入数据
        Mat color_image(Size(color_w,color_h),
                        CV_8UC3,(void*)color_frame.get_data(),Mat::AUTO_STEP);
        //实现深度图对齐到彩色图
		cvtColor(color_image,color_image,COLOR_RGB2BGR);

        imshow("1",color_image);
        // vw.write(color_image);
        //imshow("result",result);
        int key = waitKey(1);
        if(char(key) == 27)break;
    }
    return 0;
}

视频分解帧:

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;
int main()
{
    Mat frame;
    char outfile[50];
    VideoCapture cap("9.mp4");
    // if (!cap.isOpened())//打开失败
        // return;
    int totalFrame=cap.get(cv::CAP_PROP_FRAME_COUNT);//获取视频总帧数
    for (int i = 1; i <=totalFrame; i++)
    {
        cap>>frame;
        if (frame.empty())
            break;
        sprintf(outfile,"/home/joyce/workplace/vscode/img1/%d.jpg",i);
        imwrite(outfile,frame);
        imshow("video",frame);
        int key = waitKey(1);
        if(char(key) == 27)break;
    }
    return 0;
    // cap.release();
    // destroyAllWindows();
}

发表回复 0

Your email address will not be published. Required fields are marked *