从vid第一帧的鼠标事件记录两组点位置

2024-04-16 20:59:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个奶牛场的录像带。我的目标是:

(a)获取牛棚(牛舍)角落的位置

(b)取下食物容器的角落

以下是我正在考虑的方法-

(a)  - capture the frame and freeze on the 1st frame

     - user will manually put the mouse on the corners

     - the x,y location will be saved in a list

     - press "p" key to proceed to the next frame


(b)  - freeze the frame on the second frame

     - user will manually put the mouse on the corners

     - the x,y location will be saved in another list

     - press "c" key to proceed to next frames

我已经有了其他代码来执行其他操作。我尝试了以下代码从图像(而不是视频)中获取点。现在确定如何暂停视频并使用现有帧作为输入图像

import cv2, numpy as np

ix,iy = -1,-1
# the list of locations
mouse = []
def get_location(event,x,y,flags,param):
    global ix,iy
    if event == cv2.EVENT_LBUTTONDBLCLK:
        ix,iy = x,y
        mouse.append([x,y])

# take image and name it
img = cv2.imread("colo.png",0)
cv2.namedWindow('image')
cv2.setMouseCallback('image',get_location)


while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(20) & 0xFF
    if k == 27:
        break
    elif k == ord('a'):
        print (ix,iy)
print (mouse)
cv2.destroyAllWindows()

我要寻找的答案是:(a)如何在特定帧号上冻结帧,以及(b)cv2.setMouseCallback('image',get_location)将字符串作为第一个参数,如何在此处插入帧作为参数


Tags: andthetoimagegetonlocationcv2
1条回答
网友
1楼 · 发布于 2024-04-16 20:59:25

a)使用变量将waitKey设置为0。只有在按键后,才会显示下一帧。按下“c”后更改变量,使视频正常运行:

waitTime = 0

k = cv2.waitKey(waitTime)
if k == ord('c'):
    waitTime = 20

b)字符串参数是回调附加到的窗口的名称。要“插入框架”,只需在窗口上调用imshow。在这方面,您的代码似乎很好

相关问题 更多 >