Project

General

Profile

Files » ver3_3.py

functional version with gradient function - Md Tamjidur RAHMAN, 10/25/2024 08:52 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

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

    
15
        #TBD
16
    def write_to_file(self):
17
        pass
18

    
19
    def load_from_file(self):
20
        pass
21
        # Methods to implement
22
        # Load config function
23
        # Write to config file 
24

    
25
class ImageIO:
26
    def __init__(self, shared_data):
27
        self.shared_data = shared_data
28

    
29
    def load_image(self, path):
30
        return cv.imread(path, cv.IMREAD_COLOR)
31

    
32
    def write_image(self, img, fileName="image.png"):
33
        if cv.imwrite(fileName, img):
34
            print(f"File {fileName} Written Successfully")
35
        else:
36
            print("File writing failed")
37

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

    
55

    
56

    
57
class Commands:
58
    def __init__(self, gui, shared_data):
59
        self.gui = gui
60
        self.shared_data = shared_data
61
        self.io = ImageIO(shared_data)
62

    
63
    def load_left_image(self):
64
        filepath = filedialog.askopenfilename(title="Select Left Image")
65
        if filepath:
66
            self.shared_data.left_image = self.io.load_image(filepath)
67
            print("Left image loaded.")
68

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

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

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

    
94
            # Apply the alpha gradient to the overlapping region
95
            left_overlap = left[:, left_overlap_start:, :].copy()
96
            left_overlap = (left_overlap * gradient_alpha[:, :, np.newaxis]).astype(np.uint8)
97

    
98
            # Replace the modified overlapping region back into the original left image
99
            left[:, left_overlap_start:, :] = left_overlap
100

    
101
            # Show the modified image
102
            self.io.show_image(left, canvas=self.gui.canvas)
103
            print("Left image displayed.")
104
        else:
105
            messagebox.showerror("Error", "No left image loaded!")
106

    
107
    def show_right_image(self):
108
        if self.shared_data.right_image is not None:
109
            self.shared_data.isLeft = False
110
            right = self.shared_data.right_image.copy()
111
            height = right.shape[0]
112
            right_overlap_end = self.shared_data.overlap_region
113
            
114
            # Create a smooth gradient alpha mask for the right image (0 to 1)
115
            gradient_alpha = np.linspace(0, 1, right_overlap_end)
116
            gradient_alpha = np.tile(gradient_alpha, (height, 1))
117

    
118
            # Apply the alpha gradient to the overlapping region
119
            right_overlap = right[:, :right_overlap_end, :].copy()
120
            right_overlap = (right_overlap * gradient_alpha[:, :, np.newaxis]).astype(np.uint8)
121

    
122
            # Replace the modified overlapping region back into the original right image
123
            right[:, :right_overlap_end, :] = right_overlap
124

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

    
141
    def show_left_original(self):
142
        # Show original image
143
        if self.shared_data.left_image is not None:
144
            self.shared_data.isLeft = True
145
            left = self.shared_data.left_image.copy()
146
            self.io.show_image(left, canvas=self.gui.canvas)
147
            print("Left image displayed.")
148
        else:
149
            messagebox.showerror("Error", "No left image loaded!")
150

    
151

    
152
    def set_overlap_region(self):
153
        self.shared_data.overlap_region = self.gui.overlap.get()
154
        print(f"Overlap region set to {self.shared_data.overlap_region}")
155

    
156

    
157

    
158

    
159
class GUI:
160
    def __init__(self, master):
161
        self.master = master
162
        self.master.title("Image Projector")
163
        self.canvas = tk.Canvas(self.master, bg="black")
164
        self.canvas.pack(fill=tk.BOTH, expand=True)
165
        self.shared_data = SharedData()
166
        self.commands = Commands(self, self.shared_data)
167
        self.control_window()
168

    
169
    def control_window(self):
170
        self.control_window = tk.Toplevel(self.master)
171
        self.control_window.title("Controls")
172
        
173
        self.overlap = tk.IntVar(value=200)
174

    
175
        self.overlapLabel = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
176
        self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
177
        self.overlapLabel.pack()
178
        self.overlapBox.pack()
179

    
180

    
181
        self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
182
        self.set_overlap_button.pack()
183

    
184
        self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
185
        self.button_load_left.pack()
186

    
187
        self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
188
        self.button_load_right.pack()
189

    
190
        self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
191
        self.button_show_left.pack()
192

    
193
        self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
194
        self.button_show_right.pack()
195

    
196
        self.button_show_left_original = tk.Button(self.control_window, text="Show Left Original", command=self.commands.show_left_original)
197
        self.button_show_left_original.pack()
198

    
199
        self.button_show_right_original = tk.Button(self.control_window, text="Show Right Original", command=self.commands.show_right_original)
200
        self.button_show_right_original.pack()
201

    
202

    
203
        
204

    
205
if __name__ == "__main__":
206
    root = tk.Tk()
207
    root.geometry("1280x800")
208
    app = GUI(root)
209
    root.mainloop()
(18-18/49)