Project

General

Profile

Coding » History » Version 4

Ayato KOTSUGI , 01/18/2024 01:47 PM

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