Project

General

Profile

Files » ver3_4.py

Final version, probably - Md Tamjidur RAHMAN, 10/31/2024 01:26 PM

 
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
import configparser
8

    
9
class SharedData:
10
    def __init__(self):
11
        self.left_image = None
12
        self.right_image = None
13
        self.overlap_region = 200
14
        self.leftSide = True
15

    
16
    def write_to_config(self, filename='config.ini'):
17
        config = configparser.ConfigParser()
18
        config['DEFAULT'] = {
19
            'overlap_region': str(self.overlap_region),
20
            'leftSide': str(self.leftSide)
21
        }
22
        with open(filename, 'w') as configfile:
23
            config.write(configfile)
24

    
25
    def read_from_config(self, filename='config.ini'):
26
        config = configparser.ConfigParser()
27
        config.read(filename)
28
        self.overlap_region = config['DEFAULT'].getint('overlap_region', self.overlap_region)
29
        self.leftSide = config['DEFAULT'].getboolean('leftSide', self.leftSide)
30

    
31
class ImageIO:
32
    def __init__(self, shared_data):
33
        self.shared_data = shared_data
34

    
35
    def load_image(self, path):
36
        return cv.imread(path, cv.IMREAD_COLOR)
37

    
38
    def write_image(self, img, fileName="image.png"):
39
        if cv.imwrite(fileName, img):
40
            print(f"File {fileName} Written Successfully")
41
        else:
42
            print("File writing failed")
43

    
44
    def show_image(self, img, title="Image Title", canvas=None):
45
        img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
46
        img_pil = Image.fromarray(img_rgb)
47
        img_tk = ImageTk.PhotoImage(img_pil)
48
        
49
        canvas.update_idletasks()  # Force the canvas to update its dimensions
50
        canvas_width = canvas.winfo_width()  # Get the updated canvas width
51
        
52
        if self.shared_data.isLeft:
53
            # Align image to the top-right corner
54
            canvas.create_image(canvas_width, 0, anchor=tk.NE, image=img_tk)
55
        else:
56
            # Align image to the top-left corner
57
            canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
58
        
59
        canvas.image = img_tk  # Prevent image from being garbage collected
60

    
61

    
62

    
63
class Commands:
64
    def __init__(self, gui, shared_data):
65
        self.gui = gui
66
        self.shared_data = shared_data
67
        self.io = ImageIO(shared_data)
68

    
69
    def load_left_image(self):
70
        filepath = filedialog.askopenfilename(title="Select Left Image")
71
        if filepath:
72
            self.shared_data.left_image = self.io.load_image(filepath)
73
            print("Left image loaded.")
74

    
75
    def load_right_image(self):
76
        filepath = filedialog.askopenfilename(title="Select Right Image")
77
        if filepath:
78
            self.shared_data.right_image = self.io.load_image(filepath)
79
            print("Right image loaded.")
80

    
81
    def smooth_gradient(self,length, rate):
82
        # Create a linear gradient that transitions from full brightness (1) to zero (0)
83
        x = np.linspace(0, 1, length)
84
        # Apply a non-linear function (e.g., quadratic) for a smoother transition
85
        gradient = 1 - (x ** 2)  # Quadratic falloff to create a smooth transition
86
        gradient = np.clip(gradient * rate, 0, 1)  # Scale the gradient by the rate
87
        return gradient
88

    
89
    def show_left_image(self):
90
        if self.shared_data.left_image is not None:
91
            self.shared_data.isLeft = True
92
            left = self.shared_data.left_image.copy()
93
            height = left.shape[0]
94
            left_overlap_start = left.shape[1] - self.shared_data.overlap_region
95
            
96
            # Create a smooth gradient alpha mask for the left image (1 to 0)
97
            gradient_alpha = np.linspace(1, 0, self.shared_data.overlap_region)
98
            gradient_alpha = np.tile(gradient_alpha, (height, 1))
99

    
100
            # Apply the alpha gradient to the overlapping region
101
            left_overlap = left[:, left_overlap_start:, :].copy()
102
            left_overlap = (left_overlap * gradient_alpha[:, :, np.newaxis]).astype(np.uint8)
103

    
104
            # Replace the modified overlapping region back into the original left image
105
            left[:, left_overlap_start:, :] = left_overlap
106

    
107
            # Show the modified image
108
            self.io.show_image(left, canvas=self.gui.canvas)
109
            print("Left image displayed.")
110
        else:
111
            messagebox.showerror("Error", "No left image loaded!")
112

    
113
    def show_right_image(self):
114
        if self.shared_data.right_image is not None:
115
            self.shared_data.isLeft = False
116
            right = self.shared_data.right_image.copy()
117
            height = right.shape[0]
118
            right_overlap_end = self.shared_data.overlap_region
119
            
120
            # Create a smooth gradient alpha mask for the right image (0 to 1)
121
            gradient_alpha = np.linspace(0, 1, right_overlap_end)
122
            gradient_alpha = np.tile(gradient_alpha, (height, 1))
123

    
124
            # Apply the alpha gradient to the overlapping region
125
            right_overlap = right[:, :right_overlap_end, :].copy()
126
            right_overlap = (right_overlap * gradient_alpha[:, :, np.newaxis]).astype(np.uint8)
127

    
128
            # Replace the modified overlapping region back into the original right image
129
            right[:, :right_overlap_end, :] = right_overlap
130

    
131
            # Show the modified image
132
            self.io.show_image(right, canvas=self.gui.canvas)
133
            print("Right image displayed.")
134
        else:
135
            messagebox.showerror("Error", "No right image loaded!")
136
        
137
    def show_right_original(self):
138
        # Show original image
139
        if self.shared_data.right_image is not None:
140
            self.shared_data.isLeft = False
141
            right = self.shared_data.right_image.copy()
142
            self.io.show_image(right, canvas=self.gui.canvas)
143
            print("Right image displayed.")
144
        else:
145
            messagebox.showerror("Error", "No right image loaded!")
146

    
147
    def show_left_original(self):
148
        # Show original image
149
        if self.shared_data.left_image is not None:
150
            self.shared_data.isLeft = True
151
            left = self.shared_data.left_image.copy()
152
            self.io.show_image(left, canvas=self.gui.canvas)
153
            print("Left image displayed.")
154
        else:
155
            messagebox.showerror("Error", "No left image loaded!")
156

    
157

    
158
    def set_overlap_region(self):
159
        self.shared_data.overlap_region = self.gui.overlap.get()
160
        print(f"Overlap region set to {self.shared_data.overlap_region}")
161
        if(self.shared_data.isLeft):
162
            self.show_left_image()
163
        else:
164
            self.show_right_image()
165

    
166
    def increase_overlap(self):
167
        current_overlap = self.gui.overlap.get()
168
        self.gui.overlap.set(current_overlap + 1)
169
        self.set_overlap_region()
170
        if(self.shared_data.isLeft):
171
            self.show_left_image()
172
        else:
173
            self.show_right_image()
174
        
175
        
176
        
177

    
178
    def decrease_overlap(self):
179
        current_overlap = self.gui.overlap.get()
180
        self.gui.overlap.set(max(0, current_overlap - 1))  # Prevents negative overlap
181
        self.set_overlap_region()
182
        if(self.shared_data.isLeft):
183
            self.show_left_image()
184
        else:
185
            self.show_right_image()
186

    
187

    
188

    
189
class GUI:
190
    def __init__(self, master):
191
        self.master = master
192
        self.master.title("Image Projector")
193
        self.canvas = tk.Canvas(self.master, bg="black")
194
        self.canvas.pack(fill=tk.BOTH, expand=True)
195
        self.shared_data = SharedData()
196
        self.commands = Commands(self, self.shared_data)
197
        self.control_window()
198

    
199
    def control_window(self):
200
        self.control_window = tk.Toplevel(self.master)
201
        self.control_window.title("Controls")
202
        
203
        self.overlap = tk.IntVar(value=200)
204

    
205
        # Label and entry for overlap region width
206
        self.overlapLabel = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
207
        self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
208
        
209
        self.overlapLabel.grid(row=0, column=0, padx=5, pady=5)
210
        self.overlapBox.grid(row=0, column=1, padx=5, pady=5)
211

    
212
        # Button to set overlap region
213
        self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
214
        self.set_overlap_button.grid(row=1, column=0, columnspan=2, padx=5, pady=5)
215

    
216
        # New buttons for increasing and decreasing the overlap region
217
        self.increase_overlap_button = tk.Button(self.control_window, text="Increase Overlap", command=self.commands.increase_overlap)
218
        self.increase_overlap_button.grid(row=2, column=0, padx=5, pady=5)
219

    
220
        self.decrease_overlap_button = tk.Button(self.control_window, text="Decrease Overlap", command=self.commands.decrease_overlap)
221
        self.decrease_overlap_button.grid(row=2, column=1, padx=5, pady=5)
222

    
223
        # Load left and right image buttons
224
        self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
225
        self.button_load_left.grid(row=3, column=0, padx=5, pady=5)
226

    
227
        self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
228
        self.button_load_right.grid(row=3, column=1, padx=5, pady=5)
229

    
230
        # Show left and right image buttons
231
        self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
232
        self.button_show_left.grid(row=4, column=0, padx=5, pady=5)
233

    
234
        self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
235
        self.button_show_right.grid(row=4, column=1, padx=5, pady=5)
236

    
237
        # Show original left and right image buttons
238
        self.button_show_left_original = tk.Button(self.control_window, text="Show Left Original", command=self.commands.show_left_original)
239
        self.button_show_left_original.grid(row=5, column=0, padx=5, pady=5)
240

    
241
        self.button_show_right_original = tk.Button(self.control_window, text="Show Right Original", command=self.commands.show_right_original)
242
        self.button_show_right_original.grid(row=5, column=1, padx=5, pady=5)
243

    
244
        self.save_config = tk.Button(self.control_window, text="Save Config", command=self.shared_data.write_to_config)
245
        self.save_config.grid(row=6, column=0, padx=5, pady=5)
246

    
247
        self.load_config = tk.Button(self.control_window, text="Load Config", command=self.shared_data.read_from_config)
248
        self.load_config.grid(row=6, column=1, padx=5, pady=5)
249

    
250

    
251
        
252

    
253
if __name__ == "__main__":
254
    root = tk.Tk()
255
    root.geometry("1280x800")
256
    app = GUI(root)
257
    root.mainloop()
(23-23/49)