我试图在视频中找到垂直线条。 Python 2.7和OpenCV 3。 我正在使用背景减法,然后应用Canny边缘检测滤镜。 我已经能够将HoughLinesP方法应用于单个图像,但需要将其扩展为视频。
我在运行一组基本代码时收到此错误(我认为这对应于下面的行(30-33):
Traceback (most recent call last):
File "test3.py", line 33, in <module>
print(hlines.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
奇怪的是,如果Iadjust代码使得背景减法不应用(注释第26行并更改第28行,以便它指的是&#39;框架&#39;而不是&#39; img_sub&#39;),一切正常。我错过了什么?
我可以很好地输出背景减去的视频流,并按预期工作
我可以输出/打印(hlines)就好了
&#39; hlines&#39;的终端输出示例
[[470 109 470 109]]
[[337 259 337 259]]
[[330 267 330 267]]
[[338 324 338 324]]
[[338 289 338 289]]]
但是,只要我尝试使用hlines.shape属性,事情就会变得糟糕。
import cv2
import numpy as np
import imutils
np.set_printoptions(threshold=np.inf) #to print entire array, no truncation
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
#background subtraction parameter
bkgnd = cv2.bgsegm.createBackgroundSubtractorMOG()
camera =cv2.VideoCapture('/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4')
while(True):
(grabbed, frame) = camera.read()
img_sub = bkgnd.apply(frame)#, learningRate = 0.001)
canny_threshold = cv2.Canny(img_sub, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND)
hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)
print(hlines)
print(hlines.shape)
#a,b,c = hlines.shape
#for k in range(a):
#print(hlines.shape)
#break
#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)
# print("getting hlines")
# break
cv2.imshow('canny_threshold', canny_threshold)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
任何建议都将不胜感激。
编辑:
@ivan_pozdeev向我指出this question.
看起来确实适用。但是,在尝试遵循他们的解决方案时,我收到以下错误:
追踪(最近一次通话): 文件&#34; test3.py&#34;,第37行,in 对于l in hlines: TypeError:&#39; NoneType&#39;对象不可迭代
这是经过修改的代码段:
hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)
for l in hlines:
leftx, boty, rightx, topy = l[0]
line = Line((leftx, boty), (rightx, topy))
line.draw(frame, (0, 255, 0), 2)
现在,如果我没有数据,这对我有意义......但我做在hlines中返回数据。我可以使用print(hlines)语句将其打印出来。但是,它的格式与OpenCV 2.4不同......但是,现在它让我想知道为什么它在另一个问题中起作用...
EDIT2:
啊哈!取得进展。这里是脚本的输出(print(hlines)),如果我只循环它两次...即,当run_once = 0而(run_once&lt; = 1)... run_once = 1+ run_once在结尾< / p>
None
[[[690 419 693 419]]
[[672 419 679 419]]
[[696 417 701 417]]
[[713 419 714 419]]
[[504 418 504 418]]
[[688 419 688 419]]
[[672 417 679 417]]
[[510 419 511 419]]
[[693 418 693 417]]
[[688 417 688 417]]
[[692 417 692 417]]
[[696 419 699 419]]
[[713 417 714 417]]
[[686 419 686 419]]
[[622 417 622 417]]
[[690 417 690 417]]
[[506 417 506 417]]
[[622 419 623 419]]
[[505 419 505 419]]
[[686 417 687 417]]
[[506 419 506 419]]
[[700 419 701 418]]
[[509 418 509 418]]
[[623 417 623 417]]
[[510 417 511 417]]
[[712 418 712 418]]
[[685 418 685 418]]
[[689 417 689 417]]
[[689 419 689 419]]
[[671 418 671 418]]
[[691 417 691 417]]]
所以看起来第一行的hlines是&#34; None&#34;。我想,这解释了错误。但是,现在我需要弄清楚如何添加一个&#34;空行&#34; hlines ....
答案 0 :(得分:0)
按照@ivan_pozdeev上面发布的链接并检查返回类型为无修复问题。
HoughLinesP函数在第一帧上返回一个空数组(返回&#34; None&#34;)。这导致了错误。添加支票以解决问题。
uploadImg.addActionListener(e -> {
Display.getInstance().openGallery(evt -> {
String filePath = (String) evt.getSource();
int fileNameIndex = filePath.lastIndexOf("/") + 1;
String fileName = filePath.substring(fileNameIndex);
System.out.println(fileName);
System.out.println(filePath);
try {
InputStream is = FileSystemStorage.getInstance().openInputStream(filePath);
Image i = Image.createImage(is);
Image listingMask = resourceObjectInstance.getImage("refimg2.jpg");
imgLabel.setIcon(i.fill(listingMask.getWidth(), listingMask.getHeight()));
imgLabel.setHidden(false);
refreshTheme();
} catch (IOException ex) {
System.out.println(ex);
}
}, Display.GALLERY_IMAGE);
});