我使用python cv2(window10,python2.7)在图像中写入文本,当文本是英文时它可以工作,但是当我使用中文文本时它会在图像中写出乱码.
以下是我的代码:
# coding=utf-8
import cv2
import numpy as np
text = "Hello world" # just work
# text = "??????" # messy text in the image
cv2.putText(img, text,
cord,
font,
fontScale,
fontColor,
lineType)
# Display the image
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
当text = "Hello world" # just work下面是输出图像时:
当text = "??????" # Chinese text, draw messy text in the image下面是输出图像时:
怎么了?opencv putText不支持其他语言文本吗?
有没有人可以替代OpenCVputText(支持 UTF-8 字符)?如前所述,putText仅适用于 ASCII 字符,但不适用于UTF-8诸如šŠ??žŽ?
我正在使用cv2.putText()将文本放在视频上。
这按预期工作,但我正在尝试使用不同的字体(在 OpenCV 中不可用)。
我知道 OpenCV 仅限于cv2.FONT_HERSHEY字体,所以我使用 PIL 和 OpenCV 来实现这一点。
我将这种方法用于图像,并且该实验成功了。但是当我在视频上尝试类似的东西时我失败了。
import cv2
from PIL import ImageFont, ImageDraw, Image
camera = cv2.VideoCapture('some_video.wmv')
while cv2.waitKey(30) < 0:
rv, frame = camera.read()
if rv:
font = ImageFont.truetype("calibrii.ttf", 80)
cv2.putText(frame, 'Hello World!', (600, 600), font, 2.8, 255)
cv2.imshow('Video', frame)
Run Code Online (Sandbox Code Playgroud)
我在同一目录中有“calibrii.ttf”,正如我所提到的,这种方法适用于图像。
这是错误:
cv2.putText(frame, 'Hello World!', (600, 600), font, 2.8, 255)
TypeError: an integer is required (got type FreeTypeFont)
Run Code Online (Sandbox Code Playgroud) 我正在像这样在 OpenCV 中添加文本...
import numpy as np
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
# Create a black image
img = np.zeros((512,512,3), np.uint8)
cv2.putText(img,'Hack Projects',(10,500), font, 1,(255,255,255),2)
#Display the image
cv2.imshow("img",img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
这有效,但文本质量不是很好。有人知道我做错了什么吗?