zhiqingstudy

Be a young person with knowledge and content

# 可以通过鼠标进行基本的图形绘制
# 1.可以画线:当用户按下l键,即选择画线,滑动鼠标即可画线
# 2.可以画矩形:当用户按下r键,即选择画矩形,滑动鼠标即可画矩形
# 3.可以画圆:当用户按下c键,即选择画圆,滑动鼠标即可画圆

import cv2
import numpy as np

# 显示背景
img=np.zeros((360,640,3),np.uint8)


curshape=0
startpos=(0,0)

# 鼠标回调函数
def mouse_callback(event,x,y,flags,userdata):
    global curshape
    global startpos
    # 获取鼠标左键按下的坐标点
    if event & cv2.EVENT_LBUTTONDOWN==cv2.EVENT_LBUTTONDOWN:
        startpos=(x,y)
    # 获取鼠标左键弹起的坐标点,并画图形
    elif event & cv2.EVENT_LBUTTONUP==cv2.EVENT_LBUTTONUP:
        if curshape==0:
            cv2.line(img,startpos,(x,y),(0,0,255))
        elif curshape==1:
            cv2.rectangle(img,startpos,(x,y),(0,0,255))
        elif curshape==2:
            a=x-startpos[0]
            b=y-startpos[1]
            r=int((a**2+b**2)**0.5)
            cv2.circle(img,startpos,r,(0,0,255))
        else:
            print('error:no shape')


# 创建窗口
cv2.namedWindow('drawshape',cv2.WINDOW_NORMAL)
cv2.resizeWindow('drawshape',640,360)

# 设置鼠标回调
cv2.setMouseCallback('drawshape',mouse_callback,'123')


while 1:
    cv2.imshow('drawshape',img)
    key=cv2.waitKey(1)
    if key==ord('q'):
        break
    elif key==ord('l'):
        curshape=0
    elif key==ord('r'):
        curshape=1
    elif key==ord('c'):
        curshape=2

cv2.destroyAllWindows()

comment
head sculpture
Code:
Related

Why you shouldn't stay at a job for more than 2 years?

3 harsh facts long-distance relationships

how to keep your girlfriend interested in a long-distance relationship




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号