保存图像
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Read the image file
Mat image = imread("/home/CORPUSERS/xp025362/Pictures/opencv_test1.png");
// Check for failure
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //wait for any key press
return -1;
}
/*
Make changes to the image as necessary
e.g.
1. Change brightness/contrast of the image
2. Smooth/Blur image
3. Crop the image
4. Rotate the image
5. Draw shapes on the image
*/
bool isSuccess = imwrite("/home/CORPUSERS/xp025362/software/opencv-3.2.0/wxt/save/wxt.jpg", image); //write the image to a file as JPEG
//bool isSuccess = imwrite("D:/MyImage.png", image); //write the image to a file as PNG
if (isSuccess == false)
{
cout << "Failed to save the image" << endl;
cin.get(); //wait for a key press
return -1;
}
cout << "Image is succusfully saved to a file" << endl;
String windowName = "The Saved Image"; //Name of the window
namedWindow(windowName); // Create a window
imshow(windowName, image); // Show our image inside the created window.
waitKey(0); // Wait for any keystroke in the window
destroyWindow(windowName); //destroy the created window
return 0;
}
保存视频
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
//Open the default video camera
VideoCapture cap("/home/CORPUSERS/xp025362/Videos/dog.mp4");
// if not success, exit program
if (cap.isOpened() == false)
{
cout << "Cannot open the video camera" << endl;
cin.get(); //wait for any key press
return -1;
}
int frame_width = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH)); //get the width of frames of the video
int frame_height = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT)); //get the height of frames of the video
Size frame_size(frame_width, frame_height);
int frames_per_second = 10;
//Create and initialize the VideoWriter object
int codec = CV_FOURCC('M','J','P','G');
VideoWriter oVideoWriter("/home/CORPUSERS/xp025362/software/opencv-3.2.0/wxt/save/wxt.avi",
codec, frames_per_second, frame_size, true);
//If the VideoWriter object is not initialized successfully, exit the program
if (oVideoWriter.isOpened() == false)
{
cout << "Cannot save the video to a file" << endl;
cin.get(); //wait for any key press
return -1;
}
string window_name = "My Camera Feed";
namedWindow(window_name); //create a window called "My Camera Feed"
while (true)
{
Mat frame;
bool isSuccess = cap.read(frame); // read a new frame from the video camera
//Breaking the while loop if frames cannot be read from the camera
if (isSuccess == false)
{
cout << "Video camera is disconnected" << endl;
cin.get();//Wait for any key press
break;
}
//write the video frame to the file
oVideoWriter.write(frame);
//show the frame in the created window
imshow(window_name, frame);
if (waitKey(10) == 27)
{
cout << "Esc key is pressed by the user. Stopping the video" << endl;
break;
}
}
//Flush and close the video file
oVideoWriter.release();
return 0;
}
这里两个例子比较简单,相信大家都能看懂。 那么在使用不同版本的OpenCV编译时,会抛出比如我这样
‘CV_FOURCC’ is not a member of ‘cv::VideoWriter’ 的error,大家可以参考/sample/下的示例code,看下你自己所用的版本对应的member怎么使用,可以很快解决问题。
关于codec的参数含义可以参考:FOURCC
记录下第二天的学习路程,小白一枚有问题可以互相交流~