【问题标题】:img is not a numpy array, neither a scalar ... But 'img' isn't used in my code as a variable nameimg 不是一个 numpy 数组,也不是一个标量......但是我的代码中没有使用 'img' 作为变量名
【发布时间】:2017-04-28 01:50:08
【问题描述】:

我正在尝试在视频中查找垂直线。 Python 2.7 和 OpenCV 3。
我正在使用背景减法,然后应用 Canny 边缘检测过滤器。
我已经能够将 HoughLinesP 方法应用于单个图像,但需要将其扩展到视频。

我在运行一组基本代码时收到此错误(我相信它对应于下面的“a,b,c = hlines.shape”行):

Video file not grabbed
Traceback (most recent call last):
File "test2.py", line 60, in <module>
cv2.line(camera, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)
TypeError: img is not a numpy array, neither a scalar

现在,发生了一些奇怪的事情,首先“img”不是这个脚本中的变量名(尽管它在我在过去并打电话给 HoughLinesP……尽管这在这里可能无关紧要)。另一个奇怪的是,相同的代码在从该视频文件中获取的 .PNG 图像上运行良好。
我能够很好地打开视频文件并应用上述过滤器。
现在,有趣的是,出于某种原因……“if not args.get("video", False):" case 也被输入了。即使我可以通过终端“访问”该视频文件也很好。
我也可以输出 (print(hlines.shape)) 就好了...

这是代码。注释掉“for循环”的行可以让它运行得很好。

import cv2
import numpy as np
import imutils 
import argparse

np.set_printoptions(threshold=np.inf) #to print entire array, no truncation

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help = "/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4")

args = vars(ap.parse_args())

LOWER_BOUND = 55   #cv2.threshold()
UPPER_BOUND = 255  #cv2.threshold()

CANNY_LOWER_BOUND = 10  #cv2.Canny()
CANNY_UPPER_BOUND = 250 #cv2.Canny()

MIN_LINE_LENGTH = 2  #HoughLinesP()
MAX_LINE_GAP = 100     #HoughLinesP() 
HOUGH_THETA = np.pi/180 #HoughLinesP() angle resolution of the accumulator, radians
HOUGH_THRESHOLD = 25 #HoughLinesP() 
HOUGH_RHO = 1         #HoughLinesP() rho, Distance resolution of the accumulator, pixels


#bkgnd = cv2.bgsegm.createBackgroundSubtractorMOG()
camera = cv2.VideoCapture('/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4')

# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
    camera = cv2.VideoCapture(0)
    print("Video file not grabbed")

# otherwise, grab a reference to the video file
else:
    camera = cv2.VideoCapture(args["video"])

while(True):
    (grabbed, frame) = camera.read()

# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
    if args.get("video") and not grabbed:
        break

# resize the frame, blur it, and convert it to the HSV
# color space
    frame = imutils.resize(frame, width=600)

    canny_threshold = cv2.Canny(frame, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND)    
    hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)

    a,b,c = hlines.shape

    for k in range(a):
        #pretty sure the issue is somewhere here
        cv2.line(camera, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)



cv2.imshow("Frame", frame)
cv2.imshow('image', canny_threshold)





if cv2.waitKey(1) & 0xFF == ord('q'):
    break

camera.release()
cv2.destroyAllWindows()

任何建议将不胜感激。

【问题讨论】:

  • 如果您检查了出现错误的函数调用的documentation,您会看到imgcv2.line 的第一个参数的名称。查看您作为该参数传递的对象。另外,学习查找文档。
  • 试试cv2.line(frame, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)`
  • @user2357112:没有理由讨厌。一个简单的“嘿,你向函数传递了错误的东西”就足够了,而且不会让人觉得是个混蛋。
  • @Jeru Luke:谢谢!这很有帮助!

标签: python opencv houghlinesp


【解决方案1】:

@Jeru Luke 的回答似乎成功了。 我的函数调用错误。

据我了解问题...我试图使用原始视频捕获变量(文件路径)作为我正在解析的数组。这个原始变量不是一个数组(它是一个文本字符串)......回想起来,这使得错误更有意义。 我将抛出错误的行更改为:

cv2.line(frame, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)  `

这让我可以遍历数组。

【讨论】:

  • 为什么不给个详细的答复?
  • 您可以选择验证您的答案
  • 说我得先等几天。 (现在要弄清楚下一个错误“AttributeError:'NoneType'对象没有属性......'
  • 出现在哪一行?
  • 如果我取消注释第 26 行 (bkgnd = cv2. ...) 并添加一些其他语句...它可能需要一个全新的问题,因为代码得到了一点调整(附加功能来电)。我将进行一些挖掘,但可能很快就会在这里发布另一个问题。
猜你喜欢
  • 2018-12-18
  • 2014-05-30
  • 2015-04-27
  • 2018-03-12
  • 2018-08-10
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
相关资源
最近更新 更多