img 不是数字元组
img is not a numerical tuple
整个程序是可视化从一个文件夹中的每个图像中提取的光流。当我应用 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
为什么要将 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
需要是一个非负数。
整个程序是可视化从一个文件夹中的每个图像中提取的光流。当我应用 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
为什么要将 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
需要是一个非负数。