python:未定义全局名称

时间:2014-12-15 20:52:31

标签: python opencv numpy

所以我使用opencv,numpy和python进行计算机视觉项目。我将离开本教程:http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html 并完全遵循它(减去代码中显而易见的一些小改动)。我不确定为什么我会收到此错误(如下),并且非常感谢您对此有所了解。谢谢!

代码:

def applyHarrisCornerDetection():
    image1 = cv2.imread('pictures/board1.png')
    image2 = cv2.imread('pictures/board2.png')

    gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)

    gray1 = np.float32(gray1)
    gray2 = np.float32(gray2)

    dst1 = cv2.cornerHarris(gray1,2,3,0.04)
    dst2 = cv2.cornerHarris(gray2,2,3,0.04)

    dst1 = cv2.dilate(dst1,None)
    dst2 = cv2.dilate(dst2,None)

    img1[dst1>0.01*dst1.max()]=[0,0,255]
    img2[dst2>0.01*dst2.max()]=[0,0,255]

    cv2.imshow('dst1',img1)
    cv2.imshow('dst2',img2)

    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()

错误: enter image description here

1 个答案:

答案 0 :(得分:0)

这是因为这两行:

img1[dst1>0.01*dst1.max()]=[0,0,255]
img2[dst2>0.01*dst2.max()]=[0,0,255]

您正在将元素分配给列表中的某个位置,但该列表尚未初始化。