本文是 OpenCV图像视觉入门之路的第8篇文章,本文详细的在图像上面进行了绘制线条、绘制圆、空心圆、实心圆、画矩形、空心矩形、实心矩形、添加英文、添加汉字、绘制多边形等操作。
OpenCV 图像绘图、线条、文字、矩形目录
1 绘制线条
主要使用了line函数
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
image = cv2.imread('D:/Jupyter_Notebooks/0.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# (0, 0) XY点 (800, 512) 终点 (255, 0, 0)颜色 5宽度
cv2.line(image, (0, 0), (800, 512), (255, 0, 0), 5)
plt.imshow(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2 绘制圆
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
image = cv2.imread('D:/Jupyter_Notebooks/0.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# cv2.circle(图像, 圆点, 半径, 颜色, 宽度)
cv2.circle(image, (200, 200), 50, (255, 0, 0), 1)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.1 空心圆
# cv2.circle(图像, 圆点, 半径, 颜色, 宽度)
cv2.circle(image, (200, 200), 50, (255, 0, 0), 1)
2.2 实心圆
# cv2.circle(图像, 圆点, 半径, 颜色, 宽度)
cv2.circle(image, (200, 200), 50, (255, 0, 0), -1)
3 画矩形
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
image = cv2.imread('D:/Jupyter_Notebooks/0.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 画矩形 左上角坐标(20, 20),右下角坐标(80, 80),框颜色为绿色
image = cv2.rectangle(image, (20, 20), (80, 80), (0, 255, 0), 2)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
3.1 空心矩形
# 画矩形 左上角坐标(20, 20),右下角坐标(80, 80),框颜色为绿色
image = cv2.rectangle(image, (20, 20), (80, 80), (0, 255, 0), 2)
3.2 实心矩形
# 画矩形 左上角坐标(20, 20),右下角坐标(80, 80),框颜色为绿色
image = cv2.rectangle(image, (20, 20), (80, 80), (0, 255, 0), -2)
4 添加文字
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
image = cv2.imread('D:/Jupyter_Notebooks/0.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 添加文字,加载字体
font = cv2.FONT_HERSHEY_SIMPLEX
# 添加文字hello
cv2.putText(image, 'hello word', (10, 200), font,
4, (255, 255, 255), 2, lineType=cv2.LINE_AA)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
5 添加汉字
import cv2
from PIL import Image, ImageFont, ImageDraw
import matplotlib.pyplot as plt
import numpy as np
from numpy import unicode
def paint_chinese_opencv(cv2image):
# 将cv2格式转化为PIL格式
pilimg = Image.fromarray(cv2image)
# PIL图片上打印汉字
draw = ImageDraw.Draw(pilimg) # 图片上打印
# 参数1:字体文件路径,参数2:字体大小 ,参数3:编码格式
font = ImageFont.truetype("D:/SimHei.ttf", 60, encoding="utf-8")
# 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
draw.text((150, 250), "你好 世界", (255, 0, 0), font=font)
# PIL图片转cv2 图片
# cv2.cvtColor颜色空间转换函数,第一个参数是需要转换的图片,第二个参数是转换成何种格式,这里是将RGB换成BGR
cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
return cv2charimg
if __name__ == '__main__':
image = cv2.imread('D:/Jupyter_Notebooks/0.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = paint_chinese_opencv(image)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
6 绘制多边形
import cv2
from PIL import Image, ImageFont, ImageDraw
import matplotlib.pyplot as plt
import numpy as np
from numpy import unicode
if __name__ == '__main__':
image = np.zeros((500, 500, 3), dtype=np.uint8)
data = np.array([[172.0, 82], [194.0, 86], [182, 151.0], [194, 86]]) # [N,2]
roi_as = [data.astype(np.int)]
cv2.polylines(image, roi_as, True, (255, 0, 255)) # 画任意多边形
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
img:要在上面画多边形的图像
pts:包含多边形上点的数组
isClosed:标志,决定所绘制的多边形是否闭合。若为 True ,则画若干个闭合多边形;若为 False ,则画一条连接所有点的折线
color:多边形颜色
thickness:多边形线的粗细
lineType:多边形线的类型
shift:坐标精确到小数点后第几位