Coding » History » Version 8
Ayato KOTSUGI , 01/18/2024 02:22 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 | 8 | Ayato KOTSUGI | # -*- coding: utf-8 -*- |
| 16 | """ |
||
| 17 | @file |
||
| 18 | @brief This script performs image modification and processing based on configurations specified in the "config_left.ini" or "config_right.ini" file. |
||
| 19 | |||
| 20 | @details |
||
| 21 | The script utilizes the following classes: |
||
| 22 | - `MaskCreator`: Responsible for creating masks and applying image modifications. |
||
| 23 | - `ConfigReader`: Reads configuration settings from the specified configuration file ("config_left.ini" or "config_right.ini"). |
||
| 24 | - `MainDisplay`: Handles the main display functionalities. |
||
| 25 | |||
| 26 | @note |
||
| 27 | Make sure to change the configuration file path (`CReader = ConfigReader("config_left.ini")`) accordingly based on the image side. |
||
| 28 | |||
| 29 | @date January 18, 2024 |
||
| 30 | """ |
||
| 31 | |||
| 32 | 2 | Ayato KOTSUGI | from MaskCreator import MaskCreator |
| 33 | from ConfigReader import ConfigReader |
||
| 34 | from MainDisplay import MainDisplay |
||
| 35 | |||
| 36 | # creating object for MainDisplay, ConfigReader classes |
||
| 37 | |||
| 38 | # change to config_left.ini or config_right.ini when necessary |
||
| 39 | CReader = ConfigReader("config_left.ini") |
||
| 40 | # CReader = ConfigReader("config_right.ini") |
||
| 41 | MDisplay = MainDisplay() |
||
| 42 | |||
| 43 | # initializing variables |
||
| 44 | mask_width = CReader.getProjectedOverlapWidth() |
||
| 45 | image_width = CReader.getProjectedImageWidth() |
||
| 46 | gamma = CReader.getGamma() |
||
| 47 | image_side = CReader.getImageSide() |
||
| 48 | image_path = CReader.getImageName() |
||
| 49 | |||
| 50 | if image_side == 0: |
||
| 51 | image_name = 'left' |
||
| 52 | elif image_side == 1: |
||
| 53 | image_name = 'right' |
||
| 54 | else: |
||
| 55 | print("Invalid ImageSide value in config.ini. Use 0 for left image, 1 for right image.") |
||
| 56 | |||
| 57 | # loading image |
||
| 58 | image = MDisplay.readImage(image_path) |
||
| 59 | result_image = MDisplay.setImage(image) |
||
| 60 | |||
| 61 | if image is not None: |
||
| 62 | # creating object for MaskCreator class |
||
| 63 | MCreator = MaskCreator(image) |
||
| 64 | |||
| 65 | # image modification |
||
| 66 | MCreator.create_mask(image_side, mask_width, image_width) |
||
| 67 | MCreator.gammaCorrection(gamma) |
||
| 68 | 1 | Satya Sai Abhiram OGGU | MCreator.result_image = result_image |
| 69 | 4 | Ayato KOTSUGI | MCreator.alpha_blending(image_side) |
| 70 | 1 | Satya Sai Abhiram OGGU | MCreator.mod_intensity(image_side) |
| 71 | |||
| 72 | # saving image |
||
| 73 | MDisplay.saveImage() |
||
| 74 | else: |
||
| 75 | print(f"Failed to read the image (ImageSide={image_side}).") |
||
| 76 | |||
| 77 | </code></pre> |
||
| 78 | |||
| 79 | 8 | Ayato KOTSUGI | h2. MaskCreator.py: creates objects and variables necessary for the other file. |
| 80 | |||
| 81 | 1 | Satya Sai Abhiram OGGU | <pre><code class="python"> |
| 82 | 8 | Ayato KOTSUGI | #!/usr/bin/env python3 |
| 83 | #-*- coding: utf-8 -*- |
||
| 84 | #˅ |
||
| 85 | from MaskCreator import MaskCreator |
||
| 86 | from ConfigReader import ConfigReader |
||
| 87 | from MainDisplay import MainDisplay |
||
| 88 | 1 | Satya Sai Abhiram OGGU | |
| 89 | |||
| 90 | 8 | Ayato KOTSUGI | # creating object for MainDisplay, ConfigReader classes |
| 91 | 1 | Satya Sai Abhiram OGGU | |
| 92 | 8 | Ayato KOTSUGI | # change to config_left.ini or config_right.ini when necessary |
| 93 | CReader = ConfigReader("config_left.ini") |
||
| 94 | # CReader = ConfigReader("config_right.ini") |
||
| 95 | MDisplay = MainDisplay() |
||
| 96 | 2 | Ayato KOTSUGI | |
| 97 | 8 | Ayato KOTSUGI | # initializing variables |
| 98 | mask_width = CReader.getProjectedOverlapWidth() |
||
| 99 | image_width = CReader.getProjectedImageWidth() |
||
| 100 | gamma = CReader.getGamma() |
||
| 101 | image_side = CReader.getImageSide() |
||
| 102 | image_path = CReader.getImageName() |
||
| 103 | 1 | Satya Sai Abhiram OGGU | |
| 104 | 8 | Ayato KOTSUGI | if image_side == 0: |
| 105 | image_name = 'left' |
||
| 106 | elif image_side == 1: |
||
| 107 | image_name = 'right' |
||
| 108 | else: |
||
| 109 | print("Invalid ImageSide value in config.ini. Use 0 for left image, 1 for right image.") |
||
| 110 | 1 | Satya Sai Abhiram OGGU | |
| 111 | 8 | Ayato KOTSUGI | # loading image |
| 112 | image = MDisplay.readImage(image_path) |
||
| 113 | result_image = MDisplay.setImage(image) |
||
| 114 | 1 | Satya Sai Abhiram OGGU | |
| 115 | |||
| 116 | 8 | Ayato KOTSUGI | if image is not None: |
| 117 | # creating object for MaskCreator class |
||
| 118 | MCreator = MaskCreator(image) |
||
| 119 | |||
| 120 | # image modification |
||
| 121 | MCreator.create_mask(image_side, mask_width, image_width) |
||
| 122 | MCreator.gammaCorrection(gamma) |
||
| 123 | MCreator.result_image = result_image |
||
| 124 | MCreator.alpha_blending(image_side) |
||
| 125 | MCreator.mod_intensity(image_side) |
||
| 126 | |||
| 127 | # saving image |
||
| 128 | MDisplay.saveImage() |
||
| 129 | else: |
||
| 130 | print(f"Failed to read the image (ImageSide={image_side}).") |
||
| 131 | 1 | Satya Sai Abhiram OGGU | |
| 132 | 2 | Ayato KOTSUGI | </code></pre> |
| 133 | 4 | Ayato KOTSUGI | |
| 134 | |||
| 135 | h2. mainDisplay.py: Displays the result |
||
| 136 | 1 | Satya Sai Abhiram OGGU | |
| 137 | 4 | Ayato KOTSUGI | <pre><code class="python"> |
| 138 | #!/usr/bin/env python3 |
||
| 139 | 2 | Ayato KOTSUGI | #-*- coding: utf-8 -*- |
| 140 | 8 | Ayato KOTSUGI | #!/usr/bin/env python3 |
| 141 | # -*- coding: utf-8 -*- |
||
| 142 | """ |
||
| 143 | @file |
||
| 144 | @brief This script defines the MainDisplay class, which handles reading, setting, and displaying images using the OpenCV library. |
||
| 145 | 2 | Ayato KOTSUGI | |
| 146 | 8 | Ayato KOTSUGI | @date January 18, 2024 |
| 147 | """ |
||
| 148 | |||
| 149 | 2 | Ayato KOTSUGI | import cv2 |
| 150 | |||
| 151 | 4 | Ayato KOTSUGI | class MainDisplay: |
| 152 | 8 | Ayato KOTSUGI | """ |
| 153 | @brief The MainDisplay class provides functionalities for image handling and display. |
||
| 154 | 2 | Ayato KOTSUGI | |
| 155 | 8 | Ayato KOTSUGI | @details |
| 156 | The class includes methods for: |
||
| 157 | - Reading an image from a specified path. |
||
| 158 | - Setting the result image. |
||
| 159 | - Displaying the result image using the OpenCV library. |
||
| 160 | |||
| 161 | @note |
||
| 162 | Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class. |
||
| 163 | |||
| 164 | @date January 18, 2024 |
||
| 165 | """ |
||
| 166 | |||
| 167 | 2 | Ayato KOTSUGI | def __init__(self): |
| 168 | 8 | Ayato KOTSUGI | """ |
| 169 | @brief Initializes a MainDisplay object. |
||
| 170 | |||
| 171 | @details |
||
| 172 | The result_image attribute is initialized to None. |
||
| 173 | """ |
||
| 174 | 2 | Ayato KOTSUGI | self.result_image = None |
| 175 | |||
| 176 | def readImage(self, image_path): |
||
| 177 | 8 | Ayato KOTSUGI | """ |
| 178 | @brief Reads an image from the specified path. |
||
| 179 | |||
| 180 | @param image_path: The path to the image file. |
||
| 181 | |||
| 182 | @return The read image. |
||
| 183 | """ |
||
| 184 | 1 | Satya Sai Abhiram OGGU | image = cv2.imread(image_path) |
| 185 | return image |
||
| 186 | |||
| 187 | def setImage(self, image): |
||
| 188 | 8 | Ayato KOTSUGI | """ |
| 189 | @brief Sets the result image. |
||
| 190 | |||
| 191 | @param image: The image to set as the result image. |
||
| 192 | |||
| 193 | @return The set result image. |
||
| 194 | """ |
||
| 195 | 1 | Satya Sai Abhiram OGGU | self.result_image = image |
| 196 | return self.result_image |
||
| 197 | |||
| 198 | def saveImage(self): |
||
| 199 | 8 | Ayato KOTSUGI | """ |
| 200 | @brief Displays and saves the result image. |
||
| 201 | |||
| 202 | @details |
||
| 203 | The result image is displayed in a window with the window name indicating the displayed image. |
||
| 204 | The window remains open until a key is pressed. Then, it is closed, and the program continues. |
||
| 205 | |||
| 206 | @note |
||
| 207 | Ensure that the OpenCV window is closed before proceeding with the execution of other code. |
||
| 208 | |||
| 209 | @warning |
||
| 210 | This method uses blocking code (cv2.waitKey(0)) that might lead to issues in certain scenarios. |
||
| 211 | """ |
||
| 212 | 1 | Satya Sai Abhiram OGGU | # display image. (window name, image to display) |
| 213 | cv2.imshow(f"Displaying {self.result_image}", self.result_image) |
||
| 214 | cv2.waitKey(0) |
||
| 215 | cv2.destroyAllWindows() |
||
| 216 | 8 | Ayato KOTSUGI | |
| 217 | 1 | Satya Sai Abhiram OGGU | |
| 218 | </code></pre> |