Python 如何使用0xFF识别左键和右键单击

Python 如何使用0xFF识别左键和右键单击,python,opencv,Python,Opencv,我有一个图像捕获程序,当按下键盘上的某个键时,它可以从网络摄像头中捕获任何帧。目前,我想创建一个替代品,除了使用键盘,即使用鼠标点击,但我需要两者都工作。代码如下: while True: # Capture frame-by-frame ret, frame = video_capture.read() pressedKey = cv2.waitKey(1) & 0xFF #take picture

我有一个图像捕获程序,当按下键盘上的某个键时,它可以从网络摄像头中捕获任何帧。目前,我想创建一个替代品,除了使用键盘,即使用鼠标点击,但我需要两者都工作。代码如下:

    while True:
        # Capture frame-by-frame
        ret, frame = video_capture.read()

        pressedKey = cv2.waitKey(1) & 0xFF

        #take picture
        if pressedKey == ord('c'):
            cv2.imwrite('./results/'+str(subject)+'_'+str(position)+'_'+rgba[counter]+'.png',frame)
            counter= counter + 1
            details=str(subject)+','+str(position)+','+str(counter)+'\n'
            with open('./details.txt', 'a') as f:
                f.write(details)
            print("Image taken succesfully")
        #quit
        elif pressedKey== ord('q'):
            print("Closing application")
            break
        cv2.imshow('Video', frame)

我想通过按“c”或使用鼠标单击来拍照,然后通过按“q”或使用鼠标单击退出。

我对您的代码进行了一些修改,因为我没有关于主题、位置等的信息

不管怎么说,这是您想要的最终版本:

import cv2
video_capture = cv2.VideoCapture(0)

status = 1
# mouse callback function
def mouse_func(event,x,y,flags,param):
    global status
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.imwrite('save.png',frame)
        details='details'
        with open('./details.txt', 'a') as f:
            f.write(details)
        print("Image taken succesfully")

    elif event == cv2.EVENT_RBUTTONDOWN:
        status = 0
        print("Closing application")

cv2.namedWindow('Video')
cv2.setMouseCallback('Video',mouse_func)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    pressedKey = cv2.waitKey(1) & 0xFF

    #take picture
    if pressedKey == ord('c'):
        cv2.imwrite('save.png',frame)
        details='details'
        with open('./details.txt', 'a') as f:
            f.write(details)
        print("Image taken succesfully")
    #quit
    elif pressedKey== ord('q') or status == 0:
        print("Closing application")
        break
    cv2.imshow('Video', frame)
print("Application Closed")
cv2.destroyAllWindows()
您应该创建一个回调函数(
mouse\u func
),然后使用
EVENT\u LBUTTONDOWN
EVENT\u RBUTTONDOWN
检查右键和左键单击


请注意,如果右键单击,则存在一个全局变量
status
,该变量设置为
0
。然后,在主循环中,我们检查按下的键是
q
还是
status
设置为
0

我对您的代码做了一些修改,因为我没有关于主题、位置等的信息

不管怎么说,这是您想要的最终版本:

import cv2
video_capture = cv2.VideoCapture(0)

status = 1
# mouse callback function
def mouse_func(event,x,y,flags,param):
    global status
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.imwrite('save.png',frame)
        details='details'
        with open('./details.txt', 'a') as f:
            f.write(details)
        print("Image taken succesfully")

    elif event == cv2.EVENT_RBUTTONDOWN:
        status = 0
        print("Closing application")

cv2.namedWindow('Video')
cv2.setMouseCallback('Video',mouse_func)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    pressedKey = cv2.waitKey(1) & 0xFF

    #take picture
    if pressedKey == ord('c'):
        cv2.imwrite('save.png',frame)
        details='details'
        with open('./details.txt', 'a') as f:
            f.write(details)
        print("Image taken succesfully")
    #quit
    elif pressedKey== ord('q') or status == 0:
        print("Closing application")
        break
    cv2.imshow('Video', frame)
print("Application Closed")
cv2.destroyAllWindows()
您应该创建一个回调函数(
mouse\u func
),然后使用
EVENT\u LBUTTONDOWN
EVENT\u RBUTTONDOWN
检查右键和左键单击


请注意,如果右键单击,则存在一个全局变量
status
,该变量设置为
0
。然后,在主循环中,我们检查按下的键是
q
还是
status
设置为
0

看看,看看,谢谢你的回答,这正是我想要的。主题、位置等并不重要,因为它只是计数器,所以我没有将其放入代码中。谢谢你的回答,这正是我想要的。主题、位置等并不重要,因为它只是计数器,所以我没有把代码放进去