Coding » History » Version 2
Ayato KOTSUGI , 01/18/2024 01:40 PM
1 | 1 | Satya Sai Abhiram OGGU | h1. Coding |
---|---|---|---|
2 | 2 | Ayato KOTSUGI | |
3 | h2. main.py |
||
4 | <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 | h2. MaskCreator.py |
||
58 | <pre><code class="python"> |
||
59 | import cv2 |
||
60 | import numpy as np |
||
61 | |||
62 | class MaskCreator: |
||
63 | |||
64 | def __init__(self, image): |
||
65 | self.__image = image |
||
66 | self.__alpha_gradient = None |
||
67 | self.__gamma_corrected = None |
||
68 | self.result_image = None |
||
69 | self.__mask = None |
||
70 | |||
71 | def create_mask(self, image_side, mask_width, image_width): |
||
72 | self.__mask = self.__image.shape[1] * mask_width // image_width |
||
73 | if image_side == 1: |
||
74 | self.__alpha_gradient = np.linspace(1, 0, self.__mask) |
||
75 | elif image_side == 0: |
||
76 | self.__alpha_gradient = np.linspace(0, 1, self.__mask) |
||
77 | |||
78 | def gammaCorrection(self, gamma): |
||
79 | self.__gamma_corrected = np.uint8(cv2.pow(self.__image / 255.0, gamma) * 255) |
||
80 | |||
81 | |||
82 | def alpha_blending(self, image_side): |
||
83 | if image_side == 1: |
||
84 | for col in range(self.__mask): |
||
85 | alpha = self.__alpha_gradient[-self.__mask + col] |
||
86 | self.result_image[:, col] = alpha * self.__gamma_corrected[:, col] + (1 - alpha) * self.result_image[:, col] |
||
87 | elif image_side == 0: |
||
88 | for col in range(self.__mask): |
||
89 | alpha = self.__alpha_gradient[-self.__mask + col] |
||
90 | self.result_image[:, -self.__mask + col] = alpha * self.__gamma_corrected[:, -self.__mask + col] + (1 - alpha) * self.result_image[:, -self.__mask + col] |
||
91 | |||
92 | def mod_intensity(self, image_side): |
||
93 | if image_side == 1: |
||
94 | for col in range(self.__mask): |
||
95 | intensity_factor = 1.0 - (self.__mask - col) / self.__mask |
||
96 | self.result_image[:, col] = (self.result_image[:, col] * intensity_factor).astype(np.uint8) |
||
97 | elif image_side == 0: |
||
98 | for col in range(self.__mask): |
||
99 | intensity_factor = 1.0 - col / self.__mask |
||
100 | self.result_image[:, -self.__mask + col] = (self.result_image[:, -self.__mask + col] * intensity_factor).astype(np.uint8) |
||
101 | |||
102 | </code></pre> |
||
103 | |||
104 | |||
105 | h2. mainDisplay.py |
||
106 | <pre><code class="python"> |
||
107 | #!/usr/bin/env python3 |
||
108 | #-*- coding: utf-8 -*- |
||
109 | |||
110 | import cv2 |
||
111 | |||
112 | class MainDisplay: |
||
113 | |||
114 | def __init__(self): |
||
115 | self.result_image = None |
||
116 | |||
117 | def readImage(self, image_path): |
||
118 | image = cv2.imread(image_path) |
||
119 | return image |
||
120 | |||
121 | def setImage(self, image): |
||
122 | self.result_image = image |
||
123 | return self.result_image |
||
124 | |||
125 | def saveImage(self): |
||
126 | # display image. (window name, image to display) |
||
127 | cv2.imshow(f"Displaying {self.result_image}", self.result_image) |
||
128 | cv2.waitKey(0) |
||
129 | cv2.destroyAllWindows() |
||
130 | |||
131 | </code></pre> |