gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:41:48 25 4
gpt4 key购买 nike

我正在尝试“修复”函数(OpenCV v3,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()

这是我得到的错误:
    Traceback (most recent call last):

在第41行的文件“test3.py”中
hlines [0] = [0,0,0,0]
TypeError:“NoneType”对象不支持项目分配

我想做的是将hlines的第一行分配给[[0,0,0,0]],以便它与数组的其余部分匹配(意味着有数据)。为什么这不起作用?

最佳答案

HoughLinesP函数在第一帧上返回一个空数组(返回“None”)。这会导致错误。添加检查可以解决该问题。

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)

关于python - 检查数组行是否为None并为其赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43685869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com