2017-02-09 113 views
0

非常简单的代码。一切正常。我将hls_binary和gradx放入comb_binary的方法中。'jumpy.ndarray'对象在jupyter笔记本中第二次运行后无法调用

image = mpimg.imread('test_images/test4.jpg') 
comb_binary = comb_binary(image) 
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(28,16)) 
ax1.imshow(image2) 
ax1.set_title('A', fontsize=20) 
ax2.imshow(comb_binary, cmap = 'gray') 
ax2.set_title('B', fontsize=20) 

但是,如果我重新运行在笔记本电池,我会碰到这个错误:

'numpy.ndarray' object is not callable 

月1日的时间。它的工作原理: enter image description here

运行该小区再次: enter image description here

这里是所有方法的定义,以防万一:

def abs_sobel_thresh(img, orient, sobel_kernel, thresh): 
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) 
    if orient == 'x': 
     sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0) 
    else: 
     sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1) 
    abs_sobel = np.absolute(sobel) 
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel)) 
    grad_binary = np.zeros_like(scaled_sobel) 
    grad_binary[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1 
    return grad_binary 

def hls_select(img, thresh): 
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS) 
    s_channel = hls[:,:,2] 
    hls_binary = np.zeros_like(s_channel) 
    hls_binary[(s_channel > thresh[0]) & (s_channel <= thresh[1])] = 1 
    return hls_binary 

def comb_binary(image): 
    gradx = abs_sobel_thresh(image, orient='x', sobel_kernel=9, thresh=(20, 100)) 
    hls_binary = hls_select(image, thresh=(170, 255)) 
    combined_binary_final = np.zeros_like(gradx) 
    combined_binary_final[(hls_binary == 1) | (gradx == 1)] = 1 
    return combined_binary_final 

回答

3

您评估jupyter细胞时,它都会运行那些从前面的命令构建的环境中的命令。所以,当你有一条线时:

comb_binary = comb_binary(image) 

第一次一切都很好。你只需用它的结果代替comb_binary(函数)。现在comb_binary是一个numpy数组......但是,如果您尝试再次执行该单元格,则comb_binary现在是一个numpy数组 - 不是函数。这是一样的,如果你这样写:

comb_binary = comb_binary(image) 
comb_binary = comb_binary(image) 

而且你不会想到这在大多数情况下工作了;-)。

+0

哦,我是多么愚蠢。对不起,软件新手。所以,我想我需要使用一些不同的变量名来避免这个问题。 – Patrick

+0

@帕特里克 - 别担心。这件事发生在我们所有人身上。我遇到了这个问题,使用了LONG jupyter文档,我重复使用了一个很久以前定义的名字......它有点令人困惑,因为您没有在此处查看以前的代码行。 – mgilson