4

我正在尝试使用 cv2.calcOpticalFlowPyrLK 在 Python 上实现 Lucas-Kanade 光流算法。但是,当我尝试画一条线来连接当前点和以前的点以及点本身时,函数返回 None。

这是代码

#draw the overlaying tracking img
for i,(new,old) in enumerate(zip(good_new,good_old)):
    a,b = new.ravel() #tmp new value
    c,d = old.ravel() #tmp old value

    #draws a line connecting the old point with the new point
    mask = cv2.line(mask,(a,b),(c,d),color[i].tolist(),2) #returns None... why??
    #draws the new point
    frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) #returns None... why??

img = cv2.add(frame,mask)

#show on window
cv2.imshow("frame",img)

a,b,c,d 是 numpy.float32

frame 是我从网络摄像头读取的值

掩码初始化为一个零数组

颜色是一个随机数组

这是我尝试 imshow 时收到的错误消息

error: /Users/vagrant/pisi-64bit/tmp/opencv-2.4.5-2/work/opencv-2.4.5/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

我真的很感激帮助!

4

1 回答 1

11

根据 OpenCV 文档,cv2.line() 和 cv2.circle() 总是返回 None。http://docs.opencv.org/modules/core/doc/drawing_functions.html

这些方法是就地方法,因此它们会更改您传递给它们的第一个参数。请参阅描述这些函数类型的 Stack Overflow 问题:Python 就地运算符函数与标准运算符函数有何不同?

您的固定代码应如下所示:

#draw the overlaying tracking img
for i,(new,old) in enumerate(zip(good_new,good_old)):
    a,b = new.ravel() #tmp new value
    c,d = old.ravel() #tmp old value

    #draws a line connecting the old point with the new point
    cv2.line(mask,(a,b),(c,d),color[i].tolist(),2) #returns None... why??
    #draws the new point
    cv2.circle(frame,(a,b),5,color[i].tolist(),-1) #returns None... why??

img = cv2.add(frame,mask)
于 2013-11-20T04:09:35.857 回答