修改程序with open('image_5.txt', 'w') as f: for i in range(matrix.shape[0]): for j in range(matrix.shape[1]): # 将 i 和 j 映射到新的坐标系中 i_new = i * avg_interval_dx + col1_min j_new = j * avg_interval_dy + col2_min # 将像素值乘以 37.463 并保存到 txt 文件中 if matrix[i, j] > 0 or matrix[i, j] < 0: f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.5f}\n') else: f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.5f}\n'),将i_new和j_new的值保留小数点后五位
时间: 2024-04-01 18:37:25 浏览: 17
修改后的程序如下:
with open('image_5.txt', 'w') as f:
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
# 将 i 和 j 映射到新的坐标系中
i_new = round(i * avg_interval_dx + col1_min, 5)
j_new = round(j * avg_interval_dy + col2_min, 5)
# 将像素值乘以 37.463 并保存到 txt 文件中
if matrix[i, j] > 0 or matrix[i, j] < 0:
f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.5f}\n')
else:
f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.5f}\n')
在代码中使用了 round() 函数来保留小数点后五位。其中 round() 函数的第一个参数是需要保留小数点后几位,第二个参数是需要保留的浮点数。
相关问题
程序执行提示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]))
这个错误提示是因为在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值,从而避免了上述错误。
修改程序gray = cv2.imread('skeleton_gaussian_high_1_binary.bmp', cv2.IMREAD_GRAYSCALE) matrix = np.array(gray) matrix = matrix / 255 # 将像素值乘以 37.463 并保存到 txt 文件中 with open('image_5.txt', 'w') as f: for i in range(matrix.shape[0]): for j in range(matrix.shape[1]): # 将 i 和 j 映射到新的坐标系中 i_new = i * avg_interval_dx + col1_min j_new = j * avg_interval_dy + col2_min # 将像素值乘以 37.463 并保存到 txt 文件中 if matrix[i, j] > 0 or matrix[i, j] < 0: f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.4f}\n') else: f.write(f'{i_new}\t{j_new}\t{matrix[i, j] * 37.463 - 25.45:.4f}\n'),提取图像中像素点为白色的对应的像素值和对应的矩阵i、j坐标
可以将代码修改为以下形式:
```
gray = cv2.imread('skeleton_gaussian_high_1_binary.bmp', cv2.IMREAD_GRAYSCALE)
matrix = np.array(gray)
matrix = matrix / 255
white_pixels = []
white_pixel_coordinates = []
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
if matrix[i, j] > 0:
white_pixels.append(matrix[i, j] * 37.463 - 25.45)
white_pixel_coordinates.append((i, j))
with open('image_5.txt', 'w') as f:
for i, coord in enumerate(white_pixel_coordinates):
# 将 i 和 j 映射到新的坐标系中
i_new = coord[0] * avg_interval_dx + col1_min
j_new = coord[1] * avg_interval_dy + col2_min
# 将像素值乘以 37.463 并保存到 txt 文件中
f.write(f'{i_new}\t{j_new}\t{white_pixels[i]:.4f}\n')
```
这样修改后,可以提取图像中像素点为白色的对应的像素值和对应的矩阵i、j坐标,并将它们保存到 txt 文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)