Project

General

Profile

Coding » History » Version 7

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