Problem when displaying text with cv2.QT_FONT_BOLD source using Opencv in Python

Asked

Viewed 405 times

0

I’m having trouble showing a specific text in an image, I believe the problem is in the source used (cv2.QT_FONT_BOLD), because when I change the source to another as cv2.FONT_HERSHEY_COMPLEX_SMALL, it works normally.

texto = "{}".format(str(round(3.141592*raio*100*fator_conv**2,2))) #cm²
texto2 = "{}".format(str(round(2*raio*fator_conv*100,2))) #cm

cv2.putText(img, texto, (int(cx-0.5*raio),int(cy-0.5*raio)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5, (0, 0, 255))
cv2.putText(img, texto2, (int(cx-0.5*raio), int(cy+0.5*raio)), cv2.QT_FONT_BOLD, 1.5, (255, 0, 0))

The program shows the following error:

  Traceback (most recent call last):
  File "D:/Projetos/codigos/Detect_HOG+SVM/deteccao_2.py", line 168, in <module>
    cv2.putText(img, texto2, (int(cx-0.5*raio), int(cy+0.5*raio)), cv2.QT_FONT_BOLD, 1.5, (255, 0, 0))
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2216: error: (-211:One of arguments' values is out of range) Unknown font type in function 'cv::getFontData'


Process finished with exit code 1

I thought it was because I didn’t have pyqt5 installed in Anaconda’s virtual environment, I installed it and it’s still the same error. I read some tutorials on how to use Qt with python, added

from PyQt5 import QtCore, QtGui

But it didn’t work out. Does anyone have any idea where I might be failing?

1 answer

1


Opencv

By looking at the code of drawing.cpp and in the documentation, you can see that the function parameters putText sane:

Parameters

img Image.

text Text string to be Drawn.

org Bottom-left corner of the text string in the image.

font Font type, see Hersheyfonts.

fontScale Font Scale factor that is multiplied by the font-specific size base.

color Color text.

Thickness Thickness of the Lines used to draw a text.

lineType Line type. See Linetypes

bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

Example:

img = cv.putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

And in the font type, an invalid font is being used: cv2.QT_FONT_BOLD, because the types of sources accepted are:

| cv.FONT_HERSHEY_SIMPLEX        | normal size sans-serif font                                          |
| cv.FONT_HERSHEY_PLAIN          | small size sans-serif font                                           |
| cv.FONT_HERSHEY_DUPLEX         | normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX) |
| cv.FONT_HERSHEY_COMPLEX        | normal size serif font                                               |
| cv.FONT_HERSHEY_TRIPLEX        | normal size serif font (more complex than FONT_HERSHEY_COMPLEX)      |
| cv.FONT_HERSHEY_COMPLEX_SMALL  | smaller version of FONT_HERSHEY_COMPLEX                              |
| cv.FONT_HERSHEY_SCRIPT_SIMPLEX | hand-writing style font                                              |
| cv.FONT_HERSHEY_SCRIPT_COMPLEX | more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX                  |
| cv.FONT_ITALIC                 | flag for italic font                                                 |

To use the Qt library source, you need to link between Opencv and Qt.

And then use the function addText together with the fontQt.

cv.addText(img, text, org, nameFont[, pointSize[, color[, weight[, style[, spacing]]]]])

Alternative

PIL

Or this reply from Soen provides an alternative with the PIL library.

import numpy as np
from PIL import ImageFont, ImageDraw, Image
import cv2
import time

## Make canvas and set the color
img = np.zeros((200,400,3),np.uint8)
b,g,r,a = 0,255,0,0

## Use cv2.FONT_HERSHEY_XXX to write English.
text = time.strftime("%Y/%m/%d %H:%M:%S %Z", time.localtime()) 
cv2.putText(img,  text, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (b,g,r), 1, cv2.LINE_AA)

## Use simsum.ttc to write Chinese.
fontpath = "./simsun.ttc"     
font = ImageFont.truetype(fontpath, 32)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
draw.text((50, 100),  "国庆节/中秋节 快乐!", font = font, fill = (b, g, r, a))
img = np.array(img_pil)

## Display 
cv2.imshow("res", img);cv2.waitKey();cv2.destroyAllWindows()
cv2.imwrite("res.png", img)

Resultado

Free Type of opencv_contrib

Or may also be used the Opencv contrib Freetype Module, in which loadFontData is used to load a source file and then can be used with the function putText freetype.

  • Thank you so much for the help! It worked here, I used the cv2.addText function()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.