OpenCV Tutorial
Types of Images
Image Conversion
Drawing Functions
Blur
Filtering
Thresholding
Sobel Derivatives
Transformation Operations
Camera and Face Detection
Geometric Transformations
Miscellaneous Chapters
OpenCV Useful Resources
Selected Reading
- OpenCV - GUI
- OpenCV - Writing an Image
- OpenCV - Reading Images
- OpenCV - Storing Images
- OpenCV - Environment
- OpenCV - Overview
- OpenCV - Home
Types of Images
Image Conversion
Drawing Functions
- OpenCV - Adding Text
- OpenCV - Drawing Arrowed Lines
- OpenCV - Drawing Convex Polylines
- OpenCV - Drawing Polylines
- OpenCV - Drawing an Ellipse
- OpenCV - Drawing a Rectangle
- OpenCV - Drawing a Line
- OpenCV - Drawing a Circle
Blur
Filtering
- OpenCV - Image Pyramids
- OpenCV - Morphological Operations
- OpenCV - Erosion
- OpenCV - Dilation
- OpenCV - Filter2D
- OpenCV - SQRBox Filter
- OpenCV - Box Filter
- OpenCV - Bilateral Filter
Thresholding
Sobel Derivatives
Transformation Operations
Camera and Face Detection
Geometric Transformations
Miscellaneous Chapters
OpenCV Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV - Writing an Image
OpenCV - Writing an Image
The write() method of the Imgcodecs class is used to write an image using OpenCV. To write an image, repeat the first three steps from the previous example.
To write an image, you need to invoke the imwrite() method of the Imgcodecs class.
Following is the syntax of this method.
imwrite(filename, mat)
This method accepts the following parameters −
filename − A String variable representing the path where to save the file.
mat − A Mat object representing the image to be written.
Example
Following program is an example to write an image using Java program using OpenCV pbrary.
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; pubpc class WritingImages { pubpc static void main(String args[]) { //Loading the OpenCV core pbrary System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Instantiating the imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); //Reading the Image from the file and storing it in to a Matrix object String file ="C:/EXAMPLES/OpenCV/sample.jpg"; Mat matrix = imageCodecs.imread(file); System.out.println("Image Loaded .........."); String file2 = "C:/EXAMPLES/OpenCV/sample_resaved.jpg"; //Writing the image imageCodecs.imwrite(file2, matrix); System.out.println("Image Saved ............"); } }
On executing the above program, you will get the following output −
Image Loaded .......... Image Saved ...........
If you open the specified path, you can observe the saved image as shown below −
![Write Image](/opencv/images/write_image.jpg)