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