程序执行提示ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(),修改程序gray = cv2.imread('skeleton_median_binary.bmp') binary_matrix = (gray > 0).astype(int) with open('image_1.txt', 'w') as f: for i in range(binary_matrix.shape[0]): for j in range(binary_matrix.shape[1]): if binary_matrix[i][j] == 1: f.write("{} {} {}\n".format(i * 2 + 1, j * 3 + 2, binary_matrix[i][j]))
时间: 2024-03-22 07:41:49 浏览: 78
python3.7解决最小二乘遇到ValueError:Expected 2D array, got 1D array instead: array=[5.].关于reshape和predict
这个错误提示是因为在if语句中使用了一个numpy数组,而numpy数组的真值判断是不允许的。要解决这个问题,可以使用numpy提供的`numpy.any()`函数来判断numpy数组中是否存在True值。下面是修改后的代码:
```python
gray = cv2.imread('skeleton_median_binary.bmp', cv2.IMREAD_GRAYSCALE)
binary_matrix = (gray > 0).astype(int)
with open('image_1.txt', 'w') as f:
for i in range(binary_matrix.shape[0]):
for j in range(binary_matrix.shape[1]):
if binary_matrix[i][j].any():
f.write("{} {} {}\n".format(i * 2 + 1, j * 3 + 2, binary_matrix[i][j]))
```
在if语句中,使用了`.any()`函数来判断二值矩阵中是否存在True值,从而避免了上述错误。
阅读全文