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