Introduction
In this tutorial I will show you the OpenCV functionality to render text. Again, it is not very powerful and sophisticated but it does the job. In the previous tutorial we saw how to render basic shapes. Now it is time to render some text. In OpenCV we have ready functions for this and now we are going it see their usage.
Text rendering related functions
The putText() function
OpenCV provides the putText() function. This function takes as a first argument the image matrix, where it will render the text on. The second argument is the text string. Next, we provide the origin point, where the rendering of the text will start from. It is one Point object. After that we need to provide the font face or the font family. OpenCV supports only a small set of fonts to be used. These font types are available as the following enum: enum cv::HersheyFonts. Note that OpenCV does not provide a lot of font types so we will have to use what is available.
The next argument is the font scale – it is factor that OpenCV multiplies by the font-specific base size. There is a function to calculate this factor depending on the provided font type and pixel size. We will see it later. Again we have the color, which we provide as a Scalar object. We have the line thickness and line type. Lastly there is one argument stating whether to use the bottom-left or the top-left corner of the text as an origin.
Keep in mind that if the font that we are using does not support a given character, OpenCV will render this character as a question mark. Here you can find more information about the putText() function.
The getFontScaleFromHeight() function
Another function from OpenCV that supports the text rendering is the getFontScaleFromHeight(). This is a helper function, which will calculate the scale factor from the font type we are going to use, its pixel height and line thickness. Here is more information about it.
The getTextSize() function
Lastly, we have another helper function – getTextSize(). It calculates the width and height of a given text string. This function uses the text itself, the font type, font scale and the line thickness. More information about it here.
Text rendering demo application
I have prepared one small program to demonstrate the text rendering over another image. Here is the code.
using namespace cv; void displayText() { // Path to the input image std::string l_pathToInputImage{ "../Resources/city.jpg" }; // Create an object to hold the image data of the first image Mat l_image1; // Read the image date from a file with no change to color scheme l_image1 = imread(l_pathToInputImage, IMREAD_UNCHANGED); // Check if we have read the first image data correctly if (!l_image1.data) { std::cout << "No image data \n"; return; } constexpr HersheyFonts l_fontType = HersheyFonts::FONT_HERSHEY_SCRIPT_SIMPLEX; constexpr int l_lineThickness = 5; // Calculate the font scale factor double l_fontScale{ getFontScaleFromHeight(l_fontType, 72, l_lineThickness) }; const String l_text{ "With my hommie" }; // Calculate the text string size Size l_textSize{ getTextSize(l_text, l_fontType, l_fontScale, l_lineThickness, nullptr) }; // Find the center point for the text Point l_imageCenter{ l_image1.cols / 2 - l_textSize.width / 2, l_image1.rows / 2 - l_textSize.height / 2 }; // Render the text putText(l_image1, l_text, l_imageCenter, l_fontType, l_fontScale, Scalar(255, 0, 0), l_lineThickness, LINE_8, false); // Display the input image namedWindow("Input", WINDOW_NORMAL); cv::imshow("Input", l_image1); }
Firstly I read one image from a file and check if the image data is read correctly. After that I calculate the font scale by using the getFontScaleFromHeight() function. I provide this function with the font type, the pixel size and the line thickness. Then I create one variable to hold the string I want to render. Next I am getting the text width and height using the getTextSize() function. I am providing this function with the text I want to render, the font type, the scale I have calculated previously and the line thickness. I don’t need to get the baseline so I leave the last argument to be nullptr. Lastly before rendering the text i find the point where I need to put the text in order to be centered horizontally and vertically.
Now I render the text using the putText() function. The first argument is the image matrix we will be rendering on. Then the next argument is the text itself. After that I provide the point where the text should start from. Next I provide the font type and the font scale, followed by the font color. Then the next two arguments are the line thickness and the line type. The last argument is set to false because I want the origin to be the top-left corner. After rendering I display the result on a window. Here is how it looks on my side.

Conclusion
In this tutorial we saw how to render a text using OpenCV functions. This is quite an useful functionality. Even though OpenCV does not have a very sophisticated rendering engine, it is more than enough to do the job.
The project is again available as an archive. You can also download it from GitHub.
Next we will see how to apply blurring to an image and smooth it, removing any noise, applied during the process of taking the image.

Passionate developer, loving husband and caring father. As an introvert programming is my life. I work as a senior software engineer in a big international company. My hobbies include playing computer games (mostly World of Warcraft), watching TV series and hiking.