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.
Mat img(100,100,CV_16UC3,Scalar(0,0,100));
if(img.empty())
{
cout<<"error"<<endl;
return -1;
}
vector<int> comp;
comp.push_back(CV_IMWRITE_JPEG_QUALITY);
comp.push_back(98);
bool b=imwrite("ii.jpg",img,comp);
waitKey(0);
return 0;
- 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.
Write Video to File
n the following example, a video is captured from the webcam and written into a file. Let's see how to do it with OpenCV.
VideoCapture cap(0);
namedWindow("my video",1);
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
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
while(1)
{
Mat frame;
bool bSuccess=cap.read(frame);
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;
- Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight))
Create a
Size object with a given width and height. Here I have cast the width and height to integers because they are originally double values and the
Size constructor does not accept double values as its parameters.
- VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
This is the constructor of the VideoWriter class. It initializes the object with following parameters
- const string& filename - Specify the name and the location of the output file. The video stream is written into this file
- int fourcc - specify the 4 character code for the codec which is used to compress the video. Your computer may not be supported some codecs. So, if you fail to save the video, please try other codecs. Here are some popular codecs.
- CV_FOURCC('D', 'I', 'V', '3') for DivX MPEG-4 codec
- CV_FOURCC('M', 'P', '4', '2') for MPEG-4 codec
- CV_FOURCC('D', 'I', 'V', 'X') for DivX codec
- CV_FOURCC('P','I','M','1') for MPEG-1 codec
- CV_FOURCC('I', '2', '6', '3') for ITU H.263 codec
- CV_FOURCC('M', 'P', 'E', 'G') for MPEG-1 codec
Complete list of codec can be found here
Here I have used CV_FOURCC(
'P', 'I', 'M', '1'
) as the four character code of codec to compress the video. If the output file cannot be opened, you can try different
four character code of codecs.
For
Windows users, it is possible to use -1 instead of the above codecs in order to choose compression method and additional compression parameters from a dialog box. It is a best method for Microsoft Windows users.
- double fps - frames per seconds of the video stream. I have used 20. You can try different values. But the codec should support the fps value. So, use an appropriate value.
- Size frameSize - Size object which specify the width and the height of each frame of the video stream.
- bool isColor - If you want to save a color video, pass the value as true. Otherwise false. Remember codec should support whatever value, you pass. In the above example, you have to give true as the 5th argument. Otherwise it will not work.
- if ( !oVideoWriter.isOpened() )
Check whether the VideoWriter object initialize successfully. If not exit the program immediately.
- void write(const Mat& image)
Write a frame to the video stream. The size of the frame should be same as the size you specified when initializing the VideoWriter object.
All other OpenCV functions have been discussed in earlier lessons. So, if you are not familiar with those OpenCV functions yet, please go through
Capture Video from File or Camera