Related Articles

Related Articles

Java Program to Add Text to an image in OpenCV
  • Last Updated : 24 Nov, 2020

OpenCV is the cross-platform open-source library for computer vision, image processing, and machine learning.  It plays a major role nowadays in real-time operations in improving modules provides adequate functions for image manipulation. It has C++, C, Python, and Java interfaces and supports Windows, Linux, macOS, iOS, and Android. By using it, one can even process images and videos to identify objects, faces, or even handwriting of a human

Here, putText() which is an inbuilt method will be used after importing this method from its respective module given below in the java program.

Syntax: to import module to deal with images:

import org.opencv.imgproc.Imgproc;

Syntax: to use putText() method of this class is as follows:

putText(image, text, org, fontType, fontSize, color, thickness)


Datatype of parameters Name of parameter Functionalities
Mat object image Text to be added to an input image object
String text Text to be inserted to an input image
Point (tuple) org Coordinates of the bottom left corner of a text string in the image
Integer fontType Depicting the style of fonts
Double fontSize Size of text to be added over an input image 
Scalar color Color of the text string to be drawn over an input image
Integer thickness The thickness of the line in the unit, by default it is unity

Exception: No exception is thrown by this method of this class because simply the tuple is passed. For example in BGR color spectrum, the blue light tuple is as follows: (255,0,0)

Implementation: Input image is as follows been randomly taken. Now text- “GFG IS COOL” is to be added over this image.

Input Image






// Importing all OpenCV files
import org.opencv.*;
import org.opencv.imgproc.Imgproc;
public class GFG {
    // Main driver code
    public static void main(String args[]) throws Exception
        // Loading the OpenCV core library
        // Reading the contents of the image
        // from local computer directory
        String src = "D:\\InputImage.jpg";
        // Creating a Mat object
        Mat image = Imgcodecs.imread(src);
        // Text to be added
        String text = "GFG IS COOL";
        // Points from where text should be added
        Point org = new Point(170, 280);
        // Color of the text
        Scalar color = new Scalar(0, 0, 255);
        // Fonttype of the text to be added
        int fontType = Imgproc.FONT_HERSHEY_PLAIN;
        // Fontsize of the text to be added
        int fontSze = 1;
        // Thickness of the lines in px
        int thickness = 3;
        // Adding text to the image using putText method
        Imgproc.putText(image, text, org, fontType,
                        fontSize, color, thickness);
        // Displaying the Image after adding the Text
        HighGui.imshow("", image);
        // Waiting for a key event to delay


Output: The image after adding text is as follows:

Output Image

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :