OpenCV study notes (15) openc frame video solution

 

Amendment 1 reads the video address

2 Modify the path to save the image sequence

String videopath = "F: / dongdong / 0tool / 3D / 2 Model / camera array / 1_12cam one hundred million camera / data / giga1014 2 /";
String imagepath = "F: / dongdong / 0tool / 3D / 2 Model / camera array / 1_12cam one hundred million camera / data / giga1014 2 / jpg / 1 /";

 

 

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main ()
{
	// open a video file: in fact, is to create a structure VideoCapture
	String videopath = "F: / dongdong / 0tool / 3D / 2 Model / camera array / 1_12cam one hundred million camera / data / giga1014 2 /";
	String imagepath = "F: / dongdong / 0tool / 3D / 2 Model / camera array / 1_12cam one hundred million camera / data / giga1014 2 / jpg / 1 /";

	VideoCapture capture(videopath + "ch11_20191014175459.mp4");
	// detect whether the normal open: the success of open, isOpened return ture
	if (!capture.isOpened())
		cout << "fail toopen!" << endl;

	// Get the whole frame
	long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "whole video were" << totalFrameNumber << "frame" << endl;


	// Set the start frame ()
	long frameToStart =33;
	capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);
	cout << "from" << frameToStart << "Frame Start reading" << endl;

	// set the end frame
	int frameToStop = 66;

	if (frameToStop < frameToStart)
	{
		cout << "the end of the frame is less than the start frame, bugs, about to quit!" << endl;
		return -1;
	}
	else
	{
		cout << "End frame: a first" << frameToStop << "frame" << endl;
	}

	// Get the frame rate
	double rate = capture.get(CV_CAP_PROP_FPS);
	cout << "frame rate:" << rate << endl;


	// definition of a control variable for the end of the video read cycle
	bool stop = false;

	// load image for each frame
	Mat frame;

	// window displaying each frame
	//namedWindow( "Extractedframe" );

	// time interval between two frames:
	//int delay = 1000/rate;
	double delay = 1000 / rate;


	// read the frame using a while loop
	// currentFrame control variable is read after the end of the loop frame specified in the loop body
	long currentFrame = frameToStart;


	while (!stop)
	{
		// Read the next frame
		if (!capture.read(frame))
		{
			cout << "Failed to read the video" << endl;
			return -1;
		}


		cout << "is first read" << currentFrame << "frame" << endl;
		cvNamedWindow("Extractedframe",0);
		imshow( "Extractedframe", frame );

		// Here is skipping operation
		if (currentFrame% 1 == 0) // here the number of frames interval, there can be a modification
		{
			cout << "is writing the first" << currentFrame << "frame" << endl;
			stringstream str;
			str << imagepath << currentFrame << ".jpg"; / * location for storing pictures * /

			cout << str.str() << endl;
			imwrite(str.str(), frame);
		}

		// waitKey (intdelay = 0) when delay≤ 0 wait forever; when the delay> 0 will wait delay milliseconds
		// When no key is pressed before the end time, return -1; otherwise key
		int c = waitKey(delay);
		// After pressing the ESC or reach the end of the specified video frame quit reading
		if ((char)c == 27 || currentFrame > frameToStop)
		{
			stop = true;
		}
		// After pressing the button will stay on the current frame, waiting for the next key
		if (c >= 0)
		{
			waitKey(0);
		}
		currentFrame++;

	}

	// close the video file
	capture.release();
	waitKey(0);
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/kekeoutlook/p/11681934.html