源代码如下:
img = cv.imread(os.path.join(data_dir,file))
for i in range(0,img.shape[0]):
29 for j in range(0,img.shape[1]):
---> 30 if img[i][j] <= 255:
31 c[i][j] = 0
32 #map(lambda x:[[i,0][i<250] for i in x],img)
报错:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
原因图片是rgb三通道,print(img)时可以看到:
<class 'numpy.ndarray'> [0 0 0]
img[i][j]是一维数组,不能直接判断大小。
改为:
img = cv.imread(os.path.join(data_dir,file),0)
读取灰度图像,保留一个通道
print(img)可以看到
<class 'numpy.uint8'> 0