我试图在视频中找到垂直线。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()
任何建议,将不胜感激。