opencv读取视频

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

using namespace cv;

void main()
{
	VideoCapture capture(0);
	VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480));
	Mat frame;
	
	while (capture.isOpened())
	{
		capture >> frame;
		writer << frame;
		imshow("video", frame);
		if (cvWaitKey(20) == 27)
		{
			break;
		}
	}
}


Write Image & Video to File

In this lesson, I am going to show you how to write images and videos to a file using OpenCV.
If you have not install and configure OpenCV yet, please refer to Installing & Configuring with Visual Studio.


Write Image to File

In the following example, a yellow image is created and written into a file. Let's see how to do it with OpenCV.

///

#include "opencv2/highgui/highgui.hpp"
#include <iostream>


using namespace cv;

using namespace std;

int main( int argc, const char** argv )

{
  Mat img(650, 600, CV_16UC3, Scalar(0,50000, 50000)); //create an image ( 3 channels, 16 bit image depth, 650 high, 600 wide, (0, 50000, 50000) assigned for Blue, Green and Red plane respectively. )


if (img.empty()) //check whether the image is loaded or not

{
cout << "ERROR : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
return -1;
}

     vector<int> compression_params; //vector that stores the compression parameters of the image

     compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique

     compression_params.push_back(98); //specify the compression quality



     bool bSuccess = imwrite("D:/TestImage.jpg", img, compression_params); //write the image to file



     if ( !bSuccess )

    {

cout << "ERROR : Failed to save the image" << endl;

         //system("pause"); //wait for a key press

    }

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"

imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0);  //wait for a keypress


     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"


return 0;

}
///
You can download this OpenCV visual c++ project from here(The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)

The above program is very much similar to the program under 'Create a Blank Image & Display' section in the lesson of Read & Display Image. If you need further clarifications of any OpenCV functions which are not explained here, please refer to  Read & Display Image lesson.


New OpenCV functions which are not found earlier
  • bool imwrite( const string& filename, InputArray img, const vector<int>& params=vector<int>())
The function saves the image in the variable 'img' to a file, specified by 'filename' . If this function fails to save the image, it will return false. On success of writing the file to the harddisk, it will return true.

Parameters -

  • filename - specify the location and name of the file to be saved
  • img - hold the image which is going to save
  • params - This is a int vector to which you have to insert some int parameters specifying the format of the image
    • JPEG format - You have to puch_back CV_IMWRITE_JPEG_QUALITY first and then a number between 0 and 100 (higher is the better). If you want the best quality output, use 100. I have used 98 in the above sample program. But higher the value, it will take longer time to write the image
    • PNG format - You have to puch_back CV_IMWRITE_PNG_COMPRESSION first and then a number between 0 and 9 (higher is the better compression, but slower).

The image format is chosen depending on the file name extension. Only images with 8 bit or 16 bit unsigned   single channel or 3 channel ( CV_8UC1, CV_8UC3, CV_8SC1, CV_8SC3,  CV_16UC1, CV_16UC3)  with 'BGR' channel order, can be saved. If the depth or channel order of the image is different, use   'Mat::convertTo()'  or  'cvtColor'  functions to convert the image to supporting format before using imwrite function.

The above program is very much similar to the program under 'Create a Blank Image & Display' section in the lesson of Read & Display Image. If you need further clarifications of any OpenCV functions which are not explained here, please refer to  Read & Display Image lesson.

Summary
This program  creates an yellowish image ( 3 channels, 16 bit image depth, 650 high, 600 wide, (0, 50,000, 50,000) assigned for BGR channels). Because the image has 16 bit depth, you can use values between 0 and 65535 for each element in each channel. I have used 50,000 for each element in the green and red planes which gives the yellowish color. You can try different values. 
Then it specifies the compressing technique. I have used JPEG as the compression technique in the above example. Then it saves the image in the  "D:/TestImage.jpg"  location. Then it displays the newly created image in a window and wait indefinitely until an user presses a key.




Write Video to File

In the following example, a video is captured from the webcam and written into a file. Let's see how to do it with OpenCV.

///
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "ERROR: Cannot open the video file" << endl;
        return -1;
    }

 namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"


   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

   cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

   Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

 VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object 

   if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
   {
cout << "ERROR: Failed to write the video" << endl;
return -1;
   }

    while (1)
    {


        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
{
             cout << "ERROR: Cannot read a frame from video file" << endl;
             break;
        }

oVideoWriter.write(frame); //writer the frame into the file

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break
       }
    }

    return 0;

}
///
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)

Here is how I record my video from my webcam and save to a file in D drive
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值