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;
}
#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.
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 -
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
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
'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.
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.
0 comments :
Post a Comment