Python 如何修复此错误:没有足够的值来解包(预期值为4,实际值为1)
我的密码是:Python 如何修复此错误:没有足够的值来解包(预期值为4,实际值为1),python,arrays,image,numpy,Python,Arrays,Image,Numpy,我的密码是: def averaged_slope_intercept(mage, lines, line=None): left_fit = [] right_fit = [] x1, y1, x2, y2 = line.reshape(4) for line in lines: line.reshape(4) x1, y1, x2, y2 = line.reshape(4) parameters = np.po
def averaged_slope_intercept(mage, lines, line=None):
left_fit = []
right_fit = []
x1, y1, x2, y2 = line.reshape(4)
for line in lines:
line.reshape(4)
x1, y1, x2, y2 = line.reshape(4)
parameters = np.polyfit((x1,x2),(y1,y2), 1)
print(parameters)
slope = parameters[0]
intercept = parameters[1]
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
print(left_fit)
print(right_fit)
left_fit_avarage = np.average(left_fit, axis=0)
right_fit_avarage = np.average(right_fit, axis=0)
print(left_fit_avarage, "left")
print(right_fit_avarage, "right")
left_line = make_coordinates(mage, left_fit_average)
right_line = make_coordinates(mage, right_fit_average)
return np.array([left_line, right_line])
def平均斜率截距(图像、线条、线条=无):
左拟合=[]
右拟合=[]
x1,y1,x2,y2=直线。重塑(4)
对于行中的行:
线条。重塑(4)
x1,y1,x2,y2=直线。重塑(4)
参数=np.多边形拟合((x1,x2),(y1,y2),1)
打印(参数)
斜率=参数[0]
截距=参数[1]
如果坡度<0:
左拟合附加((斜率,截距))
其他:
右拟合附加((斜率,截距))
打印(左对齐)
打印(右对齐)
左拟合平均值=np.平均值(左拟合,轴=0)
右拟合平均值=np.平均值(右拟合,轴=0)
打印(左对齐,左对齐)
打印(右对齐,右对齐)
左线=生成坐标(图像、左拟合平均值)
右线=生成坐标(图像、右拟合平均值)
返回np.array([左\u行,右\u行])
但我一直在犯这样的错误:
:没有足够的值来解包(预期为4,实际为1)
在生产线上:
x1,y1,x2,y2=直线。重塑(4)
教训是@
返回前面提到的numpy数组,该数组是一个单值,但包含4项。您可以通过以下方式修复此问题并执行
def平均斜率截距(图像、线条、线条=无):
左拟合=[]
右拟合=[]
#x1,y1,x2,y2=直线。重塑(4)#移除
对于行中的行:
#线条。重塑(4)#移除
x1,y1,x2,y2=*线条。重塑(4)#更新
参数=np.多边形拟合((x1,x2),(y1,y2),1)
打印(参数)
斜率=参数[0]
截距=参数[1]
如果坡度<0:
左拟合附加((斜率,截距))
其他:
右拟合附加((斜率,截距))
打印(左对齐)
打印(右对齐)
左拟合平均值=np.平均值(左拟合,轴=0)
右拟合平均值=np.平均值(右拟合,轴=0)
打印(左对齐,左对齐)
打印(右对齐,右对齐)
左线=生成坐标(图像、左拟合平均值)
右线=生成坐标(图像、右拟合平均值)
返回np.array([左\u行,右\u行])
重塑()
返回一个numpy
数组,而不是4个值。您可以将行调整为x1、y1、x2、y2=*line。重塑(4)
以解构数组以分配值。line
或line
看起来像什么?它有一个重塑
方法这一事实意味着它是一个numpy数组,但是数据类型和形状是什么呢<代码>a,b,c,d=np。一个((4,3))
工作正常。数组在第一维是可迭代的。我们需要更多地了解line
。程序的主体被分割成调用的函数,其中:lines=cv2.HoughLinesP(crapped_image,2,np.pi/180,100,np.array([]),minLineLength=40,maxLineGap=5)定义了行variable@MNFsoft,我知道你的代码在语义上不正确,不确定传入的行
参数将如何帮助您的算法。
def averaged_slope_intercept(mage, lines, line=None):
left_fit = []
right_fit = []
#x1, y1, x2, y2 = line.reshape(4) # Removed
for line in lines:
#line.reshape(4) # Removed
x1, y1, x2, y2 = *line.reshape(4) # Updated
parameters = np.polyfit((x1,x2),(y1,y2), 1)
print(parameters)
slope = parameters[0]
intercept = parameters[1]
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
print(left_fit)
print(right_fit)
left_fit_avarage = np.average(left_fit, axis=0)
right_fit_avarage = np.average(right_fit, axis=0)
print(left_fit_avarage, "left")
print(right_fit_avarage, "right")
left_line = make_coordinates(mage, left_fit_average)
right_line = make_coordinates(mage, right_fit_average)
return np.array([left_line, right_line])