Project

General

Profile

Files » ver3_2.py

adjusted - Md Tamjidur RAHMAN, 10/24/2024 04:11 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.alpha = 0
14

    
15
        self.leftSide = True
16

    
17
        # Methods to implement
18
        # Load config function
19
        # Write to config file 
20

    
21
class ImageIO:
22
    def __init__(self, shared_data):
23
        self.shared_data = shared_data
24

    
25
    def load_image(self, path):
26
        return cv.imread(path, cv.IMREAD_COLOR)
27

    
28
    def write_image(self, img, fileName="image.png"):
29
        if cv.imwrite(fileName, img):
30
            print(f"File {fileName} Written Successfully")
31
        else:
32
            print("File writing failed")
33

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

    
51

    
52

    
53
class Commands:
54
    def __init__(self, gui, shared_data):
55
        self.gui = gui
56
        self.shared_data = shared_data
57
        self.io = ImageIO(shared_data)
58

    
59
    def load_left_image(self):
60
        filepath = filedialog.askopenfilename(title="Select Left Image")
61
        if filepath:
62
            self.shared_data.left_image = self.io.load_image(filepath)
63
            print("Left image loaded.")
64

    
65
    def load_right_image(self):
66
        filepath = filedialog.askopenfilename(title="Select Right Image")
67
        if filepath:
68
            self.shared_data.right_image = self.io.load_image(filepath)
69
            print("Right image loaded.")
70

    
71
    def show_left_image(self):
72
        if self.shared_data.left_image is not None:
73
            self.shared_data.isLeft = True
74
            left = self.shared_data.left_image.copy()
75
            height = left.shape[0]
76

    
77
            left_overlap_start = left.shape[1] - self.shared_data.overlap_region
78

    
79
            # Create a gradient alpha mask for the left image (1 to 0)
80
            gradient_alpha = np.linspace(1, 0, self.shared_data.overlap_region).astype(np.float32)
81
            gradient_alpha = np.tile(gradient_alpha, (height, 1))  # Repeat to match height
82

    
83
            # Ensure the gradient is clipped to avoid any unexpected behavior
84
            gradient_alpha = np.clip(gradient_alpha, 0, 1)
85

    
86
            # Apply the alpha gradient to the overlapping region
87
            left_overlap = left[:, left_overlap_start:, :].copy()
88
            left_overlap[:, :, :3] = cv.convertScaleAbs(left_overlap[:, :, :3], alpha=1)  # Keep original brightness
89
            left_overlap = (left_overlap * gradient_alpha[..., np.newaxis]).astype(np.uint8)
90

    
91
            # Replace the modified overlapping region back into the original left image
92
            left[:, left_overlap_start:, :] = left_overlap
93

    
94
            # Show the modified image
95
            self.io.show_image(left, canvas=self.gui.canvas)
96
            print("Left image displayed.")
97
        else:
98
            messagebox.showerror("Error", "No left image loaded!")
99

    
100
    def show_right_image(self):
101
        if self.shared_data.right_image is not None:
102
            self.shared_data.isLeft = False
103
            right = self.shared_data.right_image.copy()
104
            height = right.shape[0]
105

    
106
            right_overlap_end = self.shared_data.overlap_region
107

    
108
            # Create a gradient alpha mask for the right image (0 to 1)
109
            gradient_alpha = np.linspace(0, 1, right_overlap_end * self.shared_data.alpha).astype(np.float32)
110
            gradient_alpha = np.tile(gradient_alpha, (height, 1))  # Repeat to match height
111

    
112
            # Ensure the gradient is clipped to avoid any unexpected behavior
113
            gradient_alpha = np.clip(gradient_alpha, 0, 1)
114

    
115
            # Apply the alpha gradient to the overlapping region
116
            right_overlap = right[:, :right_overlap_end, :].copy()
117
            right_overlap[:, :, :3] = cv.convertScaleAbs(right_overlap[:, :, :3], alpha=1)  # Keep original brightness
118
            right_overlap = (right_overlap * gradient_alpha[..., np.newaxis]).astype(np.uint8)
119

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

    
123
            # Show the modified image
124
            self.io.show_image(right, canvas=self.gui.canvas)
125
            print("Right image displayed.")
126
        else:
127
            messagebox.showerror("Error", "No right image loaded!")
128

    
129
    def set_overlap_region(self):
130
        self.shared_data.overlap_region = self.gui.overlap.get()
131
        print(f"Overlap region set to {self.shared_data.overlap_region}")
132

    
133
    def set_alpha_value(self):
134
        alpha = self.gui.alpha.get()
135
        if(alpha > 1.0):
136
            alpha = 1
137
            
138
        if(alpha < 0.0):
139
            alpha = 0
140

    
141
        self.shared_data.alpha = alpha
142
        print(f"Alpha value set to {self.shared_data.alpha}")
143

    
144

    
145

    
146
class GUI:
147
    def __init__(self, master):
148
        self.master = master
149
        self.master.title("Image Projector")
150
        self.canvas = tk.Canvas(self.master, bg="black")
151
        self.canvas.pack(fill=tk.BOTH, expand=True)
152
        self.shared_data = SharedData()
153
        self.commands = Commands(self, self.shared_data)
154
        self.control_window()
155

    
156
    def control_window(self):
157
        self.control_window = tk.Toplevel(self.master)
158
        self.control_window.title("Controls")
159
        
160
        self.overlap = tk.IntVar(value=200)
161
        self.alpha = tk.DoubleVar(value=0.5)
162

    
163
        self.alphaLabel = tk.Label(self.control_window, text='Alpha value', font=('calibre', 10, 'bold'))
164
        self.alphaBox = tk.Entry(self.control_window, textvariable=self.alpha)
165
        self.alphaLabel.pack()
166
        self.alphaBox.pack()
167

    
168
        self.set_alpha_button = tk.Button(self.control_window, text="Set Alpha", command=self.commands.set_alpha_value)
169
        self.set_alpha_button.pack()
170

    
171
        self.overlapLabel = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
172
        self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
173
        self.overlapLabel.pack()
174
        self.overlapBox.pack()
175

    
176

    
177
        self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
178
        self.set_overlap_button.pack()
179

    
180
        self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
181
        self.button_load_left.pack()
182

    
183
        self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
184
        self.button_load_right.pack()
185

    
186
        self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
187
        self.button_show_left.pack()
188

    
189
        self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
190
        self.button_show_right.pack()
191

    
192
        
193

    
194
if __name__ == "__main__":
195
    root = tk.Tk()
196
    root.geometry("1280x800")
197
    app = GUI(root)
198
    root.mainloop()
(13-13/49)