Show menu >>

Cv2.Puttext

Cv2.Puttext Online ZIP

OpenCV - An open source computer vision and machine learning library. It includes more than 2,500 algorithms, which include both classical and modern algorithms for computer vision and machine learning.
2021-11-21, by ,

#CV2 || #Python || #Library ||

Table of contents:



In the previous lesson, we learned how to get a stream of frames from a webcam and apply various transformations to them. Today we will consider a few more useful functions that will help us display important information in the frame.

  • frame - the image on which we overlay the text;
  • text - of course, the text that we are going to display in the frame;
  • coordinates - a tuple of two coordinates of the lower left corner of the text, for example (5,10);
  • font type is one of the constants listed below;
  • font scale - the font has a certain standard size, which is quite large. This option allows you to shrink or enlarge the font relative to the standard. For example, to increase twice - write 2, to decrease 2 times - 0.5;
  • color is a tuple of three numbers from 0 to 255 that specify the color in the RGB model. Remember that in this tuple, colors go backwards: BGR. Blue color - (255,0,0);
  • pen thickness - optional;
  • line type - an optional parameter, one of three values: LINE_8 small dotted line, LINE_4 - large dotted line, LINE_AA - smoothed line;
  • coordinate center - optional. By default, text coordinates are measured from the upper left corner. If this parameter is True, then they will be from the bottom left corner.

To write text on an image using the OpenCV Python library, use the putText () method. The use of the function is shown in the following example.

import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Write some Text
font                   = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale              = 1
fontColor              = (255,255,255)
lineType               = 2
cv2.putText(img,'Hello World!',
    bottomLeftCornerOfText,
    font,
    fontScale,
    fontColor,
    lineType)
#Display the image
cv2.imshow("img",img)
#Save image
cv2.imwrite("out.jpg", img)
cv2.waitKey(0)

Text in the center of the image

If you know the shape (width, height) of the text you write on the image, you can place it in the center of the image. The approximate form of the text in the above example is (268, 36). You may have to find the shape of your specific text using Paint or another application.

Contours - object recognition

Object recognition is performed using color segmentation of the image. There are two functions for this: cv2.findContours and cv2.drawContours.

This article details object detection using color segmentation. Everything you need for her is there.

Digital Marketing Agency