1
|
import cv2 as cv
|
2
|
import numpy as np
|
3
|
import tkinter as tk
|
4
|
from tkinter import filedialog, messagebox
|
5
|
import os
|
6
|
from PIL import Image, ImageTk
|
7
|
class SharedData:
|
8
|
def __init__(self):
|
9
|
self.left_image = None
|
10
|
self.right_image = None
|
11
|
self.overlap_region = 0
|
12
|
class ImageIO:
|
13
|
def __init__(self, shared_data):
|
14
|
self.shared_data = shared_data
|
15
|
def load_image(self, path):
|
16
|
return cv.imread(path, cv.IMREAD_COLOR)
|
17
|
def write_image(self, img, fileName="image.png"):
|
18
|
if cv.imwrite(fileName, img):
|
19
|
print(f"File {fileName} Written Successfully")
|
20
|
else:
|
21
|
print("File writing failed")
|
22
|
def show_image(self, img, title="Image Title", canvas=None):
|
23
|
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
|
24
|
img_pil = Image.fromarray(img_rgb)
|
25
|
img_tk = ImageTk.PhotoImage(img_pil)
|
26
|
canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
|
27
|
canvas.image = img_tk
|
28
|
class Commands:
|
29
|
def __init__(self, gui, shared_data):
|
30
|
self.gui = gui
|
31
|
self.shared_data = shared_data
|
32
|
self.io = ImageIO(shared_data)
|
33
|
def load_left_image(self):
|
34
|
filepath = filedialog.askopenfilename(title="Select Left Image")
|
35
|
if filepath:
|
36
|
self.shared_data.left_image = self.io.load_image(filepath)
|
37
|
print("Left image loaded.")
|
38
|
def load_right_image(self):
|
39
|
filepath = filedialog.askopenfilename(title="Select Right Image")
|
40
|
if filepath:
|
41
|
self.shared_data.right_image = self.io.load_image(filepath)
|
42
|
print("Right image loaded.")
|
43
|
def show_left_image(self):
|
44
|
if self.shared_data.left_image is not None:
|
45
|
self.io.show_image(self.shared_data.left_image, canvas=self.gui.canvas)
|
46
|
print("Left image displayed.")
|
47
|
else:
|
48
|
messagebox.showerror("Error", "No left image loaded!")
|
49
|
def show_right_image(self):
|
50
|
if self.shared_data.right_image is not None:
|
51
|
self.io.show_image(self.shared_data.right_image, canvas=self.gui.canvas)
|
52
|
print("Right image displayed.")
|
53
|
else:
|
54
|
messagebox.showerror("Error", "No right image loaded!")
|
55
|
def set_overlap_region(self):
|
56
|
self.shared_data.overlap_region = self.gui.overlap.get()
|
57
|
print(f"Overlap region set to {self.shared_data.overlap_region}")
|
58
|
|
59
|
|
60
|
class GUI:
|
61
|
|
62
|
## \brief This class creates a graphical user interface for the image projector.
|
63
|
#
|
64
|
# The GUI class is responsible for initializing the main window and setting up the control window. It provides
|
65
|
# a canvas for displaying images and buttons for loading and displaying images.
|
66
|
#
|
67
|
# \author
|
68
|
# \version 1.0
|
69
|
#
|
70
|
|
71
|
def __init__(self, master):
|
72
|
|
73
|
## \brief Constructor for the GUI class.
|
74
|
#
|
75
|
# This constructor initializes the main window and sets up the control window.
|
76
|
#
|
77
|
# \param master the parent window
|
78
|
# \param self the object pointer
|
79
|
|
80
|
self.master = master
|
81
|
|
82
|
self.master.title("Image Projector")
|
83
|
|
84
|
self.canvas = tk.Canvas(self.master, bg="black")
|
85
|
|
86
|
self.canvas.pack(fill=tk.BOTH, expand=True)
|
87
|
|
88
|
self.shared_data = SharedData()
|
89
|
|
90
|
self.commands = Commands(self, self.shared_data)
|
91
|
self.control_window()
|
92
|
|
93
|
def control_window(self):
|
94
|
# \brief Call the function to set up the control window.
|
95
|
#
|
96
|
# A seperate window which allows loading and setting up images and overlap region. It contains buttons and entry
|
97
|
# fields for loading images, setting the overlap region and control the display of images.
|
98
|
#
|
99
|
# \param self the object pointer
|
100
|
|
101
|
|
102
|
self.control_window = tk.Toplevel(self.master)
|
103
|
|
104
|
self.control_window.title("Controls")
|
105
|
|
106
|
self.overlap = tk.IntVar()
|
107
|
|
108
|
self.label = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
|
109
|
|
110
|
self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
|
111
|
|
112
|
self.label.pack()
|
113
|
self.overlapBox.pack()
|
114
|
|
115
|
self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
|
116
|
self.set_overlap_button.pack()
|
117
|
|
118
|
self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
|
119
|
self.button_load_left.pack()
|
120
|
|
121
|
self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
|
122
|
self.button_load_right.pack()
|
123
|
|
124
|
self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
|
125
|
self.button_show_left.pack()
|
126
|
|
127
|
self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
|
128
|
self.button_show_right.pack()
|
129
|
|
130
|
# \brief Main program entry point.
|
131
|
#
|
132
|
# This block initializes the tkinter root window, sets the window size and initializes the GUI class.
|
133
|
if __name__ == "__main__":
|
134
|
|
135
|
root = tk.Tk()
|
136
|
|
137
|
root.geometry("1280x800")
|
138
|
|
139
|
app = GUI(root)
|
140
|
|
141
|
root.mainloop()
|
142
|
|
143
|
|