OpenCV2.x的putText是无法处理中文的(OpenCV3.x中集成了freetype实现中文输出),同样,Python2.x对中文的支持也很差(同样这一情况在Python3.x中得到了改善)。
所以最方便的方法是,两个都换成3.x版本的。。。[捂脸]
计算机上显示文本的过程大体上是,先将文本转换成一个一个的bitmap,然后再用图形系统将这些bitmap显示出来。freetype是一个开源的字体引擎,它完成的工作即是将字符转换成bitmap。
参考这篇文章和Github项目介绍,首先需要编译安装freetype2
1)到这里下载最新的freetype2,解压缩后进入目录
../configure
make
sudo make install
2)到这里下载freetype-py源代码,解压缩后进入目录
sudo ./setup.py install
这样就算安装好了
推荐这篇文章
归纳起来就是:Python2.x里有两种字符串类型(str和unicode),可以类比c++里的char和wchar_t
两者可以通过 decode 和 encode 互转
此外需要注意的是,编码正常情况下 print 会打印正确的字符,但pdb观察该变量则是转义符组成的值。这是内部机制的问题,只要print正确就好了
建议使用 wqy-zenhei.ttc,测试了freetype自带的 Vera.ttf 和 windows下的 msyh.ttf,部分中文还是会乱码
算法核心参考这篇文章
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
##################################################
# tools #
#------------------------------------------------#
# draw chinese text using freetype on python2.x # #
# 2017.4.12 #
##################################################
'''
import numpy as np
import freetype
import copy
import pdb
class put_chinese_text(object):
def __init__(self, ttf):
self._face = freetype.Face(ttf)
def draw_text(self, image, pos, text, text_size, text_color):
'''
draw chinese(or not) text with ttf
:param image: image(numpy.ndarray) to draw text
:param pos: where to draw text
:param text: the context, for chinese should be unicode type
:param text_size: text size
:param text_color:text color
:return: image
'''
self._face.set_char_size(text_size * 64)
metrics = self._face.size
ascender = metrics.ascender/64.0
#descender = metrics.descender/64.0
#height = metrics.height/64.0
#linegap = height - ascender + descender
ypos = int(ascender)
if not isinstance(text, unicode):
text = text.decode('utf-8')
img = self.draw_string(image, pos[0], pos[1]+ypos, text, text_color)
return img
def draw_string(self, img, x_pos, y_pos, text, color):
'''
draw string
:param x_pos: text x-postion on img
:param y_pos: text y-postion on img
:param text: text (unicode)
:param color: text color
:return: image
'''
prev_char = 0
pen = freetype.Vector()
pen.x = x_pos << 6 # div 64
pen.y = y_pos << 6
hscale = 1.0
matrix = freetype.Matrix(int(hscale)*0x10000L, int(0.2*0x10000L),\
int(0.0*0x10000L), int(1.1*0x10000L))
cur_pen = freetype.Vector()
pen_translate = freetype.Vector()
image = copy.deepcopy(img)
for cur_char in text:
self._face.set_transform(matrix, pen_translate)
self._face.load_char(cur_char)
kerning = self._face.get_kerning(prev_char, cur_char)
pen.x += kerning.x
slot = self._face.glyph
bitmap = slot.bitmap
cur_pen.x = pen.x
cur_pen.y = pen.y - slot.bitmap_top * 64
self.draw_ft_bitmap(image, bitmap, cur_pen, color)
pen.x += slot.advance.x
prev_char = cur_char
return image
def draw_ft_bitmap(self, img, bitmap, pen, color):
'''
draw each char
:param bitmap: bitmap
:param pen: pen
:param color: pen color e.g.(0,0,255) - red
:return: image
'''
x_pos = pen.x >> 6
y_pos = pen.y >> 6
cols = bitmap.width
rows = bitmap.rows
glyph_pixels = bitmap.buffer
for row in range(rows):
for col in range(cols):
if glyph_pixels[row*cols + col] != 0:
img[y_pos + row][x_pos + col][0] = color[0]
img[y_pos + row][x_pos + col][1] = color[1]
img[y_pos + row][x_pos + col][2] = color[2]
if __name__ == '__main__':
# just for test
import cv2
file = open('class.txt','r')
line = file.readline()
img = np.zeros([300,300,3])
color_ = (0,255,0)
pos = (3, 3)
text_size = 24
ft = put_chinese_text('wqy-zenhei.ttc')
image = ft.draw_text(img, pos, line, text_size, color_)
cv2.imshow('ss', image)
cv2.waitKey(0)