Project

General

Profile

Coding » History » Version 10

Ayato KOTSUGI , 01/18/2024 02:33 PM

1 1 Satya Sai Abhiram OGGU
2 5 Satya Sai Abhiram OGGU
h1=. <pre>
3
Coding</pre>
4
5
---
6
7 7 Satya Sai Abhiram OGGU
*[[Wiki]]*   |  *[[About Us]]*  |  *[[Regarding Our Project]]* | *[[Coding]]*  |  *[[UML Diagrams]]*  |  *[[Results]]*  |
8
9 5 Satya Sai Abhiram OGGU
10
---
11 6 Satya Sai Abhiram OGGU
12 4 Ayato KOTSUGI
h2. main.py: creates objects and variables necessary for the another file.
13 2 Ayato KOTSUGI
<pre><code class="python">
14
#!/usr/bin/env python3
15 9 Ayato KOTSUGI
#-*- coding: utf-8 -*-
16
17 8 Ayato KOTSUGI
from MaskCreator import MaskCreator
18 2 Ayato KOTSUGI
from ConfigReader import ConfigReader
19
from MainDisplay import MainDisplay
20
21 9 Ayato KOTSUGI
22 2 Ayato KOTSUGI
# creating object for MainDisplay, ConfigReader classes
23
24
# change to config_left.ini or config_right.ini when necessary
25
CReader = ConfigReader("config_left.ini")
26
# CReader = ConfigReader("config_right.ini")
27
MDisplay = MainDisplay()
28
29
# initializing variables
30 1 Satya Sai Abhiram OGGU
mask_width = CReader.getProjectedOverlapWidth()
31 2 Ayato KOTSUGI
image_width = CReader.getProjectedImageWidth()
32
gamma = CReader.getGamma()
33
image_side = CReader.getImageSide()
34
image_path = CReader.getImageName()
35
36
if image_side == 0:
37
    image_name = 'left'
38
elif image_side == 1:
39
    image_name = 'right'
40
else:
41
    print("Invalid ImageSide value in config.ini. Use 0 for left image, 1 for right image.")
42
43
# loading image
44
image = MDisplay.readImage(image_path)
45
result_image = MDisplay.setImage(image)
46
47 9 Ayato KOTSUGI
48 2 Ayato KOTSUGI
if image is not None:
49
    # creating object for MaskCreator class
50
    MCreator = MaskCreator(image)
51
    
52 1 Satya Sai Abhiram OGGU
    # image modification
53
    MCreator.create_mask(image_side, mask_width, image_width)
54 2 Ayato KOTSUGI
    MCreator.gammaCorrection(gamma)
55 1 Satya Sai Abhiram OGGU
    MCreator.result_image = result_image
56
    MCreator.alpha_blending(image_side)
57
    MCreator.mod_intensity(image_side)
58 2 Ayato KOTSUGI
    
59 1 Satya Sai Abhiram OGGU
    # saving image
60
    MDisplay.saveImage()
61
else:
62
    print(f"Failed to read the image (ImageSide={image_side}).")
63
64 4 Ayato KOTSUGI
</code></pre>
65 1 Satya Sai Abhiram OGGU
66
h2. MaskCreator.py: creates objects and variables necessary for the other file.
67
<pre><code class="python">
68
69 9 Ayato KOTSUGI
import cv2
70
import numpy as np
71 1 Satya Sai Abhiram OGGU
72 9 Ayato KOTSUGI
class MaskCreator:
73
    """
74
    @brief The MaskCreator class performs image modification, including creating masks, gamma correction, alpha blending, and intensity modification.
75 1 Satya Sai Abhiram OGGU
76 9 Ayato KOTSUGI
    @details
77
    The class includes methods for:
78
    - Creating masks based on specified parameters.
79
    - Applying gamma correction to the image.
80
    - Performing alpha blending on the image based on the image side.
81
    - Modifying image intensity based on the image side.
82 1 Satya Sai Abhiram OGGU
83 9 Ayato KOTSUGI
    @note
84
    Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class.
85 1 Satya Sai Abhiram OGGU
86 9 Ayato KOTSUGI
    @date January 18, 2024
87
    """
88 1 Satya Sai Abhiram OGGU
89 9 Ayato KOTSUGI
    def __init__(self, image):
90
        """
91
        @brief Initializes a MaskCreator object.
92 1 Satya Sai Abhiram OGGU
93 9 Ayato KOTSUGI
        @param image: The input image on which modifications will be applied.
94
        """
95
        self.__image = image
96
        self.__alpha_gradient = None
97
        self.__gamma_corrected = None
98
        self.result_image = None
99
        self.__mask = None
100
        
101
    def create_mask(self, image_side, mask_width, image_width):
102
        """
103
        @brief Creates a mask based on specified parameters.
104 8 Ayato KOTSUGI
105 9 Ayato KOTSUGI
        @param image_side: The side of the image (0 for left, 1 for right).
106
        @param mask_width: The width of the mask.
107
        @param image_width: The width of the input image.
108
        """
109
        self.__mask = self.__image.shape[1] * mask_width // image_width
110
        if image_side == 1:
111
            self.__alpha_gradient = np.linspace(1, 0, self.__mask)
112
        elif image_side == 0:
113
            self.__alpha_gradient = np.linspace(0, 1, self.__mask)
114 8 Ayato KOTSUGI
115 9 Ayato KOTSUGI
    def gammaCorrection(self, gamma):
116
        """
117
        @brief Applies gamma correction to the image.
118
119
        @param gamma: The gamma value for correction.
120
        """
121
        self.__gamma_corrected = np.uint8(cv2.pow(self.__image / 255.0, gamma) * 255)
122
        
123
    def alpha_blending(self, image_side):
124
        """
125
        @brief Performs alpha blending on the image based on the image side.
126
127
        @param image_side: The side of the image (0 for left, 1 for right).
128
        """
129
        if image_side == 1:
130
            for col in range(self.__mask):
131
                alpha = self.__alpha_gradient[-self.__mask + col]
132
                self.result_image[:, col] = alpha * self.__gamma_corrected[:, col] + (1 - alpha) * self.result_image[:, col]
133
        elif image_side == 0:
134
            for col in range(self.__mask):
135
                alpha = self.__alpha_gradient[-self.__mask + col]
136
                self.result_image[:, -self.__mask + col] = alpha * self.__gamma_corrected[:, -self.__mask + col] + (1 - alpha) * self.result_image[:, -self.__mask + col]
137
        
138
    def mod_intensity(self, image_side):
139
        """
140
        @brief Modifies image intensity based on the image side.
141
142
        @param image_side: The side of the image (0 for left, 1 for right).
143
        """
144
        if image_side == 1:
145
            for col in range(self.__mask):
146
                intensity_factor = 1.0 - (self.__mask - col) / self.__mask
147
                self.result_image[:, col] = (self.result_image[:, col] * intensity_factor).astype(np.uint8)
148
        elif image_side == 0:
149
            for col in range(self.__mask):
150
                intensity_factor = 1.0 - col / self.__mask
151
                self.result_image[:, -self.__mask + col] = (self.result_image[:, -self.__mask + col] * intensity_factor).astype(np.uint8)
152
153 4 Ayato KOTSUGI
</code></pre>
154
155
156 10 Ayato KOTSUGI
h2. ConfigReader.py:  For reading file's configuration parameters
157
158
<pre><code class="python">
159
#!/usr/bin/env python3
160
# -*- coding: utf-8 -*-
161
"""
162
@file
163
@brief This script defines the MainDisplay class, which handles reading, setting, and displaying images using the OpenCV library.
164
165
@date January 18, 2024
166
"""
167
168
import cv2
169
170
class MainDisplay:
171
    """
172
    @brief The MainDisplay class provides functionalities for image handling and display.
173
174
    @details
175
    The class includes methods for:
176
    - Reading an image from a specified path.
177
    - Setting the result image.
178
    - Displaying the result image using the OpenCV library.
179
180
    @note
181
    Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class.
182
183
    @date January 18, 2024
184
    """
185
186
    def __init__(self):
187
        """
188
        @brief Initializes a MainDisplay object.
189
190
        @details
191
        The result_image attribute is initialized to None.
192
        """
193
        self.result_image = None
194
195
    def readImage(self, image_path):
196
        """
197
        @brief Reads an image from the specified path.
198
199
        @param image_path: The path to the image file.
200
201
        @return The read image.
202
        """
203
        image = cv2.imread(image_path)
204
        return image
205
206
    def setImage(self, image):
207
        """
208
        @brief Sets the result image.
209
210
        @param image: The image to set as the result image.
211
212
        @return The set result image.
213
        """
214
        self.result_image = image
215
        return self.result_image
216
217
    def saveImage(self):
218
        """
219
        @brief Displays and saves the result image.
220
221
        @details
222
        The result image is displayed in a window with the window name indicating the displayed image.
223
        The window remains open until a key is pressed. Then, it is closed, and the program continues.
224
225
        @note
226
        Ensure that the OpenCV window is closed before proceeding with the execution of other code.
227
228
        @warning
229
        This method uses blocking code (cv2.waitKey(0)) that might lead to issues in certain scenarios.
230
        """
231
        # display image. (window name, image to display)
232
        cv2.imshow(f"Displaying {self.result_image}", self.result_image)
233
        cv2.waitKey(0)
234
        cv2.destroyAllWindows()
235
236
</code></pre>
237
238 4 Ayato KOTSUGI
h2. mainDisplay.py:  Displays the result
239 2 Ayato KOTSUGI
240 8 Ayato KOTSUGI
<pre><code class="python">
241
#!/usr/bin/env python3
242
# -*- coding: utf-8 -*-
243
"""
244
@file
245 2 Ayato KOTSUGI
@brief This script defines the MainDisplay class, which handles reading, setting, and displaying images using the OpenCV library.
246 8 Ayato KOTSUGI
247
@date January 18, 2024
248
"""
249 2 Ayato KOTSUGI
250
import cv2
251 4 Ayato KOTSUGI
252 8 Ayato KOTSUGI
class MainDisplay:
253
    """
254 2 Ayato KOTSUGI
    @brief The MainDisplay class provides functionalities for image handling and display.
255 8 Ayato KOTSUGI
256
    @details
257
    The class includes methods for:
258
    - Reading an image from a specified path.
259
    - Setting the result image.
260
    - Displaying the result image using the OpenCV library.
261
262
    @note
263
    Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class.
264
265
    @date January 18, 2024
266
    """
267 2 Ayato KOTSUGI
268 8 Ayato KOTSUGI
    def __init__(self):
269
        """
270
        @brief Initializes a MainDisplay object.
271
272
        @details
273
        The result_image attribute is initialized to None.
274 2 Ayato KOTSUGI
        """
275
        self.result_image = None
276
277 8 Ayato KOTSUGI
    def readImage(self, image_path):
278
        """
279
        @brief Reads an image from the specified path.
280
281
        @param image_path: The path to the image file.
282
283
        @return The read image.
284 1 Satya Sai Abhiram OGGU
        """
285
        image = cv2.imread(image_path)
286
        return image
287
288 8 Ayato KOTSUGI
    def setImage(self, image):
289
        """
290
        @brief Sets the result image.
291
292
        @param image: The image to set as the result image.
293
294
        @return The set result image.
295 1 Satya Sai Abhiram OGGU
        """
296
        self.result_image = image
297
        return self.result_image
298
299 8 Ayato KOTSUGI
    def saveImage(self):
300
        """
301
        @brief Displays and saves the result image.
302
303
        @details
304
        The result image is displayed in a window with the window name indicating the displayed image.
305
        The window remains open until a key is pressed. Then, it is closed, and the program continues.
306
307
        @note
308
        Ensure that the OpenCV window is closed before proceeding with the execution of other code.
309
310
        @warning
311
        This method uses blocking code (cv2.waitKey(0)) that might lead to issues in certain scenarios.
312 1 Satya Sai Abhiram OGGU
        """
313
        # display image. (window name, image to display)
314
        cv2.imshow(f"Displaying {self.result_image}", self.result_image)
315
        cv2.waitKey(0)
316 8 Ayato KOTSUGI
        cv2.destroyAllWindows()
317 1 Satya Sai Abhiram OGGU
318
</code></pre>