opencv 鼠标画直线圆矩形

import cv2
import numpy as np

cv2.namedWindow('bgr', cv2.WINDOW_NORMAL)
img = np.zeros((800,800,3),np.uint8)

text1='line:l'
text2='rectangle:r'
text3='circle:c'
text4='out:q'       
cv2.putText(img,text1,(10,20),cv2.FONT_HERSHEY_COMPLEX,0.5,[200,200,200])
cv2.putText(img,text2,(10,40),cv2.FONT_HERSHEY_COMPLEX,0.5,[200,200,200])
cv2.putText(img,text3,(10,60),cv2.FONT_HERSHEY_COMPLEX,0.5,[200,200,200])
cv2.putText(img,text4,(10,80),cv2.FONT_HERSHEY_COMPLEX,0.5,[200,200,200])
cv2.imshow('bgr',img)

def line(event, x, y, flags, userdata):
    if event == cv2.EVENT_LBUTTONDOWN:
        dot.append((x,y))
    if event == cv2.EVENT_LBUTTONUP:
        dot.append((x,y))
        cv2.line(img,(dot[0]),(dot[1]),(220,200,255),1,64)
        cv2.imshow('bgr',img)
def rec(event, x, y, flags, userdata):
    if event == cv2.EVENT_LBUTTONDOWN:
        dot.append((x,y))
    if event == cv2.EVENT_LBUTTONUP:
        dot.append((x,y))
        cv2.rectangle(img,(dot[0]),(dot[1]),(220,200,255),1,64)
        cv2.imshow('bgr',img)
def cir(event, x, y, flags, userdata):
    if event == cv2.EVENT_LBUTTONDOWN:
        dot.append((x,y))
    if event == cv2.EVENT_LBUTTONUP:
        dot.append((x,y))
        a=dot[1][1]-dot[0][1]
        b=dot[1][0]-dot[0][0]
        r = int((a**2+b**2)**0.5)
        cv2.circle(img,(dot[0]),r,(220,200,255),1,64)
        cv2.imshow('bgr',img)
        
        
while True:
    dot = []
    key = cv2.waitKey(0)
    if  key == ord('l'):
        cv2.setMouseCallback('bgr', line, 'line')
    elif  key == ord('r'):
        cv2.setMouseCallback('bgr', rec, 'rec')
    elif  key == ord('c'):
        cv2.setMouseCallback('bgr', cir, 'cir')
    elif  key == ord('q'):
        cv2.destroyAllWindows()
        break
    else:
        continue

opencv 鼠标画直线圆矩形

你可能感兴趣的:(opencv,python,计算机视觉)