一、线段的绘制
绘制线段并拼成一个“王”字
import numpy as np
import cv2
canvas = np.zeros((300,300,3),np.uint8)
canvas = cv2.line(canvas,(50,50),(250,50),(255,0,0),5)
canvas = cv2.line(canvas,(50,150),(250,150),(0,255,0),10)
canvas = cv2.line(canvas,(50,250),(250,250),(0,0,255),15)
canvas = cv2.line(canvas,(150,50),(150,250),(0,255,255),20)
cv2.imshow("Lines",canvas)
cv2.w
二、矩形的绘制
绘制一个矩形边框
import numpy as np
import cv2
canvas = np.ones((300,300,3),np.uint8)
canvas = cv2.rectangle(canvas,(50,50),(200,150),(255,255,0),20)
cv2.imshow("Rectangle",canvas)
cv2.waitKey()
绘制一个实心矩形
import numpy as np
import cv2
canvas = np.ones((300, 300, 3), np.uint8)
canvas = cv2.rectangle(canvas, (50, 50), (200, 150), (255, 255, 0), -1)
cv2.imshow("Rectangle", canvas)
cv2.waitKey()
绘制正方形
import numpy as np
import cv2
canvas = np.zeros((300,300,3),np.uint8)
canvas = cv2.rectangle(canvas,(50,50),(250,250),(0,0,255),40)
canvas = cv2.rectangle(canvas,(90,90),(210,210),(0,255,255),30)
canvas = cv2.rectangle(canvas,(120,120),(180,180),(0,0,255),20)
canvas = cv2.rectangle(canvas,(140,140),(160,160),(0,0,255),-1)
cv2.imshow("Square",canvas)
cv2.waitKey()
三、圆形的绘制
绘制“交通灯”
import numpy as np
import cv2
canvas = np.ones((100,300,3),np.uint8)
canvas = cv2.circle(canvas,(50,50),40,(0,0,255),-1)
canvas = cv2.circle(canvas,(150,50),40,(0,255,255),-1)
canvas = cv2.circle(canvas,(250,50),40,(0,255,0),-1)
cv2.imshow("Circle",canvas)
cv2.waitKey()
绘制同心圆
import numpy as np
import cv2
canvas = np.zeros((300, 300, 3), np.uint8)
center_X = int(canvas.shape[1] / 2)
center_Y = int(canvas.shape[0] / 2)
for r in range(0, 150, 30):
cv2.circle(canvas, (center_X, center_Y), r, (0, 255, 0), 6)
cv2.imshow("Circle", canvas)
cv2.waitKey()
绘制33个随机实心圆
import numpy as np
import numpy as pnp
import cv2
canvas = np.zeros((300,300,3),np.uint8)
for i in range(0,32):
center_X = np.random.randint(0,high = 300)
center_Y = np.random.randint(0,high = 300)
radius = np.random.randint(11,high = 71)
color = np.random.randint(0,high = 256,size = (3,)).tolist()
cv2.circle(canvas,(center_X,center_Y),radius,color,-1)
cv2.imshow("Circle",canvas)
cv2.waitKey()
因为OpenCV中的颜色值是一个列表,所以 color = np.random.randint(0,high = 256,size = (3,)).tolist() 中的tolist()不能省略,否则运行程序会发生错误。
四、多边形的绘制
import numpy as np
import cv2
canvas = np.zeros((300,300,3),np.uint8)
pts = np.array([[100,50],[200,50],[250,250],[50,250]],np.int32)
canvas = cv2.polylines(canvas,[pts],True,(0,255,0),4)
cv2.imshow("pts",canvas)
cv2.waitKey()
五、文字的绘制
import numpy as np
import cv2
canvas = np.ones((100,500,3),np.uint8)*255
cv2.putText(canvas,"SmartBoy",(20,70),cv2.FONT_HERSHEY_TRIPLEX,2,(255,0,0),5)
cv2.imshow("SmartBoy",canvas)
cv2.waitKey()
语法格式:
img = cv2.putText(img,text,org,fontFace,fontScale,color,lintType,bottomLeftOrigin)
字体样式
字体样式 | 含义 |
---|
FONT_HERSHEY_SIMPLEX | 正常大小的sans-serif字体 | FONT_HERSHEY_PLAIN | 小号的sans-serif字体 | FONT_HERSHEY_DUPLEX | 正常大小的sans-serif字体(比SIMPLE字体样式复杂) | FONT_HERSHEY_COMPLEX | 正常大小的serif字体 | FONT_HERSHEY_TRIPLEX | 正常大小的serif字体 | FONT_HERSHEY_COMPLEX_SMALL | 简化版 | FONT_HERSHEY_SCRIPT_SIMPLEX | 手写风格 | FONT_HERSHEY_SCRIPT_COMPLEX | 进阶版 | FONT_ITALIC | 斜体 |
在图像上绘制文字
import cv2
img = cv2.imread("logo.jpg")
cv2.putText(img,"SmartBoy",(30,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2)
cv2.imshow("logo",img)
cv2.waitKey()
动态绘制图像
import cv2
import time
import numpy as np
width, height = 200, 200
r = 20
x = r + 20
y = r + 100
x_offer = y_offer = 4
while cv2.waitKey(1) == -1:
if x > width - r or x < r:
x_offer *= -1
if y > height - r or y < r:
y_offer *= -1
x += x_offer
y += y_offer
img = np.ones((width, height, 3), np.uint8) * 150
cv2.circle(img, (x, y), r, (255, 0, 0), -1)
cv2.imshow("img", img)
time.sleep(1 / 60)
cv2.destoryAllWindows()
|