This question already has answers here:
arange: TypeError: unsupported operand type(s) for -: 'list' and 'int'

(2个答案)


在10个月前关闭。




我正在提取特征并将其作为 vector 传递给训练分类器。我收到此错误:
unsupported operand type(s) for -: 'list' and 'int'`

我了解错误,但我似乎不知道自己做错了什么,有什么帮助吗?
def featurestest (img):
    # corners
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)
    corners = np.int0(corners)

    for i in corners:
        x, y = i.ravel()
        cv2.circle(img, (x, y), 3, 255, -1)

    # edges
    edges = cv2.Canny(gray, 10, 100, apertureSize=3)
    minLineLength = 50
    lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi / 180, threshold=100, lines=np.array([]),
                            minLineLength=minLineLength, maxLineGap=80)

    a, b, c = lines.shape
    for i in range(a):
        cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    # print(lines, edges)

    # aspect ratio
    ar = 1.0 * float(img.shape[1] / img.shape[0])

    # skew and kurtosis

    skew = scipy.stats.skew(img)
    kurt = scipy.stats.kurtosis(img)

    for i in range(0, img.shape[0]):
        for j in range(0, img.shape[1]):

         vector_val = np.arange([lines,edges, ar, x, y, skew,kurt])
         return_raf= (vector_val)

    return return_raf

最佳答案

线

vector_val = np.arange([lines, edges, ar, x, y, skew, kurt])

是错的。我不确定您要做什么,但是np.arange会开始,停止和逐步调整大小,并返回该范围内均匀间隔的数字数组。您正在给它一个列表作为它的第一个参数,这是一个类型错误。

出现实际错误消息是因为np.arange在内部通过执行类似于(stop - start) / step的操作来计算其范围。在这种情况下,stop是您提供的列表,start的默认值为0。因此,它正在执行[lines, edges, ar, x, y, skew, kurt] - 0,这会在此处引发确切的TypeError

10-08 00:34