Reputation: 534
I want to apply some text to an image using python cv2 module.
I have found two cv2 functions that can do it cv2.putText
and cv2.addText
. I would like to know the pro & cons for each.
Thank you
Upvotes: 6
Views: 10032
Reputation: 1239
Adding for completeness and eventually gather more information:
Actually cv2.AddText seems to be used in Qt context, if you compile OpenCV with Qt support. See this page for more API and usage.
But if you open a console and insert below commands to know about Python OpenCV support:
import cv2 # version 4.5.0 complied with Qt support
res = [f for f in dir(cv2) if 'Text' in f]
print(res)
['addText', 'getTextSize', 'putText', 'text_TextDetector', 'text_TextDetectorCNN', 'ximgproc_segmentation_SelectiveSearchSegmentationStrategyTexture']
We can see both addText and putText.
However if we search for "fontQt" function as in documentation (this page):
res = [f for f in dir(cv2) if 'fontQt' in f] # no results for 'Qt' also
print(res)
[]
So it seems Qt font support should be available in OpenCV but there is no Python wrapper to it.
Upvotes: 3
Reputation: 445
Just like Yunus from the first comment I did not know the addText function yet. And had to do some research first.
In the c++ source code the two functions are implemented in different modules. The putText function in imgproc and the addText function in the highgui module. The latter is as far as I know intended for the easy creation of GUIs. I guess that the addText function should be used in this GUI context.
This is the only documentation I could find about addText in python: https://kite.com/python/docs/cv2.addText The call looks identical so far. Nevertheless I get the following error when executing the following program:
img = cv2.imread("img url")
t = time.time()
cv2.putText(img, "Hello", (20,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
print(str(time.time() - t))
t = time.time()
cv2.addText(img, "World", (50,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
print(str(time.time() - t))
cv2.imshow("test", img)
cv2.waitKey(0)
Error:
Traceback (most recent call last):
File ".../test.py", line 20, in <module>
main()
File ".../test.py", line 12, in main
cv2.addText(img, "World", (50,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
SystemError: <built-in function addText> returned NULL without setting an error
To put it in your pro, con list:
putText: + works + usualy used - nothing
addText: (+) might be usable in gui stuff - found no working documentation
Upvotes: 6