Project

General

Profile

Coding » History » Version 6

Satya Sai Abhiram OGGU , 01/18/2024 01:48 PM

1 1 Satya Sai Abhiram OGGU
2 5 Satya Sai Abhiram OGGU
h1=. <pre>
3
Coding</pre>
4
5
---
6
7
*[[Wiki]]*   |  *[[About Us]]*  |  *[[Regarding Our Project]]* | *[[Coding]]*  |  *[[UML Diagrams]]*  |
8
9
---
10 6 Satya Sai Abhiram OGGU
11 4 Ayato KOTSUGI
h2. main.py: creates objects and variables necessary for the another file.
12 2 Ayato KOTSUGI
<pre><code class="python">
13
#!/usr/bin/env python3
14
#-*- coding: utf-8 -*-
15
16
from MaskCreator import MaskCreator
17
from ConfigReader import ConfigReader
18
from MainDisplay import MainDisplay
19
20
21
# creating object for MainDisplay, ConfigReader classes
22
23
# change to config_left.ini or config_right.ini when necessary
24
CReader = ConfigReader("config_left.ini")
25
# CReader = ConfigReader("config_right.ini")
26
MDisplay = MainDisplay()
27
28
# initializing variables
29
mask_width = CReader.getProjectedOverlapWidth()
30
image_width = CReader.getProjectedImageWidth()
31
gamma = CReader.getGamma()
32
image_side = CReader.getImageSide()
33
image_path = CReader.getImageName()
34
35
if image_side == 0:
36
    image_name = 'left'
37
elif image_side == 1:
38
    image_name = 'right'
39
else:
40
    print("Invalid ImageSide value in config.ini. Use 0 for left image, 1 for right image.")
41
42
# loading image
43
image = MDisplay.readImage(image_path)
44
result_image = MDisplay.setImage(image)
45
46
47
if image is not None:
48
    # creating object for MaskCreator class
49
    MCreator = MaskCreator(image)
50
    
51
    # image modification
52
    MCreator.create_mask(image_side, mask_width, image_width)
53
    MCreator.gammaCorrection(gamma)
54
    MCreator.result_image = result_image
55
    MCreator.alpha_blending(image_side)
56
    MCreator.mod_intensity(image_side)
57
    
58
    # saving image
59
    MDisplay.saveImage()
60
else:
61
    print(f"Failed to read the image (ImageSide={image_side}).")
62
63
</code></pre>
64
65 4 Ayato KOTSUGI
h2. MaskCreator.py: 
66 2 Ayato KOTSUGI
<pre><code class="python">
67
import cv2
68
import numpy as np
69
70 1 Satya Sai Abhiram OGGU
class MaskCreator:
71 4 Ayato KOTSUGI
    """
72
    @brief The MaskCreator class performs image modification, including creating masks, gamma correction, alpha blending, and intensity modification.
73 1 Satya Sai Abhiram OGGU
74 4 Ayato KOTSUGI
    @details
75
    The class includes methods for:
76
    - Creating masks based on specified parameters.
77
    - Applying gamma correction to the image.
78
    - Performing alpha blending on the image based on the image side.
79
    - Modifying image intensity based on the image side.
80 1 Satya Sai Abhiram OGGU
81 4 Ayato KOTSUGI
    @note
82
    Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class.
83
84
    @date January 18, 2024
85
    """
86
87 1 Satya Sai Abhiram OGGU
    def __init__(self, image):
88 4 Ayato KOTSUGI
        """
89
        @brief Initializes a MaskCreator object.
90
91
        @param image: The input image on which modifications will be applied.
92
        """
93 2 Ayato KOTSUGI
        self.__image = image
94
        self.__alpha_gradient = None
95 1 Satya Sai Abhiram OGGU
        self.__gamma_corrected = None
96
        self.result_image = None
97
        self.__mask = None
98
        
99
    def create_mask(self, image_side, mask_width, image_width):
100 4 Ayato KOTSUGI
        """
101
        @brief Creates a mask based on specified parameters.
102
103
        @param image_side: The side of the image (0 for left, 1 for right).
104
        @param mask_width: The width of the mask.
105
        @param image_width: The width of the input image.
106
        """
107 1 Satya Sai Abhiram OGGU
        self.__mask = self.__image.shape[1] * mask_width // image_width
108
        if image_side == 1:
109 2 Ayato KOTSUGI
            self.__alpha_gradient = np.linspace(1, 0, self.__mask)
110 1 Satya Sai Abhiram OGGU
        elif image_side == 0:
111
            self.__alpha_gradient = np.linspace(0, 1, self.__mask)
112
113
    def gammaCorrection(self, gamma):
114 4 Ayato KOTSUGI
        """
115
        @brief Applies gamma correction to the image.
116 1 Satya Sai Abhiram OGGU
117 4 Ayato KOTSUGI
        @param gamma: The gamma value for correction.
118
        """
119
        self.__gamma_corrected = np.uint8(cv2.pow(self.__image / 255.0, gamma) * 255)
120
        
121 2 Ayato KOTSUGI
    def alpha_blending(self, image_side):
122 4 Ayato KOTSUGI
        """
123
        @brief Performs alpha blending on the image based on the image side.
124
125
        @param image_side: The side of the image (0 for left, 1 for right).
126
        """
127 2 Ayato KOTSUGI
        if image_side == 1:
128
            for col in range(self.__mask):
129
                alpha = self.__alpha_gradient[-self.__mask + col]
130 1 Satya Sai Abhiram OGGU
                self.result_image[:, col] = alpha * self.__gamma_corrected[:, col] + (1 - alpha) * self.result_image[:, col]
131
        elif image_side == 0:
132
            for col in range(self.__mask):
133
                alpha = self.__alpha_gradient[-self.__mask + col]
134
                self.result_image[:, -self.__mask + col] = alpha * self.__gamma_corrected[:, -self.__mask + col] + (1 - alpha) * self.result_image[:, -self.__mask + col]
135 2 Ayato KOTSUGI
        
136
    def mod_intensity(self, image_side):
137 4 Ayato KOTSUGI
        """
138
        @brief Modifies image intensity based on the image side.
139
140
        @param image_side: The side of the image (0 for left, 1 for right).
141
        """
142 2 Ayato KOTSUGI
        if image_side == 1:
143
            for col in range(self.__mask):
144
                intensity_factor = 1.0 - (self.__mask - col) / self.__mask
145
                self.result_image[:, col] = (self.result_image[:, col] * intensity_factor).astype(np.uint8)
146 1 Satya Sai Abhiram OGGU
        elif image_side == 0:
147
            for col in range(self.__mask):
148 2 Ayato KOTSUGI
                intensity_factor = 1.0 - col / self.__mask
149
                self.result_image[:, -self.__mask + col] = (self.result_image[:, -self.__mask + col] * intensity_factor).astype(np.uint8)
150
151
</code></pre>
152
153
154 4 Ayato KOTSUGI
h2. mainDisplay.py:  Displays the result
155
156 2 Ayato KOTSUGI
<pre><code class="python">
157
#!/usr/bin/env python3
158
#-*- coding: utf-8 -*-
159
160
import cv2
161
162
class MainDisplay:
163
164
    def __init__(self):
165
        self.result_image = None
166
167
    def readImage(self, image_path):
168
        image = cv2.imread(image_path)
169
        return image
170
171
    def setImage(self, image):
172
        self.result_image = image
173
        return self.result_image
174
175
    def saveImage(self):
176
        # display image. (window name, image to display)
177
        cv2.imshow(f"Displaying {self.result_image}", self.result_image)
178
        cv2.waitKey(0)
179
        cv2.destroyAllWindows()
180
181
</code></pre>