img不是一个数字元组

时间:2017-05-03 04:40:11

标签: python opencv computer-vision opticalflow

整个程序是从一个文件夹中可视化从每个图像中提取的光流。当我应用draw_flow函数时会发生问题。我已将图像数组转换为元组,但它不是错误提及的数字元组。我不知道如何解决这个问题。  当我尝试执行以下代码时:

def draw_flow(im, greyim, flow, step = 16):

    h, w = im.shape[:2]
    y, x = np.mgrid[step/2:h:step,step/2:w:step].reshape(2, -1)
    fx, fy = flow[y,x].T
    lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1,2,2)
    lines = np.int32(lines)

    for(x1,y1),(x2,y2) in lines:
        cv2.line(greyim, (x1,y1), 1, (0,255,0), -1)
    return greyim


def main():
    mypath = 'jpl_thomas'
    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath,f)) ]
    color_images = np.empty(len(onlyfiles), dtype=object)
    gray_images = np.empty(len(onlyfiles), dtype=object)

    for n in range(1, len(onlyfiles)):
        color_images[n] = cv2.imread(join(mypath,onlyfiles[n]))

        gray_images[n] = cv2.imread(join(mypath,onlyfiles[n]),0)

    for i in range(2, len(gray_images)):
        flow =cv2.calcOpticalFlowFarneback(gray_images[i],gray_images[i-1], None, 0.5, 3, 15, 3, 5, 1.2, 0)
        draw_flow(color_images[i],tuple(gray_images[i]),flow, step = 16)

我收到如下错误消息:

Traceback (most recent call last):
File "main.python", line 51, in <module>
  main()
File "main.python", line 45, in main
  draw_flow(color_images[i],tuple(gray_images[i]),flow, step = 16)
File "main.python", line 19, in draw_flow
  cv2.line(greyim, (x1,y1), 1, (0,255,0), -1)
TypeError: img is not a numerical tuple

1 个答案:

答案 0 :(得分:1)

为什么要将gray_images[i]作为元组传递?因为,这可以很好地工作,而无需在我的机器上转换为元组。

我猜你已修改此行进行中间测试,因为它不正确。

cv2.line(greyim, (x1,y1), 1, (0,255,0), -1)

根据文档,

cv.Line(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) 

pt2是以元组形式传递的一个点,thickness需要是一个非负数。