Python OpenCV 繪製文字 putText

本篇將介紹如何使用 OpenCV 與 Python 來繪製文字 putText,在寫 Python 影像處理程式時常會用到 OpenCV cv2.putText() 繪製文字的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來進行繪製文字 putText。

使用範例

以下示範 cv2.putText() 的用法,一開始建立 640*480 大小的影像,並填充128灰階值的背景顏色
接著使用 cv2.putText() 將 Hello, World! 這個字串繪製在 (100, 50) 的影像座標上,
並指定顏色為黃色 (0, 255, 255),最後 imshow 顯示出來就是結果啦!
詳細程式碼如下:

opencv-putText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# Create a blank 640x480 black image
image = np.zeros((480, 640, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (128, 128, 128)

text = 'Hello, World!'
cv2.putText(image, text, (100, 50), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 255, 255), 1, cv2.LINE_AA)

cv2.imshow('Result', image)
cv2.waitKey(0)

結果如下圖所示:

cv2.putText 參數為

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

圖片影像/繪製的文字/左上角坐標/字體/字體大小/顏色/字體粗細/字體線條種類
img – 要繪製文字的影像
text – 要繪製的文字
org – 文字左下角在影像中的座標位置
fontFace – 文字字體, 選項有 FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX
fontScale – 文字縮放比例
color – 文字顏色
thickness – 文字線條粗細度
lineType – 文字線條樣式
bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

cv2.putText() 參數最少填六個,第六個為顏色,
例如這樣,

1
cv2.putText(img, text, org, fontFace, fontScale, color)

有近一步需求再帶入其它參數,
cv2.putText 參數的詳細細節請參考這裡

範例. 用 putText 繪製顯示系統當前時間

這範例要將系統時間繪製顯示在影像上,例如:10:30:59 PM
之前有介紹詳細過Python 取得系統當前時間的使用方法可以回去看看
這邊我們用 putText 將 系統時間繪製在影像上。

opencv-putText2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time

# Create a blank 640x480 black image
image = np.zeros((480, 640, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (128, 128, 128)

localtime = time.localtime()
text = time.strftime("%Y-%m-%d %I:%M:%S %p", localtime)
cv2.putText(image, text, (100, 50), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 255, 255), 1, cv2.LINE_AA)

cv2.imshow('Result', image)
cv2.waitKey(0)

結果如下圖所示:

範例. 用 putText 繪製數字另存圖片檔

這邊示範用 putText 繪製數字 7 並另存圖片檔,

opencv-putText3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# Create a blank 128x128 black image
image = np.zeros((128, 128, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (30, 30, 30)

fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 3
thickness = 2
text = '7'
testSize = cv2.getTextSize(text, fontFace, fontScale, thickness)
#print(testSize)
bottomLeftX = 64-int(testSize[0][0]/2)
bottomLeftY = 64+int(testSize[0][1]/2)
cv2.putText(image, text, (bottomLeftX, bottomLeftY), fontFace,
fontScale, (0, 255, 255), thickness, cv2.LINE_AA)

cv2.imshow('Result', image)
cv2.imwrite('7.jpg', image)
cv2.waitKey(0)

結果如下圖所示:

範例. 用 putText 繪製物件偵測的標籤

用 OpenCV 作物件偵測時,需要將物件標示出來,並且顯示該物件的標籤名稱,這在物件偵測中還蠻實用且常用到的功能,

opencv-drawBoundingBox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

def drawBoundingBox(img, bboxs):
for box in bboxs:
x1,y1,x2,y2 = (box['x1'], box['y1'], box['x2'], box['y2'])
label = box['label']
cv2.rectangle(img, (x1,y1), (x2,y2), (0,255,0), 6)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 0.5
thickness = 1
labelSize = cv2.getTextSize(label, fontFace, fontScale, thickness)
_x1 = x1 # bottomleft x of text
_y1 = y1 # bottomleft y of text
_x2 = x1+labelSize[0][0] # topright x of text
_y2 = y1-labelSize[0][1] # topright y of text
cv2.rectangle(img, (_x1,_y1), (_x2,_y2), (0,255,0), cv2.FILLED) # text background
cv2.putText(img, label, (x1,y1), fontFace, fontScale, (0,0,0), thickness)
return img

# Create a blank 640x640 black image
image = np.zeros((480, 640, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (128, 128, 128)

bboxs = []
box = {}
box['label'] = 'object 1'
box['x1'] = 40
box['y1'] = 40
box['x2'] = 180
box['y2'] = 180
bboxs.append(box)
box2 = {'label': 'object 2', 'x1': 300, 'y1': 200, 'x2': 600, 'y2': 440}
bboxs.append(box2)
drawBoundingBox(image, bboxs)

cv2.imshow('Result', image)
cv2.waitKey(0)

結果如下圖所示:

其它相關文章推薦
Python OpenCV 畫矩形 rectangle
Python OpenCV 畫多邊形 polylines
Python OpenCV 彩色轉灰階(RGB/BGR to GRAY)
Python OpenCV 彩色轉YCbCr(RGB/BGR to YCbCr)
Python OpenCV 灰階轉彩色(Gray to RGB/BGR)
Python OpenCV 影像二值化 Image Thresholding
Python OpenCV 影像平滑模糊化 blur
Python OpenCV 影像邊緣偵測 Canny Edge Detection
Python OpenCV resize 圖片縮放