Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查数组行是否为None并为其赋值_Python_Arrays_Opencv_Numpy_Houghlinesp - Fatal编程技术网

Python 检查数组行是否为None并为其赋值

Python 检查数组行是否为None并为其赋值,python,arrays,opencv,numpy,houghlinesp,Python,Arrays,Opencv,Numpy,Houghlinesp,我正在尝试修复函数opencvv3 HoughLinesP的数据输出。输出的第一行始终为None。。。而其余的输出似乎工作得很好。 我试图检查第一行,实际上,任何没有的行,并给它赋值0,这样我就可以遍历数组。 现在我有这个,但它似乎不起作用: import cv2 import numpy as np import imutils np.set_printoptions(threshold=np.inf) #to print entire array, no truncation LOWE

我正在尝试修复函数opencvv3 HoughLinesP的数据输出。输出的第一行始终为None。。。而其余的输出似乎工作得很好。 我试图检查第一行,实际上,任何没有的行,并给它赋值0,这样我就可以遍历数组。 现在我有这个,但它似乎不起作用:

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


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

run_once = 0

while(run_once <= 1):
    (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)

#************************* The if statement for the empty element*****
    if hlines is None:
        hlines[0] = [0, 0, 0, 0]
    else:

        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            line = Line((leftx, boty), (rightx, topy)) 
            line.draw(frame, (0, 255, 0), 2)
#*************************************************************      

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

run_once = 1 + run_once 

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

camera.release()
cv2.destroyAllWindows()
文件test3.py,第41行,在 hline[0]=[0,0,0,0] TypeError:“非类型”对象不支持项分配


我要做的是将第一行hline分配给[[0,0,0,0]],使其与数组的其余部分匹配,这意味着有数据。为什么这不起作用

HoughLinesP函数在第一帧返回空数组,但未返回空数组。这导致了一系列错误。添加一个用于修复问题的检查

if hlines is None: #in case HoughLinesP fails to return a set of lines
        #make sure that this is the right shape [[ ]] and ***not*** []
        hlines = [[0,0,0,0]]
    else:
        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)

错误发生在代码的前面。它甚至还没有达到无测试。这很奇怪,因为如果我删除这张支票。。。然后它在数组的None值上出错。这可能发生在早期的循环中。cv2.HoughLinesP通常产生什么,为什么/什么时候可能不产生?如果hlines确实没有,那么使用hlines[0]是没有意义的。这就像不写[0]。hlines is None不测试空数组或包含None的列表。它测试变量本身是否为“None”。HoughLinesP似乎在第一次传递时返回None。。。然后在我的While循环的每个后续过程中成功。这应该很好。。。只要我能去掉None,或者在有None的情况下让它继续迭代,若它返回None,你们需要做什么吗?您可以使用hline=[[0,0,0,0]],但为什么还要这样做呢?您的代码不会将其传递给else主体。
if hlines is None: #in case HoughLinesP fails to return a set of lines
        #make sure that this is the right shape [[ ]] and ***not*** []
        hlines = [[0,0,0,0]]
    else:
        for l in hlines:
            leftx, boty, rightx, topy = l[0]
            cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)