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

时间:2019-04-26 20:33:32

标签: python opencv

我有一个奶牛场的视频。我的目标是-

(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)将字符串作为第一个参数,如何插入框架在这里作为参数?

1 个答案:

答案 0 :(得分:1)

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

waitTime = 0

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

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