gpt4 book ai didi

python - 在Mouse中将 'elif'解释为画笔代码

转载 作者:行者123 更新时间:2023-12-02 17:35:53 25 4
gpt4 key购买 nike

OpenCV的Python 3.4.1
IDE-PyCharm

我的代码:

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()

我试图了解使用Mouse作为画笔的OpenCV-Python代码。

链接: https://docs.opencv.org/3.4.1/db/d5b/tutorial_py_mouse_handling.html

现在,我有以下问题:
  • 函数elif中需要elif event == cv.EVENT_MOUSEMOVE吗?
    我的意思是为什么我们不能使用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,因为它们是互斥的。这是该事件还是该事件,但是elif可能用于避免每次都检查if条件。在C++中,您可以使用switch,但据我所知python中没有switch。
  • 模式似乎从您选择的矩形形状变为圆形。您可以使用m从一种模式更改为另一种模式。
  • 关于python - 在Mouse中将 'elif'解释为画笔代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020105/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com