OpenCV画图

OpenCV画图

使用Matplotlib

可以用来放大图像,保存图像等

# -*- coding: utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.png', 0)
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([]), plt.yticks([]) 
plt.show()

OpenCV 绘图函数

cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), cv2.putText()
参数:

  • img:想要绘制的图像
  • color:形状的颜色。以RGB为例,需要传入一个元组,例如:(255, 0,0)。
  • thickness:线条的粗细。如果给一个闭合图形设置-1,那么这个图形就会被填充。默认值是1.
  • linetype:线条的类型,8连接(默认),抗锯齿等。

1.画线:cv2.line(),需要起点和终点

import cv2
import numpy as np
# Create a black image
img = np.zeros((512, 512, 3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0, 0), (511, 511), (255, 0, 0), 5)
#cv2.polylines()可以用来画很多条线,将起始点存在列表里传给函数即可。

2.画矩形:cv2.rectangle(),需要左上顶点后右下顶点

cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)

3.画圆:cv2.circle(),需要圆心和半径

cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)

4.画椭圆:cv2.ellipse(),需要中心点,长轴,短轴,旋转角度

cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

5.画多边形:

pts = np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
pts = pts.reshape((-1, 1, 2))

6.在图片上添加文字
需要设置的参数:需要绘制的文字,位置,类型,大小,颜色等。

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500),font,4,(255,255,255),2)

7.展示

from matplotlib import pyplot as plt
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([]),plt.yticks([]) 
plt.show()

所有的绘图函数返回的都是None,所以不能用img = cv2.line()。

猜你喜欢

转载自blog.csdn.net/weixin_42902413/article/details/86648026