diff --git a/main.py b/main.py index 982afec..dfbc869 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,8 @@ if __name__ == "__main__": root = tk.Tk() - w, h = root.winfo_screenwidth()-10, root.winfo_screenheight()-200 + w, h = root.winfo_screenwidth()-10, root.winfo_screenheight()-100 root.geometry("%dx%d+0+0" % (w, h)) + root.title("Multi Map & Waypoints Merger") app = Application(root) app.mainloop() \ No newline at end of file diff --git a/map_trimmer.py b/map_trimmer.py index c4927ea..24b689a 100644 --- a/map_trimmer.py +++ b/map_trimmer.py @@ -76,7 +76,7 @@ parent=self.master, title="Select map yaml file", initialdir=str(Path(".")), - filetypes=[("YAML", "yaml")] + filetypes=[("YAML", ".yaml")] ) if not filepath: return with open(filepath) as file: # .yamlを読み込む @@ -292,7 +292,6 @@ if __name__ == "__main__": - print("\n\n") root = tk.Tk() w, h = root.winfo_screenwidth()-10, root.winfo_screenheight()-200 root.geometry("%dx%d+0+0" % (w, h)) diff --git a/mylib/__pycache__/__init__.cpython-38.pyc b/mylib/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..f053f9d --- /dev/null +++ b/mylib/__pycache__/__init__.cpython-38.pyc Binary files differ diff --git a/mylib/__pycache__/application.cpython-38.pyc b/mylib/__pycache__/application.cpython-38.pyc new file mode 100644 index 0000000..ceee298 --- /dev/null +++ b/mylib/__pycache__/application.cpython-38.pyc Binary files differ diff --git a/mylib/__pycache__/mapdisp.cpython-38.pyc b/mylib/__pycache__/mapdisp.cpython-38.pyc new file mode 100644 index 0000000..2c68c05 --- /dev/null +++ b/mylib/__pycache__/mapdisp.cpython-38.pyc Binary files differ diff --git a/mylib/__pycache__/tools.cpython-38.pyc b/mylib/__pycache__/tools.cpython-38.pyc new file mode 100644 index 0000000..40bbae7 --- /dev/null +++ b/mylib/__pycache__/tools.cpython-38.pyc Binary files differ diff --git a/mylib/__pycache__/waypointlib.cpython-38.pyc b/mylib/__pycache__/waypointlib.cpython-38.pyc new file mode 100644 index 0000000..f61a787 --- /dev/null +++ b/mylib/__pycache__/waypointlib.cpython-38.pyc Binary files differ diff --git a/mylib/application.py b/mylib/application.py index c32d961..4d9dee2 100644 --- a/mylib/application.py +++ b/mylib/application.py @@ -1,5 +1,6 @@ import tkinter as tk import tkinter.filedialog +import re from pathlib import Path from .tools import Tools @@ -86,15 +87,15 @@ def menu_export(self, event=None): if (len(self.tools.label_list) < 2): return win = tk.Toplevel() - win.geometry("500x300+50+50") + win.geometry("800x300+50+50") win.minsize(width=500, height=300) win.attributes('-topmost', True) win.title("Export") font = ("Consolas", 12) - ### ファイルパスを参照するダイアログを開く ### + ### ファイルパスを参照するダイアログを開く関数(ボタンコールバック) ### def ref_btn_callback(entry: tk.Entry, init_dir): - filepath = tkinter.filedialog.askopenfilename( + filepath = tkinter.filedialog.asksaveasfilename( parent=win, title="File path to export", initialdir=init_dir, @@ -104,7 +105,7 @@ entry.delete(0, tk.END) entry.insert(tk.END, str(filepath)) - ### ファイルの保存場所の表示、変更をするフィールドを作成する ### + ### ファイルの保存場所の表示、変更をするフィールドを作成する関数 ### def create_entry(label_txt, init_path): frame = tk.Frame(win) frame.pack(expand=False, fill=tk.X, padx=5, pady=10) @@ -128,7 +129,7 @@ merged_img_pil, merged_yaml, path = self.tools.get_merged_map() tbox3 = create_entry("- Merged map (yaml, pgm) file:", str(path)) - ### それぞれをファイルに書き込む ### + ### それぞれをファイルに書き込む関数(ボタンコールバック) ### def export_btn_callback(): path = tbox1.get() with open(path, 'w') as f: @@ -137,8 +138,9 @@ with open(path, 'w') as f: f.write(wp_yaml) path = Path(tbox3.get()).resolve() + new_merged_yaml = re.sub("image: .*\n", "image: {}\n".format(str(path.with_suffix(".pgm"))), merged_yaml) with open(str(path.with_suffix(".yaml")), 'w') as f: - f.write(merged_yaml) + f.write(new_merged_yaml) merged_img_pil.save(str(path.with_suffix(".pgm"))) win.destroy() return diff --git a/mylib/tools.py b/mylib/tools.py index aba5853..5dd620f 100644 --- a/mylib/tools.py +++ b/mylib/tools.py @@ -210,7 +210,7 @@ base_map: MyMap = self.map_disp.map_dict[self.map_disp.base_map_key] line = "\n" yaml = "multimap:" + line - name = "multi-" + name = "multi" for label in self.label_list: yaml += "- map:" + line mymap: MyMap = self.map_disp.get_map(label.map_path) @@ -303,13 +303,14 @@ right = img_corners[:, [0, 2, 4, 6]].max() lower = img_corners[:, [1, 3, 5, 7]].max() # 合成後の画像を準備 - img_size = (round(lower-upper)+2, round(right-left)+2) #(y, x) + offset = 2 # 結合時の大きさの不揃いを解消 + img_size = (int(round(lower-upper)+offset), int(round(right-left)+offset)) #(y, x) unknown = np.full(img_size, 205, dtype=np.uint8) # 未確定領域 obstacles = np.full(img_size, 255, dtype=np.uint8) # 障害物が0, それ以外が255 free_area = np.full(img_size, 0, dtype=np.uint8) # 走行可能領域が255, それ以外が0 # ベースの画像を合成 img = np.array(base_map.original_img_pil.convert("L"), dtype=np.uint8) - x, y = round(img_corners[0,0]-left), round(img_corners[0,1]-upper) + x, y = int(round(img_corners[0,0]-left)), int(round(img_corners[0,1]-upper)) h, w = img.shape obstacles[y:y+h, x:x+w] = obstacles[y:y+h, x:x+w] & np.where(img>100, 255, 0) free_area[y:y+h, x:x+w] = free_area[y:y+h, x:x+w] | np.where(img<230, 0, 255) @@ -322,21 +323,22 @@ img = img.rotate(-math.degrees(mymap.get_rotate_angle()), resample=Image.Resampling.NEAREST, expand=True, fillcolor=205) img = np.array(img, dtype=np.uint8) - x = round(img_corners[i, [0, 2, 4, 6]].min() - left) - y = round(img_corners[i, [1, 3, 5, 7]].min() - upper) + x = int(round(img_corners[i, [0, 2, 4, 6]].min() - left)) + y = int(round(img_corners[i, [1, 3, 5, 7]].min() - upper)) h, w = img.shape obstacles[y:y+h, x:x+w] = obstacles[y:y+h, x:x+w] & np.where(img>100, 255, 0) free_area[y:y+h, x:x+w] = free_area[y:y+h, x:x+w] | np.where(img<230, 0, 255) name += "-" + Path(label.map_path).with_suffix("").name # 未確定領域、走行可能領域、障害物を合成し、画像に変換 merged_img = (unknown | free_area) & obstacles + merged_img = merged_img[:-offset, :-offset] # offset分を削除 merged_img = Image.fromarray(merged_img, "L") # 原点から合成後の画像の左下までの距離 origin = [left-base_map.img_origin[0], -lower+base_map.img_origin[1], 0] origin[0] = origin[0] * base_map.resolution origin[1] = origin[1] * base_map.resolution line = "\n" - merged_yaml = "image: " + base_map.yaml_path.with_name(name+".pgm").name + line + merged_yaml = "image: ./" + base_map.yaml_path.with_name(name+".pgm").name + line merged_yaml += "resolution: " + str(base_map.resolution) + line merged_yaml += "origin: " + str(origin) + line merged_yaml += "negate: " + "0" + line