[python][原创]opencv截取图像写字画矩形框

之前每次用都忘了,这次记录下来,备份一下免得自己到处找。

抠图:左上角坐标(xmin,ymin),右下角坐标(xmax,ymax)

     save_image = img[ymin:ymax, xmin:xmax]

#用 OpenCV 标注 bounding box主要用到下面两个工具——cv2.rectangle() 和 cv2.putText()。用法如下:
# cv2.rectangle()
# 输入参数分别为图像、左上角坐标、右下角坐标、颜色数组、粗细
cv2.rectangle(img, (x,y), (x+w,y+h), (B,G,R), Thickness)

# cv2.putText()
# 输入参数为图像、文本、位置、字体、大小、颜色数组、粗细
cv2.putText(img, text, (x,y), Font, Size, (B,G,R), Thickness)

#举例:

import cv2
 
imgName = '001.jpg'
img = cv2.imread(imgName)
# 画矩形框
cv2.rectangle(img, (10,20), (50,60), (0,255,0), 4)

# 标注文本
font = cv2.FONT_HERSHEY_SUPLEX
text = '001'
cv2.putText(img, text, (100, 100), font, 2, (0,0,255), 1)
cv2.imwrite('new.jpg', img)
其中字体可以选择:
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

字体大小:数值越大,字体越大
字体粗细:越大越粗,数值表示描绘的线条占有的直径像素个数

你可能感兴趣的:(Python)