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)
Parameters:
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
Java
// 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
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 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
HighGui.waitKey();
}
}
Output: The image after adding text is as follows:
Output Image
Similar Reads
Java Program to Copy and Paste an image in OpenCV OpenCV is a Machine Learning and open-source computer vision software library, the main purpose for which it was developed is to enable a common medium to increase the use of machine perception in commercial businesses and accelerate the development of computer vision applications, it is a smooth an
3 min read
Java OpenCV Programs - Basic to Advanced Java is a popular programming language that can be used to create various types of applications, such as desktop, web, enterprise, and mobile. Java is also an object-oriented language, which means that it organizes data and behaviour into reusable units called classes and objects. Java is known for
2 min read
Java Program to Convert PNG Images to JPEG PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each oth
4 min read
Java Program to Add Tables to a Word Document OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision. It has C++, Python, Java, and MATLAB int
3 min read
Adding Water Marks to the Images in a PDF using Java In this article, we will learn how to add watermarks to the images in a PDF document using Java. Adding watermarks to the images in a PDF, we will use the iText library. These are the steps that should be followed to add watermarks to the images in a PDF using java. 1. Creating a PdfWriter object Th
4 min read
Setting the Position of the Image in PDF Document using Java To set the Position of the Image in a PDF document using Java multiple external dependencies need to download first. Setting the Position of the Image in a PDF, use the iText library. These are the steps that should be followed to Set the Position of the Image in a PDF using java. 1. Creating a PdfW
3 min read