- 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 - Reading Images
The Imgcodecs class of the package org.opencv.imgcodecs provides methods to read and write images. Using OpenCV, you can read an image and store it in a matrix (perform transformations on the matrix if needed). Later, you can write the processed matrix to a file.
The read() method of the Imgcodecs class is used to read an image using OpenCV. Following is the syntax of this method.
imread(filename)
It accepts an argument (filename), a variable of the String type representing the path of the file that is to be read.
Given below are the steps to be followed to read images in Java using OpenCV pbrary.
Step 1: Load the OpenCV native pbrary
Load the OpenCV native pbrary using the load() method, as shown below.
//Loading the core pbrary System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Step 2: Instantiate the Imgcodecs class
Instantiate the Imgcodecs class.
//Instantiating the Imgcodecs class Imgcodecs imageCodecs = new Imgcodecs();
Step 3: Reading the image
Read the image using the method imread(). This method accepts a string argument representing the path of the image and returns the image read as Mat object.
//Reading the Image from the file Mat matrix = imageCodecs.imread(Path of the image);
Example
The following program code shows how you can read an image using OpenCV pbrary.
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; pubpc class ReadingImages { 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 String file ="C:/EXAMPLES/OpenCV/sample.jpg"; Mat matrix = imageCodecs.imread(file); System.out.println("Image Loaded"); } }
On executing the above program, OpenCV loads the specified image and displays the following output −
Image LoadedAdvertisements