import cv2import numpy as np# 加载图像img = cv2.imread('image.jpg', 0)# 边缘检测edges = cv2.Canny(img, 100, 200)# 膨胀处理,使边缘更加明显kernel = np.ones((5, 5),np.uint8)edges = cv2.dilate(edges,kernel,iterations = 1)# 获取边缘点edge_points = np.argwhere(edges > 0)# 输出结果print(edge_points)继续写这段代码生成tsplib数据集
时间: 2023-06-17 19:08:43 浏览: 87
# 计算距离矩阵
import math
dist_matrix = []
for i in range(len(edge_points)):
row = []
for j in range(len(edge_points)):
if i == j:
row.append(0)
else:
distance = math.sqrt((edge_points[i][0]-edge_points[j][0])**2 + (edge_points[i][1]-edge_points[j][1])**2)
row.append(distance)
dist_matrix.append(row)
# 生成tsplib数据集文件
with open('dataset.tsp', 'w') as f:
# 写入文件头
f.write('NAME: edge_points\n')
f.write('TYPE: TSP\n')
f.write(f'DIMENSION: {len(edge_points)}\n')
f.write('EDGE_WEIGHT_TYPE: EUC_2D\n')
f.write('NODE_COORD_SECTION\n')
# 写入节点坐标
for i in range(len(edge_points)):
f.write(f'{i+1} {edge_points[i][0]} {edge_points[i][1]}\n')
f.write('EDGE_WEIGHT_SECTION\n')
# 写入距离矩阵
for i in range(len(dist_matrix)):
f.write(' '.join(map(str, dist_matrix[i])) + '\n')
阅读全文