Printing text on images

OpenCV contains a very easy-to-use function named putText to draw, or print, text on images. This function requires an image as the input/output parameter, which means the source image itself will be updated. So, be sure to make a copy of your original image in memory before calling this function. You also need to provide this function with an origin point, which is simply the point where the text will be printed. The font of the text must be one of the entries in the HersheyFonts enum, which can take one (or a combination) of the following values:

  • FONT_HERSHEY_SIMPLEX
  • FONT_HERSHEY_PLAIN
  • FONT_HERSHEY_DUPLEX
  • FONT_HERSHEY_COMPLEX
  • FONT_HERSHEY_TRIPLEX
  • FONT_HERSHEY_COMPLEX_SMALL
  • FONT_HERSHEY_SCRIPT_SIMPLEX
  • FONT_HERSHEY_SCRIPT_COMPLEX
  • FONT_ITALIC

For details of how each entry looks when printed, you can check out OpenCV or simply search online for more information about Hershey fonts.

Apart from the parameters we just mentioned, you also need a few additional parameters, such as the scale, color, thickness, and line type of the text. Let's break them all down with a simple example.

Here's an example code that demonstrates the usage of the putText function:

string text = "www.amin-ahmadi.com"; 
int offset = 25; 
Point origin(offset, image.rows - offset); 
HersheyFonts fontFace = FONT_HERSHEY_COMPLEX; 
double fontScale = 1.5; 
Scalar color(0, 242, 255); 
int thickness = 2; 
LineTypes lineType = LINE_AA; 
bool bottomLeftOrigin = false; 
 
putText(image, 
        text, 
        origin, 
        fontFace, 
        fontScale, 
        color, 
        thickness, 
        lineType, 
        bottomLeftOrigin); 

When executed on our example picture from the previous chapters, the following result will be created:

Obviously, increasing or decreasing scale will result in an increased or decreased text size. The thickness parameter corresponds to the thickness of the printed text and so on. The only parameter that is worth discussing more is lineType, which was LINE_AA in our example, but it can take any value from the LineTypes enum. Here are the most important line types along with their differences, demonstrated with a printed W character on a white background:

LINE_4 stands for four-connected line types, and LINE_8 stands for eight-connected line types. LINE_AA though, which is the anti-aliased line type, is slower to draw than the other two, but, as can be seen in the preceding diagram, it also provides a much better quality.

The LineTypes enum also includes a FILLED entry, which is used for filling the shapes drawn on an image with the given color. It's important to note that almost all drawing functions in OpenCV (not just putText) require a line type parameter.

OpenCV provides two more functions that are related to handling texts, but not exactly for drawing them. The first one is called getFontScaleFromHeight, which is used to get the required scale value based on a font type, height (in pixels), and thickness. Here's an example:

double fontScale = getFontScaleFromHeight(fontFace, 
                           50, // pixels for height 
                           thickness); 

We could have used the preceding code instead of providing a constant value for scale in our previous example usage of the putText function. Obviously, we need to replace 50 with any desired pixel height value for our text.

Besides getFontScaleFromHeight, OpenCV also includes a function named getTextSize that can be used to retrieve the width and height that is required for printing a specific text on an image. Here's an example code that shows how we can find out the width and height in pixels required for printing the "Example" word with the FONT_HERSHEY_PLAIN font type, a scale of 3.2, and a thickness of 2 on an image using the getTextSize function:

int baseLine; 
Size size = getTextSize("Example", 
                        FONT_HERSHEY_PLAIN, 
                        3.2, 
                        2, 
                        &baseLine); 
 
cout << "Size = " << size.width << " , " << size.height << endl; 
cout << "Baseline = " << baseLine << endl; 

The results should look like the following:

Size = 216 , 30 
Baseline = 17 

This means the text will need 216 by 30 pixels of space to be printed and the baseline will be 17 pixels farther from the bottom of the text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.36.38