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
import org.opencv.*;
import org.opencv.imgproc.Imgproc;
public class GFG {
public static void main(String args[]) throws Exception
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String src = "D:\\InputImage.jpg" ;
Mat image = Imgcodecs.imread(src);
String text = "GFG IS COOL" ;
Point org = new Point( 170 , 280 );
Scalar color = new Scalar( 0 , 0 , 255 );
int fontType = Imgproc.FONT_HERSHEY_PLAIN;
int fontSze = 1 ;
int thickness = 3 ;
Imgproc.putText(image, text, org, fontType,
fontSize, color, thickness);
HighGui.imshow( "" , image);
HighGui.waitKey();
}
}
|
Output: The image after adding text is as follows:
Output Image
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...