1、学习目标
(1)在python中使用opencv将文本放在视频上
(2)使用OpenCV Python在视频上显示日期和时间
2、使用函数
cv2.putText()
3、程序
import cv2
import datetime
cap = cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#cap.set(3, 3000)
#cap.set(4, 3000)
#print(cap.get(3))
#print(cap.get(4))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'Width: '+ str(cap.get(3)) + ' Height:' + str(cap.get(4))
datet = str(datetime.datetime.now())
frame = cv2.putText(frame, text, (10, 50), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
frame = cv2.putText(frame, datet, (10, 100), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
cv2.putText(图像,添加的文字,左上角坐标,字体,字体大小,
颜色,字体粗细)
cv.putText(img, text, org, fontFace,fontScale,color[, thickness[, lineType[, bottomLeftOrigin]]])
程序中的例子:
cv2.putText(frame, datet, (10, 100), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
程序中字体选择:
font = cv2.FONT_HERSHEY_SIMPLEX
font 字体选择 :
FONT_HERSHEY_SIMPLEX
Python: cv.FONT_HERSHEY_SIMPLEX
normal size sans-serif font
FONT_HERSHEY_PLAIN
Python: cv.FONT_HERSHEY_PLAIN
small size sans-serif font
FONT_HERSHEY_DUPLEX
Python: cv.FONT_HERSHEY_DUPLEX
normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
FONT_HERSHEY_COMPLEX
Python: cv.FONT_HERSHEY_COMPLEX
normal size serif font
FONT_HERSHEY_TRIPLEX
Python: cv.FONT_HERSHEY_TRIPLEX
normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
FONT_HERSHEY_COMPLEX_SMALL
Python: cv.FONT_HERSHEY_COMPLEX_SMALL
smaller version of FONT_HERSHEY_COMPLEX
FONT_HERSHEY_SCRIPT_SIMPLEX
Python: cv.FONT_HERSHEY_SCRIPT_SIMPLEX
hand-writing style font
FONT_HERSHEY_SCRIPT_COMPLEX
Python: cv.FONT_HERSHEY_SCRIPT_COMPLEX
more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
FONT_ITALIC
Python: cv.FONT_ITALIC
flag for italic font
字体大小,数值越大,字体越大
字体粗细,越大越粗,数值表示线占有直径像素个数