How to save an Image in OpenCV using C++?

Here, we will understand how to save the OpenCV image to any location on your computer.  OpenCV provides imwrite() function to save an image to a specified file. The file extension represents the image format. 

The actual format of the function is −

imwrite("Destination/Name of the image with extension", Source Matrix)

Here, "Destination" is where we want to save the image. In this program, we save the image as "Lakshmi.jpg". We can give any name to the image. The "Source Matrix" is the matrix where the image has been loaded. In this program, the image is loaded as "myImage" matrix.


using namespace cv;
using namespace std;
int main(int argc,const char** argv) {
   Mat myImage;//declaring a matrix named myImage//
   myImage = imread("lena.png");//loading the image named lena in the matrix//
   imwrite("lakshmi.jpg", myImage);  
   waitKey(0);//wait till user press any key
   destroyWindow("MyWindow");//close the window and release allocate memory//
   cout << "Image is saved successfully…..";
   return 0;


Image is saved successfully...

Updated on: 10-Mar-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started