opencv 系列 入门之 鼠标操作

setMouse linetype = 16 毛刺就会变小

import cv2.cv2 as cv2
import numpy as np

# 画图
def paint(even, x, y, flags, parse):
    global start, polygon
    if even == 1:
        start = (x,y,)
    elif even == 4:
        if polygon == 'rectangle':
            print(start, x, y)
            # cv2.rectangle(img,start,(x,y),(255,255,255),2)
            cv2.rectangle(img, start, (x, y), (255, 255, 255), 2)
        elif polygon == 'circle':
            r = int((abs(x - start[0]) ** 2 + abs(y - start[1]) ** 2) ** 0.5)
            cv2.circle(img, start, r, (0, 255, 0), 2,lineType=16)
        elif polygon == 'line':
            cv2.line(img,start,(x,y),(0,0,255),2,lineType=16)
        else:
            print('please set polygon')
    # print(even,x,y,flags,parse)


cv2.namedWindow('camera', cv2.WINDOW_NORMAL)
cv2.resizeWindow('camera', 640, 480)

# draw black picture
img = np.zeros([480, 640, 3], np.uint8)

# set
cv2.setMouseCallback('camera', paint, 'this progame is run...')
# test

# start x,y
start, polygon = (0, 0), ''
# show
while True:
    cv2.imshow('camera', img)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    elif key == ord('r'):
        print('switch to rectangle')
        polygon = 'rectangle'
    elif key == ord('c'):
        print('switch to circle')
        polygon = 'circle'
    elif key == ord('l'):
        print('switch to paint line')
        polygon = 'line'

cv2.destroyAllWindows()

 

posted @ 2022-05-24 21:16  inks  阅读(50)  评论(0)    收藏  举报