根据上述代码,改写下列程序try: df1 = pd.read_csv('image_3_2.txt', sep='\t', header=None, error_bad_lines=False, na_values='?') df2 = pd.read_csv('image_6.txt', sep='\t', header=None, error_bad_lines=False, na_values='?') tree = KDTree(df2.iloc[:, :2].values) k = 4 distances, indices = tree.query(df1.iloc[:, :2].values, k=k) values = df2.iloc[indices[:, 1:], 2].values mean_values = values.mean(axis=1) df1.iloc[:, 2] = mean_values df1.to_csv('4.txt', sep='\t', header=None, index=None) except Exception as e: print("Error: ", e)
时间: 2024-03-26 09:39:31 浏览: 75
python读csv文件时指定行为表头或无表头的方法
根据上述代码,改写后的程序如下:
```
try:
points3D = pd.read_csv('points3D.txt', sep='\t', header=None, error_bad_lines=False, na_values='?')
skeleton = cv2.imread('skeleton.jpg', cv2.IMREAD_GRAYSCALE)
tree = KDTree(points3D.iloc[:, :2].values)
with open("./voc/skelon/3d/result.txt", 'w', encoding='utf-8') as f1:
for y in range(skeleton.shape[0]):
for x in range(skeleton.shape[1]):
if skeleton[y][x] == 255:
x_axis_coord, y_axis_coord = transform_xy(x, y)
query_point = [x_axis_coord, y_axis_coord]
dist, index = tree.query(query_point)
f1.write(str(points3D.iloc[index, 0]) + " " +
str(points3D.iloc[index, 1]) + " " +
str(points3D.iloc[index, 2]) + " " + '\n')
except Exception as e:
print("Error: ", e)
```
这段代码与原始代码类似,不过是将输入的数据文件和输出的文件名、格式都进行了修改以适应新的应用场景。同时,由于我们无法直接读取图像文件,需要使用cv2库里的imread函数获取二维骨架图像。最后,将得到的三维坐标写入到txt文件中。
阅读全文