
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to draw Image Contours using Java OpenCV library?
Contours is nothing but the line joining all the points along the boundary of a particular shape. Using this you can −
Find the shape of an object.
Calculate the area of an object.
Detect an object.
Recognize an object.
You can find the contours of various shapes, objects in an image using the findContours() method. In the same way you can draw
You can draw the found contours of an image using the drawContours() method this method accepts the following parameters −
An empty Mat object to store the result image.
A list object containing the contours found.
An integer value specifying the contour to draw (-ve value to draw all of them).
A Scalar object to specify the color of the contour.
An integer value to specify the thickness of the contour.
Example
import java.util.ArrayList; import java.util.List; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawingContours { public static void main(String args[]) throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); String file ="D:\Images\shapes.jpg"; Mat src = Imgcodecs.imread(file); //Converting the source image to binary Mat gray = new Mat(src.rows(), src.cols(), src.type()); Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); Mat binary = new Mat(src.rows(), src.cols(), src.type(), new Scalar(0)); Imgproc.threshold(gray, binary, 100, 255, Imgproc.THRESH_BINARY_INV); //Finding Contours List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchey = new Mat(); Imgproc.findContours(binary, contours, hierarchey, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); //Drawing the Contours Scalar color = new Scalar(0, 0, 255); Imgproc.drawContours(src, contours, -1, color, 2, Imgproc.LINE_8, hierarchey, 2, new Point() ) ; HighGui.imshow("Drawing Contours", src); HighGui.waitKey(); } }
Input Image
Output
On executing, the above program generates the following window −
- Related Articles
- How to find Image Contours using Java OpenCV library?
- How to draw geometrical shapes on image using OpenCV Java Library?
- How to draw markers on an image using Java OpenCV library?
- Detecting contours in an image using OpenCV
- How to write an image using Java OpenCV library?
- How to flip an image using Java OpenCV library?
- Find and Draw Contours using OpenCV in Python
- How to create a mirror image using Java OpenCV library?
- How to convert a negative image to positive image using Java OpenCV library?
- How to convert a colored image to Sepia image using Java OpenCV library?
- How to convert RGB image to HSV using Java OpenCV library?
- How to convert HSV to colored image using Java OpenCV library?
- How to convert HSV to BGR image using Java OpenCV library?
- How to convert HLS to colored image using Java OpenCV library?
- How to convert colored image to HLS using Java OpenCV library?
