当前位置:网站首页>Use OpenCV to decompose the video into single frame pictures and synthesize the video with pictures

Use OpenCV to decompose the video into single frame pictures and synthesize the video with pictures

2022-06-21 14:30:00 Modest learning and progress

What this paper does is based on opencv Convert video frames to picture output , Because a video contains too many frames , Often we don't need all of its frames to be converted into pictures , So we want to be able to set how many frames to rotate the picture again ( This article is set to 30 frame ), If someone needs only the first few frames , You can also rewrite the code in a similar way .
Procedure 1 :

#include <iostream>
#include "cv.h"
#include "opencv2/opencv.hpp"
 
using namespace std;
using namespace cv;
 
//  describe : Convert video frames to picture output 
void main()
{
	//  Get video files 
	VideoCapture cap("J:\\CQH\\DLFR\\lab_face\\video\\DSC_0023.MOV");
 
	//  Get the total number of video frames 
	long totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "total frames: " << totalFrameNumber << endl;
 
	Mat frame;
	bool flags = true;
	long currentFrame = 0;
 
	while (flags){
		//  Read every frame of video 
		cap.read(frame);
 
		stringstream str;
		str << "cqh" << currentFrame << ".jpg";
		cout << " Under processing " << currentFrame << " frame " << endl;
		printf("\n");
 
		//  Set every 30 Frame get one frame 
		if (currentFrame % 30 == 0){
			//  Convert frame to picture output 
			imwrite("J:\\CQH\\DLFR\\lab_face\\videoToImages\\DSC_0023\\" + str.str(), frame);
		}
		//  The end condition 
		if (currentFrame >= totalFrameNumber){
			flags = false;
		}
		currentFrame++;
	}
	
	system("pause");
}

Program 2( Read pictures in batch order )

#include<opencv2/opencv.hpp>

using namespace cv;

void main()
{
    // Batch reading of pictures ( Orderly )
    char filename[50];
    char winName[50];
    Mat srcImg;
    for (int i = 1; i < 100; i++)
    {
        sprintf(filename,"%d.bmp",i);
        sprintf(winName,"NO--%d",i);
        srcImg=imread(filename);
        if (srcImg.empty())
            break;
        imshow(winName,srcImg);
    }
    waitKey(0);
    destroyAllWindows();
}

 

Two 、 Decomposing video into pictures

Need to be ahead of time E Disk creation pic Folder , Picture naming by 1,2.... Such numbers are arranged

#include<opencv2/opencv.hpp>

using namespace cv;

void main()
{
    Mat frame;
    char outfile[50];
    VideoCapture cap("E:\\2.avi");
    if (!cap.isOpened())// Open the failure 
        return;
    int totalFrame=cap.get(CV_CAP_PROP_FRAME_COUNT);// Get the total number of video frames 
    for (int i = 1; i <=totalFrame; i++)
    {
        cap>>frame;
        if (frame.empty())
            break;
        sprintf(outfile,"E:\\pic\\%d.bmp",i);
        imwrite(outfile,frame);
        imshow("video",frame);
        waitKey(15);
    }
    cap.release();
    destroyAllWindows();
}

 

3、 ... and 、 Picture synthesis video

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

void main()
{
    VideoWriter writer("E:\\out.avi",CV_FOURCC('X','V','I','D'),20,Size(320,240),true);//Size Be consistent with the size of the picture 
    char filename[50];
    Mat frame;
    for (int i = 1; i < 644; i++)
    {
        sprintf(filename,"E:\\pic\\%d.bmp",i);
        frame=imread(filename);
        if(frame.empty())   break;
        writer<<frame;
    }
    cout<<"write end!"<<endl;
    destroyAllWindows();
}

  It turns out that E The root directory generates a out.avi Video file , This directory can be changed by itself , The image source path can also be changed

Reference resources :https://blog.csdn.net/mr_evanchen/article/details/77733978

            http://www.cnblogs.com/little-monkey/p/7162594.html

原网站

版权声明
本文为[Modest learning and progress]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221424252662.html