來源:https://blog.csdn.net/wyx100/article/details/75579581
- opencv圖片寫入中文
- opencv+opencv_contrib 人臉識別和檢測
opencv圖片寫入中文(漢字)有兩方法:
方法一:
python+opencv+freetype(支持py2.py3)
https://blog.csdn.net/wyx100/article/details/75579581
python+freetype配置---http://blog.csdn.net/wyx100/article/details/73527117
#py2
pip install freetype-py
完整字體下載http://download.csdn.net/detail/o8xv0123/4589166
所有常用中英文ttf字體包,包含幾個手寫字體包括:times new roman,中山行書百年紀念版,calibri,Christopherhand,DejaVuSansMono,方正蘭亭黑,James Fajardo,Monaco,微軟雅黑,仿宋,黑體,楷體,宋體,yahei_mono,仿宋_GB2312,楷體_GB2312,迷你簡行楷碑。
#主文件 #-*- coding: utf-8 -*- import cv2 import ft2 img = cv2.imread('pic/lena.jpg') line = '你好,我是 lena' color = (0, 255, 0) # Green pos = (3, 3) text_size = 24 # ft = put_chinese_text('wqy-zenhei.ttc') ft = ft2.put_chinese_text('msyh.ttf') image = ft.draw_text(img, pos, line, text_size, color) name = u'圖片展示' cv2.imshow(name, image) cv2.waitKey(0)
###利用freetype包在圖片上寫入漢字 # -*- coding: utf-8 -*- # http://blog.csdn.net/zizi7/article/details/70145150 ''' ################################################## # 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 line = '你好' img = np.zeros([300,300,3]) color_ = (0,255,0) # Green pos = (3, 3) text_size = 24 #ft = put_chinese_text('wqy-zenhei.ttc') ft = put_chinese_text('msyh.ttf') image = ft.draw_text(img, pos, line, text_size, color_) cv2.imshow('ss', image) cv2.waitKey(0)
-
文字繪制---cv::putText IndexError: index 374 is out of bounds for axis 0 with size 341
-
解決方法:注意不同圖片的分辨率,根據不同圖片的分辨率設置text_size(分辨率高(1000+)的圖片字體設置為100左右,較低的字體大小設置為30左右)
方法二:
python+opencv+PIL(只支持python3)
https://blog.csdn.net/wyx100/article/details/80412101
#!/usr/bin/env python # -*- coding: utf-8 -*- from PIL import Image, ImageDraw, ImageFont import cv2 import numpy as np # cv2讀取圖片 img = cv2.imread('shishi.jpg') # 名稱不能有漢字 cv2img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2和PIL中顏色的hex碼的儲存順序不同 pilimg = Image.fromarray(cv2img) # PIL圖片上打印漢字 draw = ImageDraw.Draw(pilimg) # 圖片上打印 font = ImageFont.truetype("simhei.ttf", 20, encoding="utf-8") # 參數1:字體文件路徑,參數2:字體大小 draw.text((0, 0), "Hi,我是詩shi", (255, 0, 0), font=font) # 參數1:打印坐標,參數2:文本,參數3:字體顏色,參數4:字體 # PIL圖片轉cv2 圖片 cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR) # cv2.imshow("圖片", cv2charimg) # 漢字窗口標題顯示亂碼 cv2.imshow("photo", cv2charimg) cv2.waitKey (0) cv2.destroyAllWindows()
開發環境配置opencv+opencv_contrib 人臉識別和檢測
python開發環境快速搭建(30分鍾)圖文教程http://blog.csdn.net/wyx100/article/details/73008528
下載地址
報錯 AttributeError: module 'cv2.face' has no attribute 'createEigenFaceRecognizer'
原因:版本問題,未成功安裝opencv_contrib,所以model = cv2.face.createEigenFaceRecognizer() 行找不到face
解決:更換版本
詳細見 http://blog.csdn.net/wyx100/article/details/73008324
---------------------