python文件读写与序列化
1 import os 2 import pickle 3 import numpy as np 4 import cv2 5 import scipy.io as sio 6 from tqdm import tqdm 7 8 data_root = 'D:/dl_exp/Labeling_lines/linefeature_2' 9 data_img_path = data_root + '/photo' 10 data_path= 'dataOwn' 11 12 data_anno_list = data_img_path + '/anno_shp.txt' 13 14 outPath = data_path + '/dataset' 15 16 if not os.path.exists(outPath): 17 os.makedirs(outPath) 18 19 imgnames=[] 20 with open(data_anno_list, 'r') as file_to_read: 21 while True: 22 str = file_to_read.readline() # 整行读取数据 23 if not str: 24 break 25 imgnames.append(str[:-1]) 26 print(imgnames) 27 for img_name in imgnames: 28 img_file = data_img_path + '/{}{}'.format(img_name, '.tif') 29 f_Points = data_path + '/{}{}'.format(img_name,'_Points.txt' ) 30 f_Edges = data_path + '/{}{}'.format(img_name,'_Edge.txt' ) 31 f_Funcs = data_path + '/{}{}'.format(img_name,'_Junctions.txt') 32 ids = [] 33 xys = [] 34 edges = [] 35 img = cv2.imread(img_file) 36 #读取坐标点 37 with open(f_Points, 'r') as file_to_read: 38 while True: 39 lines = file_to_read.readline() # 整行读取数据 40 if not lines: 41 break 42 id, p_x, p_y = [float(i) for i in lines.split(',')] # 将整行数据分割处理,如果分割符 43 ids.append(id) # 添加新读取的数据 44 xys.append((p_x,p_y)) 45 #ids = np.array(ids) # 将数据从list类型转换为array类型。 46 #xys = np.array(xys) 47 print (xys) 48 #读取线索引 49 with open(f_Edges, 'r') as file_to_read: 50 while True: 51 texts = file_to_read.readline() # 整行读取数据 52 if not texts: 53 break 54 id, line_i, line_j = [float(i) for i in texts.split(',')] # 将整行数据分割处理,如果分割符 55 edges.append((line_i, line_j)) 56 # edges = np.array(edges) 57 #读取角点和角度 58 junctions = [] 59 thetas = [] 60 with open(f_Funcs, 'r') as file_to_read: 61 while True: 62 texts = file_to_read.readline() # 整行读取数据 63 if not texts: 64 break 65 id, p_x1, p_y1, n_theta = [float(i) for i in texts.split(',')] # 将整行数据分割处理 66 junc = (p_x1, p_y1) 67 junctions.append(junc) 68 ths = [] 69 for _ in range(int(n_theta)): 70 text_theta = file_to_read.readline() 71 t = (float(text_theta), float(text_theta)) 72 ths.append(t)#每个角点多个线方向 73 #ths = np.array(ths) 74 thetas.append(ths) 75 #junctions = np.array(junctions) 76 #thetas = np.array(thetas) 77 H = {} 78 H['imagename'] = img_name 79 point=[] 80 #读取坐标点 81 for x, y in xys: 82 point.append((x,y)) 83 H['points'] = point 84 lines=[] 85 #读取线索引 86 for line_i, line_j in edges: 87 lines.append((int(line_i), int(line_j))) 88 #读取角点 89 90 H['lines'] = lines 91 H['junction'] = junctions 92 H['theta'] = thetas 93 H['img'] = img 94 with open(outPath + '/{}{}'.format(img_name,'.pkl'), "wb") as f: 95 pickle.dump(H, f)
1 import pickle 2 from math import cos, pi, sin, sqrt, acos 3 import numpy as np 4 import cv2 5 #00030077.pkl 6 #H.pkl 7 #Z_4_20_DOM 8 data_path= 'dataOwn' 9 outPath = data_path + '/dataset' 10 pkl_file = outPath + '/{}{}'.format('Zurich_7_37_DOM','.pkl') 11 with open(pkl_file, 'rb') as handle: 12 target = pickle.load(handle, encoding='latin1') 13 #print(target) 14 15 junction = target['junction'] 16 theta = target['theta'] 17 points = target['points'] 18 lines = target['lines'] 19 print("角点") 20 print(junction) 21 print("射线角度") 22 print(theta) 23 print("坐标点") 24 print(points) 25 print("线") 26 print(lines) 27 new_ths = [None for _ in theta] 28 img=target['img'] 29 cur_h, cur_w = img.shape[:2] 30 sw = float(cur_w) 31 sh = float(cur_h) 32 scale_param = (sw, sh) 33 theta= [[x for x, _ in th] for th in target['theta']] 34 for cnt0, th in enumerate(theta): 35 bin_t = [None for _ in th] 36 for cnt, t in enumerate(th): 37 x = cos(t * pi / 180.) * scale_param[0] 38 y = sin(t * pi / 180.) * scale_param[1] 39 dist = sqrt(x ** 2 + y ** 2) 40 if abs(y) <= 0.001: 41 nt = 180. if x < 0 else 0. 42 elif y > 0: 43 nt = acos(x / dist) / pi * 180. 44 else: 45 nt = 360. - acos(x / dist) / pi * 180. 46 bin_t[cnt] = nt 47 print("处理后:") 48 print(bin_t) 49 50 junction_color = (0, 0, 255) 51 line_color = (0, 255, 0) 52 line_length = 12 53 nimage = np.zeros((cur_h,cur_w ,3), np.uint8) 54 #cv2.resize(nimage, (origin_w, origin_h)) 55 w, h = cur_w, cur_h 56 for idx, (jx, jy) in enumerate(junction): 57 if not ((jx >= 0 and jx <= w) and (jy >= 0 and jy <= h)): 58 continue 59 try: 60 cv2.circle(nimage, (int(jx), int(jy)), 2, junction_color, -1) 61 except OverflowError: 62 print(jx, jy) 63 raise 64 # 绘制角点的方向线????? 65 for th in theta[idx]: 66 rad = th * pi / 180.0 67 dx, dy = (cos(rad), sin(rad)) 68 xe, ye = (jx + line_length * dx, jy + line_length * dy) 69 xe, ye = min(max(xe, 0), w), min(max(ye, 0), h) 70 print("line: {},{} {},{}".format(jx, jy, xe, ye)) 71 cv2.line(nimage, (int(jx), int(jy)), (int(xe), int(ye)), line_color, 2) 72 cv2.imshow('img', nimage) 73 cv2.waitKey(0)
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2015-01-25 OSG第一个Demo