Writing Image and Video to file

Writing image got from camera

Following program saves the image when you press ‘A’ from your keyboard.

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
VideoCapture cap(0); // opens the video camera 0

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

double frame = cap.get(CAP_PROP_POS_FRAMES);

//playing with other arguments of namedWindow to see the effect.

namedWindow("myVideo", CV_WINDOW_NORMAL);

while (1)
{
Mat frame;
if (cap.read(frame))
{
imshow("myVideo", frame);
}
else
{
cout << "error reading frames \n";
}

if (waitKey(30) == 65)
{
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", frame, compression_params); //write the image to file
if (!bSuccess)

{

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

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

}
break;
}
}
return 0;
}

Remember saving the image to drive other than C:, usually saving to C: Drive causes permission issue.

Writing video got from camera

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
VideoCapture cap(0); // opens the video camera 0

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

double frame = cap.get(CAP_PROP_POS_FRAMES);

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('M', 'J', 'P', 'G'), 20, frameSize, true); //initialize the VideoWriter object
//20 is frame rate..

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 success = cap.read(frame);

if (success)
{
oVideoWriter.write(frame);
imshow("MyVideo", frame);
}
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;
}

Leave a Reply

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