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
|
|
8
|
class SharedData:
|
9
|
def __init__(self):
|
10
|
self.left_image = None
|
11
|
self.right_image = None
|
12
|
self.overlap_region = 200
|
13
|
self.alpha = 0.5
|
14
|
|
15
|
self.leftSide = True
|
16
|
|
17
|
# Methods to implement
|
18
|
# Load config function
|
19
|
# Write to config file
|
20
|
##
|
21
|
# @class This class is in charge of imput and output
|
22
|
#
|
23
|
class ImageIO:
|
24
|
##
|
25
|
# @breif
|
26
|
# initializator for whole method.
|
27
|
# get parsed config.cfg data from the class 'SharedData'
|
28
|
#
|
29
|
# @param self the class itself
|
30
|
# @param shared_data parsed config.cfg data
|
31
|
# @return None
|
32
|
def __init__(self, shared_data):
|
33
|
self.shared_data = shared_data
|
34
|
|
35
|
##
|
36
|
# @breif function for loading image
|
37
|
# @param path path of the image
|
38
|
# @return numpy array, image
|
39
|
def load_image(self, path):
|
40
|
return cv.imread(path, cv.IMREAD_COLOR)
|
41
|
|
42
|
##
|
43
|
# @brief function to manipulate the image
|
44
|
# @param img, loaded image, numpy matrix
|
45
|
# @param fileName, name of the image. "image.png" will be the default value if there are no input
|
46
|
# @return None
|
47
|
def write_image(self, img, fileName="image.png"):
|
48
|
if cv.imwrite(fileName, img):
|
49
|
print(f"File {fileName} Written Successfully")
|
50
|
else:
|
51
|
print("File writing failed")
|
52
|
|
53
|
##
|
54
|
# @breif function to print image through the projector
|
55
|
# @param img numpy array, image matrix
|
56
|
# @param title title of the window. 'Image Title' will be the default value when there is no input.
|
57
|
# @param canvas parameter needed for library 'Tinker Canvas'. None is default value.
|
58
|
# @return None
|
59
|
def show_image(self, img, title="Image Title", canvas=None):
|
60
|
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
|
61
|
img_pil = Image.fromarray(img_rgb)
|
62
|
img_tk = ImageTk.PhotoImage(img_pil)
|
63
|
|
64
|
canvas.update_idletasks() # Force the canvas to update its dimensions
|
65
|
canvas_width = canvas.winfo_width() # Get the updated canvas width
|
66
|
|
67
|
if self.shared_data.isLeft:
|
68
|
# Align image to the top-right corner
|
69
|
canvas.create_image(canvas_width, 0, anchor=tk.NE, image=img_tk)
|
70
|
else:
|
71
|
# Align image to the top-left corner
|
72
|
canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
|
73
|
|
74
|
canvas.image = img_tk # Prevent image from being garbage collected
|
75
|
|
76
|
|
77
|
|
78
|
class Commands:
|
79
|
def __init__(self, gui, shared_data):
|
80
|
self.gui = gui
|
81
|
self.shared_data = shared_data
|
82
|
self.io = ImageIO(shared_data)
|
83
|
|
84
|
def load_left_image(self):
|
85
|
filepath = filedialog.askopenfilename(title="Select Left Image")
|
86
|
if filepath:
|
87
|
self.shared_data.left_image = self.io.load_image(filepath)
|
88
|
print("Left image loaded.")
|
89
|
|
90
|
def load_right_image(self):
|
91
|
filepath = filedialog.askopenfilename(title="Select Right Image")
|
92
|
if filepath:
|
93
|
self.shared_data.right_image = self.io.load_image(filepath)
|
94
|
print("Right image loaded.")
|
95
|
|
96
|
def show_left_image(self):
|
97
|
if self.shared_data.left_image is not None:
|
98
|
self.shared_data.isLeft = True
|
99
|
# Create a copy of the original left image
|
100
|
left = self.shared_data.left_image.copy()
|
101
|
|
102
|
# Assuming both images have the same height
|
103
|
height = left.shape[0]
|
104
|
|
105
|
left_overlap_start = left.shape[1]-self.shared_data.overlap_region
|
106
|
|
107
|
# Extract the overlapping region
|
108
|
left_overlap = left[0:height, left_overlap_start:, :]
|
109
|
|
110
|
# Adjust the alpha value
|
111
|
left_overlap_mod = cv.convertScaleAbs(left_overlap, alpha=self.shared_data.alpha, beta=0) # Adjust brightness
|
112
|
|
113
|
# Replace the modified blue channel back into the original right image
|
114
|
left[0:height, left_overlap_start:, :] = left_overlap_mod
|
115
|
|
116
|
# Show the modified image
|
117
|
self.io.show_image(left, canvas=self.gui.canvas)
|
118
|
print("Left image displayed.")
|
119
|
else:
|
120
|
messagebox.showerror("Error", "No left image loaded!")
|
121
|
|
122
|
def show_right_image(self):
|
123
|
if self.shared_data.right_image is not None:
|
124
|
|
125
|
self.shared_data.isLeft = False
|
126
|
# Create a copy of the original right image
|
127
|
right = self.shared_data.right_image.copy()
|
128
|
|
129
|
# Assuming both images have the same height
|
130
|
height = right.shape[0]
|
131
|
|
132
|
right_overlap_start = 0
|
133
|
right_overlap_end = self.shared_data.overlap_region
|
134
|
|
135
|
# Extract the overlapping region
|
136
|
right_overlap = right[0:height, right_overlap_start:right_overlap_end, :]
|
137
|
|
138
|
# Adjust the alpha value
|
139
|
right_overlap_mod = cv.convertScaleAbs(right_overlap, alpha=self.shared_data.alpha, beta=0) # Adjust brightness
|
140
|
|
141
|
# Replace the modified blue channel back into the original right image
|
142
|
right[0:height, right_overlap_start:right_overlap_end, :] = right_overlap_mod
|
143
|
|
144
|
# Show the modified image
|
145
|
self.io.show_image(right, canvas=self.gui.canvas)
|
146
|
print("Right image displayed.")
|
147
|
else:
|
148
|
messagebox.showerror("Error", "No right image loaded!")
|
149
|
|
150
|
|
151
|
def set_overlap_region(self):
|
152
|
self.shared_data.overlap_region = self.gui.overlap.get()
|
153
|
print(f"Overlap region set to {self.shared_data.overlap_region}")
|
154
|
|
155
|
def set_alpha_value(self):
|
156
|
alpha = self.gui.alpha.get()
|
157
|
if(alpha > 1.0):
|
158
|
alpha = 1
|
159
|
|
160
|
if(alpha < 0.0):
|
161
|
alpha = 0
|
162
|
|
163
|
self.shared_data.alpha = alpha
|
164
|
print(f"Alpha value set to {self.shared_data.alpha}")
|
165
|
|
166
|
|
167
|
|
168
|
class GUI:
|
169
|
def __init__(self, master):
|
170
|
self.master = master
|
171
|
self.master.title("Image Projector")
|
172
|
self.canvas = tk.Canvas(self.master, bg="black")
|
173
|
self.canvas.pack(fill=tk.BOTH, expand=True)
|
174
|
self.shared_data = SharedData()
|
175
|
self.commands = Commands(self, self.shared_data)
|
176
|
self.control_window()
|
177
|
|
178
|
def control_window(self):
|
179
|
self.control_window = tk.Toplevel(self.master)
|
180
|
self.control_window.title("Controls")
|
181
|
|
182
|
self.overlap = tk.IntVar(value=200)
|
183
|
self.alpha = tk.DoubleVar(value=0.5)
|
184
|
|
185
|
self.alphaLabel = tk.Label(self.control_window, text='Alpha value', font=('calibre', 10, 'bold'))
|
186
|
self.alphaBox = tk.Entry(self.control_window, textvariable=self.alpha)
|
187
|
self.alphaLabel.pack()
|
188
|
self.alphaBox.pack()
|
189
|
|
190
|
self.set_alpha_button = tk.Button(self.control_window, text="Set Alpha", command=self.commands.set_alpha_value)
|
191
|
self.set_alpha_button.pack()
|
192
|
|
193
|
self.overlapLabel = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
|
194
|
self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
|
195
|
self.overlapLabel.pack()
|
196
|
self.overlapBox.pack()
|
197
|
|
198
|
|
199
|
self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
|
200
|
self.set_overlap_button.pack()
|
201
|
|
202
|
self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
|
203
|
self.button_load_left.pack()
|
204
|
|
205
|
self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
|
206
|
self.button_load_right.pack()
|
207
|
|
208
|
self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
|
209
|
self.button_show_left.pack()
|
210
|
|
211
|
self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
|
212
|
self.button_show_right.pack()
|
213
|
|
214
|
|
215
|
|
216
|
if __name__ == "__main__":
|
217
|
root = tk.Tk()
|
218
|
root.geometry("1280x800")
|
219
|
app = GUI(root)
|
220
|
root.mainloop()
|