OpenCV Quick Guide
OpenCV Quick Guide
OpenCV - Overview
OpenCV is a cross-platform library using which we can develop real-time computer vision
applications. It mainly focuses on image processing, video capture and analysis including
features like face detection and object detection.
Computer Vision
Computer Vision can be defined as a discipline that explains how to reconstruct, interrupt,
and understand a 3D scene from its 2D images, in terms of the properties of the structure
present in the scene. It deals with modeling and replicating human vision using computer
software and hardware.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 1/100
7/11/2019 OpenCV Quick Guide
Robotics Application
Navigation
Obstacles avoidance
Human Robot Interaction (HRI) − Intelligent robotics to interact with and serve
people
Medicine Application
Classification and detection (e.g. lesion or cells classification and tumor detection)
2D/3D segmentation
Assembly
Object sorting
Security Application
Transportation Application
Autonomous vehicle
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 2/100
7/11/2019 OpenCV Quick Guide
Detect specific objects such as faces, eyes, cars, in the videos or images.
Analyze the video, i.e., estimate the motion in it, subtract the background, and
track objects in it.
OpenCV was originally developed in C++. In addition to it, Python and Java bindings were
provided. OpenCV runs on various Operating Systems such as windows, Linux, OSx,
FreeBSD, Net BSD, Open BSD, etc.
This tutorial explains the concepts of OpenCV with examples using Java bindings.
Core Functionality
This module covers the basic data structures such as Scalar, Point, Range, etc., that are
used to build OpenCV applications. In addition to these, it also includes the
multidimensional array Mat, which is used to store the images. In the Java library of
OpenCV, this module is included as a package with the name org.opencv.core.
Image Processing
This module covers various image processing operations such as image filtering,
geometrical image transformations, color space conversion, histograms, etc. In the Java
library of OpenCV, this module is included as a package with the name
org.opencv.imgproc.
Video
This module covers the video analysis concepts such as motion estimation, background
subtraction, and object tracking. In the Java library of OpenCV, this module is included as
a package with the name org.opencv.video.
Video I/O
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 3/100
7/11/2019 OpenCV Quick Guide
This module explains the video capturing and video codecs using OpenCV library. In the
Java library of OpenCV, this module is included as a package with the name
org.opencv.videoio.
calib3d
This module includes algorithms regarding basic multiple-view geometry algorithms, single
and stereo camera calibration, object pose estimation, stereo correspondence and
elements of 3D reconstruction. In the Java library of OpenCV, this module is included as a
package with the name org.opencv.calib3d.
features2d
This module includes the concepts of feature detection and description. In the Java library
of OpenCV, this module is included as a package with the name org.opencv.features2d.
Objdetect
This module includes the detection of objects and instances of the predefined classes such
as faces, eyes, mugs, people, cars, etc. In the Java library of OpenCV, this module is
included as a package with the name org.opencv.objdetect.
Highgui
This is an easy-to-use interface with simple UI capabilities. In the Java library of OpenCV,
the features of this module is included in two different packages namely,
org.opencv.imgcodecs and org.opencv.videoio.
In the year 2006, its first major version, OpenCV 1.0 was released.
OpenCV - Environment
In this chapter, you will learn how to install OpenCV and set up its environment in your
system.
Installing OpenCV
First of all, you need to download OpenCV onto your system. Follow the steps given below.
Step 2 − Now, click the Downloads link highlighted in the above screenshot. On clicking,
you will be directed to the downloads page of OpenCV.
Step 3 − On clicking the highlighted link in the above screenshot, a file named opencv-
3.1.0.exe will be downloaded. Extract this file to generate a folder opencv in your
system, as shown in the following screenshot.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 5/100
7/11/2019 OpenCV Quick Guide
Step 4 − Open the folder OpenCV → build → java. Here you will find the jar file of
OpenCV named opencv-310.jar. Save this file in a separate folder for further use.
Eclipse Installation
After downloading the required JAR files, you have to embed these JAR files to your Eclipse
environment. You can do this by setting the Build Path to these JAR files and by using
pom.xml.
Step 1 − Ensure that you have installed Eclipse in your system. If not, download and
install Eclipse in your system.
Step 2 − Open Eclipse, click on File, New, and Open a new project as shown in the
following screenshot.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 6/100
7/11/2019 OpenCV Quick Guide
Step 3 − On selecting the project, you will get the New Project wizard. In this wizard,
select Java project and proceed by clicking the Next button, as shown in the following
screenshot.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 7/100
7/11/2019 OpenCV Quick Guide
Step 4 − On proceeding forward, you will be directed to the New Java Project wizard.
Create a new project and click Next, as shown in the following screenshot.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 8/100
7/11/2019 OpenCV Quick Guide
Step 5 − After creating a new project, right-click on it. Select Build Path and click
Configure Build Path… as shown in the following screenshot.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 9/100
7/11/2019 OpenCV Quick Guide
Step 6 − On clicking the Build Path option, you will be directed to the Java Build Path
wizard. Click the Add External JARs button, as shown in the following screenshot.
Step 7 − Select the path where you have saved the file opencv-310.jar.
Step 8 − On clicking the Open button in the above screenshot, those files will be added to
your library.
Step 9 − On clicking OK, you will successfully add the required JAR files to the current
project and you can verify these added libraries by expanding the Referenced Libraries.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 10/100
7/11/2019 OpenCV Quick Guide
Location of DLL files − Open the installation folder of OpenCV and go to the sub-folder
build → java. Here you will find the two folders x64 (64 bit) and x86 (32 bit) which
contain the dll files of OpenCV.
Open the respective folder suitable for your operating system, then you can see the dll
file, as shown in the following screenshot.
Now, set the path for this file too by following the steps given below −
Step 1 − Once again, open the JavaBuildPath window. Here you can observe the added
JAR file and the JRE System Library.
Step 2 − On expanding it, you will get the system libraries and Native library location,
as highlighted in the following screenshot.
Step 3 − Double-click on the Native library location. Here, you can see the Native
Library Folder Configuration window as shown below.
Here, click the button External Folder… and select the location of the dll file in your
system.
The Mat class of OpenCV library is used to store the values of an image. It represents an
n-dimensional array and is used to store image data of grayscale or color images, voxel
volumes, vector fields, point clouds, tensors, histograms, etc.
This class comprises of two data parts: the header and a pointer
Header − Contains information like size, method used for storing, and the address
of the matrix (constant in size).
Constructors
The Mat class of OpenCV Java library has various constructors, using which you can
construct the Mat object.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 11/100
7/11/2019 OpenCV Quick Guide
Mat()
1 This is the default constructor with no parameters in most cases. We use this to
constructor to create an empty matrix and pass this to other OpenCV methods.
4 This constructor accepts two parameters, an object representing the size of the
matrix and an integer representing the type of the array used to store the data.
6 Mat(long addr)
7 This constructor accepts an object of another matrix and an object of the class
Range representing the range of the rows to be taken to create a new matrix.
9 This constructor accepts two objects, one representing another matrix and the
other representing the Region Of Interest.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 12/100
7/11/2019 OpenCV Quick Guide
Note −
Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or
CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels)
matrices.
The type of the matrices were represented by various fields of the class CvType
which belongs to the package org.opencv.core.
Mat col(int x)
Mat row(int y)
2 This method accepts an integer parameter representing the index of a row and
retrieves and returns that row.
int cols()
3
This method returns the number of columns in the matrix.
int rows()
4
This method returns the number of rows in the matrix.
5 This method accepts an object of the Mat type and sets the array elements to
the specified value.
Mat setTo(Scalar s)
6 This method accepts an object of the Scalar type and sets the array elements to
the specified value.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 13/100
7/11/2019 OpenCV Quick Guide
In this section, we are going to discuss our first OpenCV example. We will see how to
create and display a simple OpenCV matrix.
Given below are the steps to be followed to create and display a matrix in OpenCV.
//Creating a matrix
Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));
And, you can set values to these using any of the variants of the setTo() methods.
Example
You can use the following program code to create and display a simple matrix in Java
using OpenCV library.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
class DisplayingMatrix {
public static void main(String[] args) {
//Loading the core library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 14/100
7/11/2019 OpenCV Quick Guide
//Creating a matrix
Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));
On executing the above program, you will get the following output −
Example
You can use the following program code to load and save images using JavaSE library.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 15/100
7/11/2019 OpenCV Quick Guide
ImageIO.write(image, "jpg", ouptut);
System.out.println("image Saved");
}
}
On executing the above program, you will get the following output −
image Saved
If you open the specified path, you can observe the saved image as follows −
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 library.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 16/100
7/11/2019 OpenCV Quick Guide
Example
The following program code shows how you can read an image using OpenCV library.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
System.out.println("Image Loaded");
}
}
On executing the above program, OpenCV loads the specified image and displays the
following output −
Image Loaded
To write an image, you need to invoke the imwrite() method of the Imgcodecs class.
imwrite(filename, mat)
filename − A String variable representing the path where to save the file.
Example
Following program is an example to write an image using Java program using OpenCV
library.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 17/100
7/11/2019 OpenCV Quick Guide
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.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);
On executing the above program, you will get the following output −
If you open the specified path, you can observe the saved image as shown below −
OpenCV - GUI
In the earlier chapters, we have discussed how to read and save an image using OpenCV
Java library. In addition to it, we can also display the loaded images in a separate window
using GUI libraries such as AWT/Swings and JavaFX.
Following are the steps to convert a Mat object of OpenCV to BufferedImage object.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 18/100
7/11/2019 OpenCV Quick Guide
Ext − A String parameter specifying the image format (.jpg, .png, etc.)
Then, instantiate the JFrame class and add the buffered image created to the
ContentPane of the JFrame, as shown below −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 19/100
7/11/2019 OpenCV Quick Guide
//Instantiate JFrame
JFrame frame = new JFrame();
Example
The following program code shows how you can read an image and display it through
swing window using OpenCV library.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
//Reading the Image from the file and storing it in to a Matrix object
String file = "C:/EXAMPLES/OpenCV/sample.jpg";
Mat image = Imgcodecs.imread(file);
//Instantiate JFrame
JFrame frame = new JFrame();
System.out.println("Image Loaded");
}
}
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 20/100
7/11/2019 OpenCV Quick Guide
On executing the above program, you will get the following output −
Image Loaded
In addition to that, you can see a window displaying the image loaded, as follows −
Example
The following program code shows how to read an image and display it through JavaFX
window using OpenCV library.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 21/100
7/11/2019 OpenCV Quick Guide
//setting the fit height and width of the image view
imageView.setFitHeight(400);
imageView.setFitWidth(500);
//Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat image = Imgcodecs.imread(file);
System.out.println("Image Loaded");
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
public static void main(String args[]) {
launch(args);
}
}
On executing the above program, you will get the following output −
Image Loaded
In addition to that, you can see a window displaying the image loaded, as follows −
OpenCV supports various types of images such as colored, binary, grayscale, etc. Using
the imread() method and predefined fields of the Imgcodecs class, you can read a given
image as another type.
imread(filename)
flags − An integer value representing a predefined flag value. For each value, this
reads the given image as a specific type (gray scale color etc.)
Following is the table listing various fields provided in the Imgproc class as values for this
parameter.
IMREAD_COLOR
1 If the flag is set to this value, the loaded image will be converted to a 3-channel
BGR (Blue Green Red) color image.
IMREAD_GRAYSCALE
2 If the flag is set to this value, the loaded image will be converted to a single-
channel grayscale image.
IMREAD_LOAD_GDAL
3
If the flag is set to this value, you can load the image using the gdal driver.
IMREAD_ANYCOLOR
4
If the flag is set to this value, the image is read in any possible color format.
5 IMREAD_REDUCED_COLOR_2
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 23/100
7/11/2019 OpenCV Quick Guide
IMREAD_REDUCED_COLOR_4
IMREAD_REDUCED_COLOR_8
If the flag is set to this value, the image is read as three-channel BGR, and the
size of the image is reduced to ½, ¼th or ⅛th of the original size of the image
with respect to the field used.
IMREAD_REDUCED_GRAYSCALE_2
IMREAD_REDUCED_GRAYSCALE_4
IMREAD_REDUCED_GRAYSCALE_8
6
If the flag is set to this value, the image is read as a single-channel grayscale
image, and the size of the image is reduced to ½, ¼th or ⅛th of the original size
of the image with respect to the field used.
IMREAD_UNCHANGED
7
If the flag is set to this value, the loaded image is returned as it is.
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 24/100
7/11/2019 OpenCV Quick Guide
imageView.setX(10);
imageView.setY(10);
// Creating a WritableImage
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
System.out.println("Image Read");
return writableImage;
}
public static void main(String args[]) throws Exception {
launch(args);
}
}
Input Image
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 25/100
7/11/2019 OpenCV Quick Guide
Assume that following is the input image sample.jpg specified in the above program.
Output Image
On executing the program, you will get the following output.
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 26/100
7/11/2019 OpenCV Quick Guide
// Creating a WritableImage
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
System.out.println("Image read");
return writableImage;
}
public static void main(String args[]) throws Exception {
launch(args);
}
}
Input Image
Assume that following is the input image sample.jpg specified in the above program.
Output Image
On executing the program, you will get the following output.
code − An integer code representing the type of the conversion, for example, RGB
to Grayscale.
You can convert colored images to gray scale by passing the code
Imgproc.COLOR_RGB2GRAY along with the source and destination matrices as a
parameter to the cvtColor() method.
Example
The following program demonstrates how to read a colored image as a grayscale image
and display it using JavaFX window.
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 28/100
7/11/2019 OpenCV Quick Guide
imageView.setFitWidth(600);
//Converting the image to gray sacle and saving it in the dst matrix
Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY);
//Creating a WritableImage
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
System.out.println("Converted to Grayscale");
return writableImage;
}
public static void main(String args[]) throws Exception {
launch(args);
}
}
Input Image
Assume that following is the input image sample.jpg specified in the above program.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 29/100
7/11/2019 OpenCV Quick Guide
Output Image
On executing the program, you will get the following output.
threshold(Mat src, Mat dst, double thresh, double maxval, int type)
type − An integer code representing the type of the conversion, for example, RGB
to Grayscale.
You can convert a grayscale image to binary image by passing the code
Imgproc.THRESH_BINARY along with the values to the remaining parameters.
Example
The following program demonstrates how to read a colored image as a binary image and
display it using JavaFX window.
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 31/100
7/11/2019 OpenCV Quick Guide
System.out.println("Converted to binary");
return writableImage;
}
public static void main(String args[]) throws Exception {
launch(args);
}
}
Input Image
Assume that following is the input image sample.jpg specified in the above program.
Output Image
On executing the program, you will get the following output.
Example
The following program demonstrates how to read a grayscale image as a binary image and
display it using JavaFX window.
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 32/100
7/11/2019 OpenCV Quick Guide
imageView.setY(10);
System.out.println("Converted to binary");
return writableImage;
}
public static void main(String args[]) throws Exception {
launch(args);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 33/100
7/11/2019 OpenCV Quick Guide
}
}
Input Image
Assume that following is the input image sample.jpg specified in the above program.
Output Image
On executing the program, you will get the following output.
You can draw a circle on an image using the method circle() of the imgproc class.
Following is the syntax of this method −
mat − A Mat object representing the image on which the circle is to be drawn.
radius − A variable of the type integer representing the radius of the circle.
Example
The following program demonstrates how to draw a circle on an image and display it using
JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 34/100
7/11/2019 OpenCV Quick Guide
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
DrawingCircle obj = new DrawingCircle();
WritableImage writableImage = obj.LoadImage();
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
//Drawing a Circle
Imgproc.circle (
matrix, //Matrix obj of the image
new Point(230, 160), //Center of the circle
100, //Radius
new Scalar(0, 0, 255), //Scalar object for color
10 //Thickness of the circle
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 35/100
7/11/2019 OpenCV Quick Guide
);
return writableImage;
}
public static void main(String args[]) {
launch(args);
}
}
On executing the above program, you will get the following output −
mat − A Mat object representing the image on which the line is to be drawn.
pt1 and pt2 − Two Point objects representing the points between which the line
is to be drawn.
Example
The following program demonstrates how to draw a line on an image and display it using
JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 36/100
7/11/2019 OpenCV Quick Guide
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 37/100
7/11/2019 OpenCV Quick Guide
// Drawing a line
Imgproc.line (
matrix, //Matrix obj of the image
new Point(10, 200), //p1
new Point(300, 200), //p2
new Scalar(0, 0, 255), //Scalar object for color
5 //Thickness of the line
);
// Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", matrix, matOfByte);
this.matrix = matrix;
On executing the above program, you will get the following output −
mat − A Mat object representing the image on which the rectangle is to be drawn.
pt1 and pt2 − Two Point objects representing the vertices of the rectangle that is
to be drawn.
Example
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 38/100
7/11/2019 OpenCV Quick Guide
The following example demonstrates how to draw a rectangle on an image and display it
using JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
DrawingRectangle obj = new DrawingRectangle();
WritableImage writableImage = obj.LoadImage();
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 39/100
7/11/2019 OpenCV Quick Guide
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
// Drawing a Rectangle
Imgproc.rectangle (
matrix, //Matrix obj of the image
new Point(130, 50), //p1
new Point(300, 280), //p2
new Scalar(0, 0, 255), //Scalar object for color
5 //Thickness of the line
);
this.matrix = matrix;
On executing the above program, you will get the following output −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 40/100
7/11/2019 OpenCV Quick Guide
The constructor of the RotatedRect class accepts an object of the class Point, an object
of the class Size, and a variable of the type double, as shown below.
Example
The following program demonstrates how to draw an ellipse on an image and display it
using JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.RotatedRect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
DrawingEllipse obj = new DrawingEllipse();
WritableImage writableImage = obj.LoadImage();
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 41/100
7/11/2019 OpenCV Quick Guide
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
// Drawing an Ellipse
Imgproc.ellipse (
matrix, //Matrix obj of the image
new RotatedRect ( // RotatedRect(Point c, Size s, double a)
new Point(200, 150),
new Size(260, 180), 180
),
new Scalar(0, 0, 255), //Scalar object for color
10 //Thickness of the line
);
this.matrix = matrix;
return writableImage;
}
public static void main(String args[]) {
launch(args);
}
}
On executing the above program, you will get the following output −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 42/100
7/11/2019 OpenCV Quick Guide
mat − A Mat object representing the image on which the Polylines are to be
drawn.
isClosed − A parameter of the type boolean specifying weather the polylines are
closed.
The constructor of the MatOfPoint class accepts objects of the class Point.
MatOfPoint(Point... a)
Example
The following program demonstrates how to draw polylines on an image and display it
using JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 43/100
7/11/2019 OpenCV Quick Guide
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
DrawingPolyLines obj = new DrawingPolyLines();
WritableImage writableImage = obj.LoadImage();
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
this.matrix = matrix;
On executing the above program, you will get the following output −
mat − A Mat object representing the image on which the convex Polylines are to
be drawn.
scalar − A Scalar object representing the color of the convex Polylines. (BGR)
The constructor of the MatOfPoint class accepts objects of the class Point.
MatOfPoint(Point... a)
Example
The following program demonstrates how to draw convex polylines on an image and
display it using JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 45/100
7/11/2019 OpenCV Quick Guide
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
FillConvexPoly obj = new FillConvexPoly();
WritableImage writableImage = obj.LoadImage();
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 46/100
7/11/2019 OpenCV Quick Guide
On executing the above program, you will get the following output −
mat − A Mat object representing the image on which the arrowed line is to be
drawn.
pt1 and pt2 − Two Point objects representing the points between which the
arrowed line is to be drawn.
scalar − A Scalar object representing the color of the arrowed line. (BGR)
Example
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 47/100
7/11/2019 OpenCV Quick Guide
The following program demonstrates how to draw arrowed line on an image and display it
using JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
DrawingArrowedLine obj = new DrawingArrowedLine();
WritableImage writableImage = obj.LoadImage();
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 48/100
7/11/2019 OpenCV Quick Guide
public WritableImage LoadImage() throws Exception {
// Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/Aish.jpg";
Mat matrix = Imgcodecs.imread(file);
//Drawing a line
Imgproc.arrowedLine(
matrix, // Matrix obj of the image
new Point(10, 200), // p1
new Point(590, 200), // p2
new Scalar(0, 100, 255) // Scalar object for color
);
return writableImage;
}
public static void main(String args[]) {
launch(args);
}
}
On executing the above program, you will get the following output −
mat − A Mat object representing the image to which the text is to be added.
org − A Point object representing the bottom left corner text string in the image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 49/100
7/11/2019 OpenCV Quick Guide
fontScale − A variable of the type double representing the scale factor that is
multiplied by the font-specific base size.
scalar − A Scalar object representing the color of the text that is to be added.
(BGR)
thickness − An integer representing the thickness of the line by default, the value
of thickness is 1.
Example
The following program demonstrates how to add text to an image and display it using
JavaFX window.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Override
public void start(Stage stage) throws Exception {
// Capturing the snapshot from the camera
AddingTextToImage obj = new AddingTextToImage();
WritableImage writableImage = obj.LoadImage();
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 50/100
7/11/2019 OpenCV Quick Guide
Group root = new Group(imageView);
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap8/input.jpg";
Mat matrix = Imgcodecs.imread(file);
// Adding Text
Imgproc.putText (
matrix, // Matrix obj of the image
"Ravivarma's Painting", // Text to be added
new Point(10, 50), // point
Core.FONT_HERSHEY_SIMPLEX , // front face
1, // front scale
new Scalar(0, 0, 0), // Scalar object for color
4 // Thickness
);
On executing the above program, you will get the following output −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 51/100
7/11/2019 OpenCV Quick Guide
Blurring (smoothing) is the commonly used image processing operation for reducing the
image noise. The process removes high-frequency content, like edges, from the image and
makes it smooth.
In general blurring is achieved by convolving (each element of the image is added to its
local neighbors, weighted by the kernel) the image through a low pass filter kernel.
Blur (Averaging)
During this operation, the image is convolved with a box filter (normalized). In this
process, the central element of the image is replaced by the average of all the pixels in the
kernel area.
You can perform this operation on an image using the method blur() of the imgproc
class. Following is the syntax of this method −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
borderType − A variable of the type integer representing the type of the border
to be used to the output.
Example
The following program demonstrates how to perform the averaging (blur) operation on an
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat src = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 52/100
7/11/2019 OpenCV Quick Guide
// Creating an empty matrix to store the result
Mat dst = new Mat();
// blur(Mat src, Mat dst, Size ksize, Point anchor, int borderType)
// Writing the image
Imgcodecs.imwrite("E:/OpenCV/chap9/blur.jpg", dst);
System.out.println("Image processed");
}
}
Assume that following is the input image sample.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
You can perform this operation on an image using the Gaussianblur() method of the
imgproc class. Following is the syntax of this method −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
sigmaX − A variable of the type double representing the Gaussian kernel standard
deviation in X direction.
Example
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 53/100
7/11/2019 OpenCV Quick Guide
The following program demonstrates how to perform the Gaussian blur operation on an
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat src = Imgcodecs.imread(file);
Assume that following is the input image sample.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
You can perform this operation on an image using the medianBlur() method of the
imgproc class. Following is the syntax of this method −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 54/100
7/11/2019 OpenCV Quick Guide
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
Example
The following program demonstrates how to perform the median blur operation on an
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image sample.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
Bilateral Filter
The Bilateral Filter operation applies a bilateral image to a filter. You can perform this
operation on an image using the medianBlur() method of the imgproc class. Following is
the syntax of this method.
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
sigmaColor − A variable of the type integer representing the filter sigma in the
color space.
sigmaSpace − A variable of the type integer representing the filter sigma in the
coordinate space.
Example
The following program demonstrates how to perform the Bilateral Filter operation on an
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap11/filter_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 56/100
7/11/2019 OpenCV Quick Guide
}
}
Assume that following is the input image filter_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
You can perform this operation on an image using the boxFilter() method of the imgproc
class. Following is the syntax of this method −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
ddepth − A variable of the type integer representing the depth of the output
image.
Normalize − A variable of the type boolean specifying weather the kernel should
be normalized.
Example
The following program demonstrates how to perform the Box Filter operation on an image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 57/100
7/11/2019 OpenCV Quick Guide
public static void main( String[] args ) {
// Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
// Reading the Image from the file and storing it in to a Matrix object
String file = "E:/OpenCV/chap11/filter_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image filter_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
ddepth − A variable of the type integer representing the depth of the output
image.
Example
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 58/100
7/11/2019 OpenCV Quick Guide
The following program demonstrates how to perform Sqrbox filter operation on a given
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap11/filter_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image filter_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
OpenCV - Filter2D
The Filter2D operation convolves an image with the kernel. You can perform this operation
on an image using the Filter2D() method of the imgproc class. Following is the syntax of
this method −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 59/100
7/11/2019 OpenCV Quick Guide
ddepth − A variable of the type integer representing the depth of the output
image.
Example
The following program demonstrates how to perform the Filter2D operation on an image.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
//Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap11/filter_input.jpg";
Mat src = Imgcodecs.imread(file);
Assume that following is the input image filter_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 60/100
7/11/2019 OpenCV Quick Guide
OpenCV - Dilation
Erosion and dilation are the two types of morphological operations. As the name implies,
morphological operations are the set of operations that process images according to their
shapes.
Based on the given input image a "structural element" is developed. This might be done in
any of the two procedures. These are aimed at removing noise and settling down the
imperfections, to make the image clear.
Dilation
This procedure follows convolution with some kernel of a specific shape such as a square
or a circle. This kernel has an anchor point, which denotes its center.
This kernel is overlapped over the picture to compute maximum pixel value. After
calculating, the picture is replaced with anchor at the center. With this procedure, the
areas of bright regions grow in size and hence the image size increases.
For example, the size of an object in white shade or bright shade increases, while the size
of an object in black shade or dark shade decreases.
You can perform the dilation operation on an image using the dilate() method of the
imgproc class. Following is the syntax of this method.
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
Example
You can prepare the kernel matrix using the getStructuringElement() method. This
method accepts an integer representing the morph_rect type and an object of the type
Size.
The following program demonstrates how to perform the dilation operation on a given
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 61/100
7/11/2019 OpenCV Quick Guide
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Input
Assume that following is the input image sample.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
OpenCV - Erosion
Erosion is quite a similar process as dilation. But the pixel value computed here is
minimum rather than maximum in dilation. The image is replaced under the anchor point
with that minimum pixel value.
With this procedure, the areas of dark regions grow in size and bright regions reduce. For
example, the size of an object in dark shade or black shade increases, while it decreases in
white shade or bright shade.
Example
You can perform this operation on an image using the erode() method of the imgproc
class. Following is the syntax of this method −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 62/100
7/11/2019 OpenCV Quick Guide
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
You can prepare the kernel matrix using the getStructuringElement() method. This
method accepts an integer representing the morph_rect type and an object of the type
Size.
The following program demonstrates how to perform the erosion operation on a given
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image processed");
}
}
Assume that following is the input image sample.jpg specified in the above program.
Output
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 63/100
7/11/2019 OpenCV Quick Guide
Image Loaded
If you open the specified path, you can observe the output image as follows −
src − An object of the class Mat representing the source (input) image.
dst − object of the class Mat representing the destination (output) image.
Example
The following program demonstrates how to apply the morphological operation "top-hat"
on an image using OpenCV library.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap12/morph_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image morph_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
More Operations
In addition to the morphological operation TOPHAT, demonstrated in the previous
example, OpenCV caters various other types of morphologies. All these types are
represented by predefined static fields (fixed values) of Imgproc class.
You can choose the type of the morphology you need by passing their respective
predefined value to the parameter op of the morphologyEx() method.
Following are the values representing the type of morphological operations and their
respective outputs.
MORPH_BLACKHAT
MORPH_CLOSE
MORPH_CROSS
MORPH_DILATE
MORPH_ELLIPSE
MORPH_ERODE
MORPH_GRADIENT
MORPH_OPEN
MORPH_RECT
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 65/100
7/11/2019 OpenCV Quick Guide
MORPH_TOPHAT
During the pyramid operation, the smoothness of the image is increased and the resolution
(size) is decreased.
Pyramid Up
In Pyramid Up, the image is initially up-sampled and then blurred. You can perform
Pyramid Up operation on an image using the pyrUP() method of the imgproc class.
Following is the syntax of this method −
src − An object of the class Mat representing the source (input) image.
mat − An object of the class Mat representing the destination (output) image.
size − An object of the class Size representing the size to which the image is to be
increased or decreased.
Example
The following program demonstrates how to perform the Pyramid Up operation on an
image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 66/100
7/11/2019 OpenCV Quick Guide
String file ="E:/OpenCV/chap13/pyramid_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image pyramid_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
Pyramid Down
In Pyramid Down, the image is initially blurred and then down-sampled. You can perform
Pyramid Down operation on an image using the pyrDown() method of the imgproc class.
Following is the syntax of this method −
src − An object of the class Mat representing the source (input) image.
mat − An object of the class Mat representing the destination (output) image.
size − An object of the class Size representing the size to which the image is to be
increased or decreased.
Example
The following program demonstrates how to perform the Pyramid Down operation on an
image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 67/100
7/11/2019 OpenCV Quick Guide
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap13/pyramid_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image pyramid_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
You can perform pyramid Mean Shift Filtering operation on an image using the pyrDown()
method of the imgproc class. Following is the syntax of this method.
src − An object of the class Mat representing the source (input) image.
mat − An object of the class Mat representing the destination (output) image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 68/100
7/11/2019 OpenCV Quick Guide
Example
The following program demonstrates how to perform a Mean Shift Filtering operation on a
given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap13/pyramid_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image pyramid_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
Simple Thresholding
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 69/100
7/11/2019 OpenCV Quick Guide
In simple thresholding operation the pixels whose values are greater than the specified
threshold value, are assigned with a standard value.
You can perform simple threshold operation on an image using the method threshold() of
the Imgproc class, Following is the syntax of this method.
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
Example
The following program demonstrates how to perform simple thresholding operation on an
image in OpenCV.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap14/thresh_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image thresh_input.jpg specified in the above
program.
Output
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 70/100
7/11/2019 OpenCV Quick Guide
Image Processed
If you open the specified path, you can observe the output image as follows −
You can choose the type of the threshold operation you need, by passing its respective
predefined value to the parameter named type of the threshold() method.
Following are the values representing various types of threshold operations and their
respective outputs.
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV
In OpenCV, you can perform Adaptive threshold operation on an image using the method
adaptiveThreshold() of the Imgproc class. Following is the syntax of this method.
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 71/100
7/11/2019 OpenCV Quick Guide
C − A variable of double type representing the constant used in the both methods
(subtracted from the mean or weighted mean).
Example
The following program demonstrates how to perform Adaptive threshold operation on an
image in OpenCV. Here we are choosing adaptive threshold of type binary and
ADAPTIVE_THRESH_MEAN_C for threshold method.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap14/thresh_input.jpg";
System.out.println("Image Processed");
}
}
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 72/100
7/11/2019 OpenCV Quick Guide
Assume that following is the input image thresh_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
Following are the values representing various combinations of values for the parameters
adaptiveMethod and thresholdType and their respective outputs.
THRESH_BINARY
THRESH_BINARY_INV
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
top − A variable of integer the type integer representing the length of the border
at the top of the image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 73/100
7/11/2019 OpenCV Quick Guide
bottom − A variable of integer the type integer representing the length of the
border at the bottom of the image.
left − A variable of integer the type integer representing the length of the border
at the left of the image.
right − A variable of integer the type integer representing the length of the border
at the right of the image.
borderType − A variable of the type integer representing the type of the border
that is to be used.
Example
Following program is an example demonstrating, how to add border to a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap15/input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image thresh_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 74/100
7/11/2019 OpenCV Quick Guide
You can choose the type of the threshold operation you need, by passing its respective
predefined value to the parameter named borderType of the copyMakeBorder()
method.
Following are the values representing various types of borders operations and their
respective outputs.
BORDER_CONSTANT
BORDER_ISOLATED
BORDER_DEFAULT
BORDER_REFLECT
BORDER_REFLECT_101
BORDER_REFLECT101
BORDER_REPLICATE
BORDER_WRAP
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 75/100
7/11/2019 OpenCV Quick Guide
Example
The following program demonstrates how to perform Sobel operation on a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap16/sobel_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image processed");
}
}
Assume that following is the input image sobel_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
sobel Variants
On passing different values to the last to parameters (dx and dy) (among 0 and 1), you
will get different outputs −
The following table lists various values for the variables dx and dy of the method Sobel()
and their respective outputs.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 76/100
7/11/2019 OpenCV Quick Guide
0 1
1 0
1 1
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
Example
The following program demonstrates how to apply scharr to a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap16/sobel_input.jpg";
Mat src = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 77/100
7/11/2019 OpenCV Quick Guide
System.out.println("Image processed");
}
}
Assume that following is the input image scharr_input.jpg specified in the above
program.
Output
On executing it, you will get the following output −
Image Processed
If you open the specified path you can observe the output image as follows −
Following is a table listing various values for the variables dx and dy of the method
scharr() and their respective outputs.
0 1
1 0
Unlike other operators Laplacian didn’t take out edges in any particular direction but it
takes out edges in following classification.
Inward Edges
Outward Edges
You can perform Laplacian Transform operation on an image using the Laplacian()
method of the imgproc class, following is the syntax of this method.
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
Example
The following program demonstrates how to perform Laplace transform operation on a
given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
//Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap18/laplacian_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image laplacian_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 79/100
7/11/2019 OpenCV Quick Guide
You can apply distance transform in OpenCV using the method distanceTransform().
Following is the syntax of this method.
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
Example
The following program demonstrates how to perform distance transformation operation on
a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap19/input.jpg";
Mat src = Imgcodecs.imread(file,0);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 80/100
7/11/2019 OpenCV Quick Guide
System.out.println("Image Processed");
}
}
Assume that following is the input image input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
You can choose the type of the distance transform operation you need, by passing its
respective predefined value to the parameter named distanceType of the
distanceTransform() method.
Following are the values representing various types of distanceTransform operations and
their respective outputs.
DIST_C
DIST_L1
DIST_L2
DIST_LABEL_PIXEL
DIST_MASK_3
Example
The following program demonstrates how to capture a frame using camera and display it
using JavaFX window. It also saves the captured frame.
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
@Override
public void start(Stage stage) throws FileNotFoundException, IOException {
// Capturing the snapshot from the camera
CameraSnapshotJavaFX obj = new CameraSnapshotJavaFX();
WritableImage writableImage = obj.capureSnapShot();
// If camera is opened
if( capture.isOpened()) {
// If there is next video frame
if (capture.read(matrix)) {
// Creating BuffredImage from the matrix
BufferedImage image = new BufferedImage(matrix.width(),
matrix.height(), BufferedImage.TYPE_3BYTE_BGR);
// Saving it again
imageCodecs.imwrite(file, matrix);
}
public static void main(String args[]) {
launch(args);
}
}
Output
On executing the program, you will get the following output.
If you open the specified path, you can observe the same frame which is saved as a jpg
file.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 84/100
7/11/2019 OpenCV Quick Guide
Example
The following program demonstrates how to detect faces in an image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap23/facedetection_input.jpg";
Mat src = Imgcodecs.imread(file);
// Drawing boxes
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(
src, // where to draw the box
new Point(rect.x, rect.y), // bottom left
new Point(rect.x + rect.width, rect.y + rect.height), // top right
new Scalar(0, 0, 255),
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 85/100
7/11/2019 OpenCV Quick Guide
3 // RGB colour
);
}
System.out.println("Image Processed");
}
}
Assume that following is the input image facedetection_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Detected 3 faces
Image Processed
If you open the specified path, you can observe the output image as follows −
Example
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 86/100
7/11/2019 OpenCV Quick Guide
@Override
public void start(Stage stage) throws FileNotFoundException, IOException {
// Capturing the snapshot from the camera
faceDetectionJavaFXX obj = new faceDetectionJavaFXX();
WritableImage writableImage = obj.capureFrame();
// If camera is opened
if(!capture.isOpened()) {
System.out.println("camera not detected");
} else
System.out.println("Camera detected ");
// Drawing boxes
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(
matrix, //where to draw the box
new Point(rect.x, rect.y), //bottom left
new Point(rect.x + rect.width, rect.y + rect.height), //top right
new Scalar(0, 0, 255) //RGB colour
);
}
// Creating BuffredImage from the matrix
BufferedImage image = new BufferedImage(matrix.width(), matrix.height(),
BufferedImage.TYPE_3BYTE_BGR);
this.matrix = matrix;
// Saving it again
imageCodecs.imwrite(file, matrix);
}
public static void main(String args[]) {
launch(args);
}
}
Output
On executing the program, you will get the following output.
If you open the specified path, you can see the same snapshot saved as a jpg image.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 88/100
7/11/2019 OpenCV Quick Guide
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
size − A variable of the type integer representing the size of the output image.
Example
The following program demonstrates how to apply affine operation on a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap24/transform_input.jpg";
Mat src = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 89/100
7/11/2019 OpenCV Quick Guide
System.out.println("Image Processed");
}
}
Assume that following is the input image transform_input.jpg specified in the above
program.
Output
On executing it, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
OpenCV - Rotation
You can perform rotation operation on an image using the warpAffine() method of the
imgproc class. Following is the syntax of this method −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
size − A variable of the type integer representing the size of the output image.
Example
The following program demonstrates how to rotate an image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap24/transform_input.jpg";
Mat src = Imgcodecs.imread(file);
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 90/100
7/11/2019 OpenCV Quick Guide
System.out.println("Image Processed");
}
}
Assume that following is the input image transform_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
OpenCV - Scaling
You can perform scaling on an image using the resize() method of the imgproc class.
Following is the syntax of this method.
resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
fx − A variable of the type double representing the scale factor along the
horizontal axis.
fy − A variable of the type double representing the scale factor along the vertical
axis.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 91/100
7/11/2019 OpenCV Quick Guide
Example
The following program demonstrates how to apply scale transformation to an image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap24/transform_input.jpg";
Mat src = Imgcodecs.imread(file);
System.out.println("Image Processed");
}
}
Assume that following is the input image transform_input.jpg specified in the above
program (size − Width:300px and height:300px).
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows (size −
Width:600px and height:600px) −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 92/100
7/11/2019 OpenCV Quick Guide
In OpenCV, you can apply different color maps to an image using the method
applyColorMap() of the class Imgproc. Following is the syntax of this method −
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the destination (output) image.
colormap − A variable of integer type representing the type of the color map to
be applied.
Example
The following program demonstrates how to apply color map to an image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap25/color_input.jpg";
Mat src = Imgcodecs.imread(file);
Assume that following is the input image color_input.jpg specified in the above program.
Output
On executing the above program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 93/100
7/11/2019 OpenCV Quick Guide
More Operations
In addition to COLORMAP_HOT demonstrated in the previous example, OpenCV caters
various other types of color maps. All these types are represented by predefined static
fields (fixed values) of Imgproc class.
You can choose the type of the colormap you need, by passing its respective predefined
value to the parameter named colormap of the applyColorMap() method.
Following are the values representing various types of color maps and their respective
outputs.
COLORMAP_AUTUMN
COLORMAP_BONE
COLORMAP_COOL
COLORMAP_HOT
COLORMAP_HSV
COLORMAP_JET
COLORMAP_OCEAN
COLORMAP_PARULA
COLORMAP_PINK
COLORMAP_RAINBOW
COLORMAP_SPRING
COLORMAP_SUMMER
COLORMAP_WINTER
You can perform this operation on an image using the Canny() method of the imgproc
class, following is the syntax of this method.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 94/100
7/11/2019 OpenCV Quick Guide
image − A Mat object representing the source (input image) for this operation.
edges − A Mat object representing the destination (edges) for this operation.
threshold1 − A variable of the type double representing the first threshold for the
hysteresis procedure.
threshold2 − A variable of the type double representing the second threshold for
the hysteresis procedure.
Example
Following program is an example demonstrating, how to perform Canny Edge Detection
operation on a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file = "E:/OpenCV/chap17/canny_input.jpg";
Assume that following is the input image canny_input.jpg specified in the above
program.
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 95/100
7/11/2019 OpenCV Quick Guide
Output
On executing the above program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
image − An object of the class Mat representing the source (input) image.
lines − An object of the class Mat that stores the vector that stores the
parameters (r, Φ) of the lines.
rho − A variable of the type double representing the resolution of the parameter r
in pixels.
theta − A variable of the type double representing the resolution of the parameter
Φ in radians.
Example
The following program demonstrates how to detect Hough lines in a given image.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file = "E:/OpenCV/chap21/hough_input.jpg";
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 96/100
7/11/2019 OpenCV Quick Guide
// Detecting edges of it
Mat canny = new Mat();
Imgproc.Canny(src, canny, 50, 200, 3, false);
System.out.println(lines.rows());
System.out.println(lines.cols());
a = Math.cos(theta);
b = Math.sin(theta);
x0 = a*rho;
y0 = b*rho;
System.out.println("Image Processed");
}
}
Assume that following is the input image hough_input.jpg specified in the above
program.
Output
On executing the program, you will get the following output −
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 97/100
7/11/2019 OpenCV Quick Guide
143
1
Image Processed
If you open the specified path, you can observe the output image as follows −
Histogram equalization improves the contrast of an image, in order to stretch out the
intensty range. You can equalize the histogram of a given image using the method
equalizeHist() of the Imgproc class. Following is the syntax of this method.
equalizeHist(src, dst)
src − An object of the class Mat representing the source (input) image.
dst − An object of the class Mat representing the output. (Image obtained after
equalizing the histogram)
Example
The following program demonstrates how to equalize the histogram of a given image.
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap20/histo_input.jpg";
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 98/100
7/11/2019 OpenCV Quick Guide
// Applying blur
Imgproc.blur(equ, equ, new Size(3, 3));
// Applying color
Imgproc.cvtColor(equ, equ, Imgproc.COLOR_BGR2YCrCb);
List<Mat> channels = new ArrayList<Mat>();
Imgcodecs.imwrite("E:/OpenCV/chap20/histo_output.jpg", equ);
System.out.println("Image Processed");
}
}
Assume that following is the input image histo_input.jpg specified in the above program.
Output
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows −
Advertisements
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 99/100
7/11/2019 OpenCV Quick Guide
https://www.tutorialspoint.com/opencv/opencv_quick_guide.htm 100/100