画字,图形,文本,图片上添加文本,绘制动态图形

import cv2
import numpy as np
#画一个王字,参数表示含义依次为起点坐标,终点坐标,线条颜色,线条宽度。坐标是【x,y]
canvas = np.zeros((300,300,3),np.uint8) #画布为黑色
#canvas = np.ones((300,300,3),np.uint8)*255 白色画布
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),(200,250),(0,255,255),20)
cv2.imshow("Lines",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#画一个矩形
canvas = np.zeros((300,300,3),np.uint8)
#绘制空心矩形参数依次是左上角坐标,右下角坐标,青色的,线条宽度
canvas = cv2.rectangle(canvas,(50,50),(200,150),(255,255,0),20)
#绘制实心矩形
#canvas = cv2.rectangle(canvas,(50,50),(200,150),(255,255,0),-1)
cv2.imshow("rectangle",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#显示正方形通过起始结束坐标实现,两点的横轴,竖轴相交点确定另外两点
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,0),30)
canvas = cv2.rectangle(canvas,(120,120),(180,180),(255,0,0),20)
canvas = cv2.rectangle(canvas,(140,140),(160,160),(0,255,255),-1)
cv2.imshow("square",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#画圆,信号灯
canvas =np.zeros((100,300,3),np.uint8)
#参数依次为:画布,圆心,半径,颜色,线宽(-1则为实心)。
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("TrafficLights",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#显示同心圆
canvas =np.zeros((300,300,3),np.uint8)
#shape[0]表示画布的高度,shape[1]表示画布的宽度
center_x = int(canvas.shape[1]/2)
center_y = int(canvas.shape[0]/2)
for r in range(0,150,30):#每次间隔30
    cv2.circle(canvas,(center_x,center_y),r,(0,255,0),5)
cv2.imshow("Circle",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#绘制27个随机同心圆
canvas =np.zeros((300,300,3),np.uint8)
for number in range(0,28):
    # 在【0,299】随机取一个值
    center_x = np.random.randint(0,high=300)
    center_y = np.random.randint(0,high=300)
    # 在【11,70】随机取一个值
    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("Circles",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#画一个等腰梯形
canvas =np.zeros((300,300,3),np.uint8)
#要注意pts中顶点顺序,polulines方法是依次连接各个顶点
pts = np.array([[100,50],[200,50],[250,250],[20,250]],np.int32)
#依次连接各个顶点,要注意pts中顶点顺序
canvas = cv2.polylines(canvas,[pts],True,(0,0,255),5)
#True改为False绘制一个不闭合的等腰梯形
canvas = cv2.polylines(canvas,[pts],True,(0,0,255),5)
cv2.imshow("Polylines",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#显示文本
canvas =np.zeros((100,300,3),np.uint8)
#参数依次为:画布,文字内容,左下角下标,字体样式,字体大小,线条颜色,线条宽度,线性(线的产生算法有4,8两个值默认为8),绘制文字时的方向默认false正写,True为镜像写
#如:cv2.putText(canvas,"mrsoft",(20,100),fontStyle,2,(0,255,0),5,8,True)
cv2.putText(canvas,"mrsoft",(20,70),cv2.FONT_HERSHEY_TRIPLEX,2,(0,255,0),5)
cv2.imshow("Text",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#有文字样式且附带斜体效果
canvas =np.zeros((100,300,3),np.uint8)
#FONT_ITALIC为斜体,与其他字体连用,字体呈现指定样式且斜体
fontStyle = cv2.FONT_HERSHEY_TRIPLEX+cv2.FONT_ITALIC
cv2.putText(canvas,"mrsoft",(20,70),fontStyle,2,(0,255,0),5)
cv2.imshow("Text",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#文字的垂直镜像效果
canvas =np.zeros((200,300,3),np.uint8)
#FONT_ITALIC为斜体,与其他字体连用,字体呈现指定样式且斜体
fontStyle = cv2.FONT_HERSHEY_TRIPLEX
cv2.putText(canvas,"mrsoft",(20,70),fontStyle,2,(0,255,0),5)
#       ****
cv2.putText(canvas,"mrsoft",(20,100),fontStyle,2,(0,255,0),5,8,True)
cv2.imshow("Text",canvas)
cv2.waitKey()
cv2.destroyAllWindows()

#在图像上绘制文字
image= cv2.imread("3.1.jpeg")
fontStyle = cv2.FONT_HERSHEY_TRIPLEX
#参数依次为:图像,文字,文字左下角坐标,字体样式,大小,颜色
cv2.putText(image,"Flower",(20,90),fontStyle,1,(0,0,255))
cv2.imshow("Text",image)
cv2.waitKey()
cv2.destroyAllWindows()

#绘制动态图形(动态正方形,动态圆),主要:图片逐渐移动,每秒显示60帧图片实现动态
import time
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 < 0:
        x_offer *= -1
    if y>height-r or y < 0:
        y_offer *= -1
    x+=x_offer
    y+=y_offer
    img = np.ones((width,height,3),np.uint8)*255
    #显示正方形
    cv2.rectangle(img, (x, y), (x+r, y+r), (0, 0, 255), -1)
    cv2.imshow("img",img)
    #每秒60帧
    time.sleep(1/60)
cv2.destroyAllWindows()

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)*255
    #显示圆形
    
    cv2.circle(img,(x,y),r,(255,0,0),-1)
    cv2.imshow("img",img)
    #每秒60帧
    time.sleep(1/60)
cv2.destroyAllWindows()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值