OpenCV Python 3.4.1版 IDE-皮查姆
我的代码:
import numpy as np
import cv2 as cv
events = [i for i in dir(cv) if 'EVENT' in i]
print(events)
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv.EVENT_LBUTTONUP:
drawing = False
if mode == True: # this part start
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv.circle(img,(x,y),5,(0,0,255),-1) # this part end
img = np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
cv.destroyAllWindows()
我正在试图理解opencvpython代码中使用鼠标作为画笔。你知道吗
链接:https://docs.opencv.org/3.4.1/db/d5b/tutorial_py_mouse_handling.html
现在,我有以下问题:
elif event == cv.EVENT_MOUSEMOVE
中的elif
需要什么?
我是说为什么我们不能用if
而不是elif
?我不明白我们为什么用
elif
。你知道吗这有什么用
if mode == True:
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv.circle(img,(x,y),5,(0,0,255),-1)
我是说密码里的这部分?你知道吗
我试着去理解它,但是我不能。 请帮忙。你知道吗
是的,在这种情况下可以使用
if
而不是elif
,因为它们是互斥的。事件不是this就是that,但是elif
可能用于避免每次都检查两个if
条件。在C++中,你可以使用开关,但据我所知,Python中没有开关。在您的选择中,模式似乎从矩形变为圆形。您可以使用
m
从一种模式切换到另一种模式。相关问题 更多 >
编程相关推荐