Project

General

Profile

Coding » History » Revision 9

Revision 8 (Ayato KOTSUGI , 01/18/2024 02:22 PM) → Revision 9/10 (Ayato KOTSUGI , 01/18/2024 02:31 PM)

 
 h1=. <pre> 
 Coding</pre> 

 --- 

 *[[Wiki]]*     |    *[[About Us]]*    |    *[[Regarding Our Project]]* | *[[Coding]]*    |    *[[UML Diagrams]]*    |    *[[Results]]*    | 


 --- 

 h2. main.py: creates objects and variables necessary for the another file. 
 <pre><code class="python"> 
 #!/usr/bin/env python3 
 #-*- # -*- coding: utf-8 -*- 
  """ 
 @file 
 @brief This script performs image modification and processing based on configurations specified in the "config_left.ini" or "config_right.ini" file. 

 @details 
 The script utilizes the following classes: 
 - `MaskCreator`: Responsible for creating masks and applying image modifications. 
 - `ConfigReader`: Reads configuration settings from the specified configuration file ("config_left.ini" or "config_right.ini"). 
 - `MainDisplay`: Handles the main display functionalities. 

 @note 
 Make sure to change the configuration file path (`CReader = ConfigReader("config_left.ini")`) accordingly based on the image side. 

 @date January 18, 2024 
 """ 

 from MaskCreator import MaskCreator 
 from ConfigReader import ConfigReader 
 from MainDisplay import MainDisplay 


 

 # creating object for MainDisplay, ConfigReader classes 

 # change to config_left.ini or config_right.ini when necessary 
 CReader = ConfigReader("config_left.ini") 
 # CReader = ConfigReader("config_right.ini") 
 MDisplay = MainDisplay() 

 # initializing variables 
 mask_width = CReader.getProjectedOverlapWidth() 
 image_width = CReader.getProjectedImageWidth() 
 gamma = CReader.getGamma() 
 image_side = CReader.getImageSide() 
 image_path = CReader.getImageName() 

 if image_side == 0: 
     image_name = 'left' 
 elif image_side == 1: 
     image_name = 'right' 
 else: 
     print("Invalid ImageSide value in config.ini. Use 0 for left image, 1 for right image.") 

 # loading image 
 image = MDisplay.readImage(image_path) 
 result_image = MDisplay.setImage(image) 


 

 if image is not None: 
     # creating object for MaskCreator class 
     MCreator = MaskCreator(image) 
    
     # image modification 
     MCreator.create_mask(image_side, mask_width, image_width) 
     MCreator.gammaCorrection(gamma) 
     MCreator.result_image = result_image 
     MCreator.alpha_blending(image_side) 
     MCreator.mod_intensity(image_side) 
    
     # saving image 
     MDisplay.saveImage() 
 else: 
     print(f"Failed to read the image (ImageSide={image_side}).") 

 </code></pre> 

 h2. MaskCreator.py: creates objects and variables necessary for the other file. 
 

 <pre><code class="python"> 

 
 #!/usr/bin/env python3 
 #-*- coding: utf-8 -*- 
 #˅ 
 from MaskCreator import cv2 MaskCreator 
 from ConfigReader import numpy as np ConfigReader 
 from MainDisplay import MainDisplay 


 # creating object for MainDisplay, ConfigReader classes 

 class MaskCreator: 
     """ 
     @brief The MaskCreator class performs image modification, including creating masks, gamma correction, alpha blending, and intensity modification. 

     @details 
     The class includes methods for: 
     - Creating masks based on specified parameters. 
     - Applying gamma correction # change to the image. 
     - Performing alpha blending on the image based on the image side. 
     - Modifying image intensity based on the image side. 

     @note 
     Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class. 

     @date January 18, 2024 
     """ 

     def __init__(self, image): 
         """ 
         @brief Initializes a MaskCreator object. 

         @param image: The input image on which modifications will be applied. 
         """ 
         self.__image config_left.ini or config_right.ini when necessary 
 CReader = image 
         self.__alpha_gradient ConfigReader("config_left.ini") 
 # CReader = None 
         self.__gamma_corrected ConfigReader("config_right.ini") 
 MDisplay = None 
         self.result_image MainDisplay() 

 # initializing variables 
 mask_width = None 
         self.__mask CReader.getProjectedOverlapWidth() 
 image_width = None 
        
     def create_mask(self, image_side, mask_width, image_width): 
         """ 
         @brief Creates a mask based on specified parameters. 

         @param image_side: The side of the image (0 for left, 1 for right). 
         @param mask_width: The width of the mask. 
         @param image_width: The width of the input image. 
         """ 
         self.__mask CReader.getProjectedImageWidth() 
 gamma = self.__image.shape[1] * mask_width // image_width 
         CReader.getGamma() 
 image_side = CReader.getImageSide() 
 image_path = CReader.getImageName() 

 if image_side == 1: 
             self.__alpha_gradient 0: 
     image_name = np.linspace(1, 0, self.__mask) 
         'left' 
 elif image_side == 0: 
             self.__alpha_gradient 1: 
     image_name = np.linspace(0, 1, self.__mask) 

     def gammaCorrection(self, gamma): 
         """ 
         @brief Applies gamma correction to the image. 

         @param gamma: The gamma 'right' 
 else: 
     print("Invalid ImageSide value in config.ini. Use 0 for correction. 
         """ 
         self.__gamma_corrected = np.uint8(cv2.pow(self.__image / 255.0, gamma) * 255) 
        
     def alpha_blending(self, image_side): 
         """ 
         @brief Performs alpha blending on the left image, 1 for right image.") 

 # loading image based on the 
 image side. 

         @param image_side: The side of the image (0 for left, 1 for right). 
         """ 
         if image_side == 1: 
             for col in range(self.__mask): 
                 alpha = self.__alpha_gradient[-self.__mask + col] 
                 self.result_image[:, col] MDisplay.readImage(image_path) 
 result_image = alpha * self.__gamma_corrected[:, col] + (1 - alpha) * self.result_image[:, col] 
         elif image_side == 0: 
             MDisplay.setImage(image) 


 if image is not None: 
     # creating object for col in range(self.__mask): 
                 alpha MaskCreator class 
     MCreator = self.__alpha_gradient[-self.__mask + col] 
                 self.result_image[:, -self.__mask + col] MaskCreator(image) 
    
     # image modification 
     MCreator.create_mask(image_side, mask_width, image_width) 
     MCreator.gammaCorrection(gamma) 
     MCreator.result_image = alpha * self.__gamma_corrected[:, -self.__mask + col] + (1 - alpha) * self.result_image[:, -self.__mask + col] 
        
     def mod_intensity(self, image_side): 
         """ 
         @brief Modifies result_image 
     MCreator.alpha_blending(image_side) 
     MCreator.mod_intensity(image_side) 
    
     # saving image intensity based on 
     MDisplay.saveImage() 
 else: 
     print(f"Failed to read the image side. 

         @param image_side: The side of the image (0 for left, 1 for right). 
         """ 
         if image_side == 1: 
             for col in range(self.__mask): 
                 intensity_factor = 1.0 - (self.__mask - col) / self.__mask 
                 self.result_image[:, col] = (self.result_image[:, col] * intensity_factor).astype(np.uint8) 
         elif image_side == 0: 
             for col in range(self.__mask): 
                 intensity_factor = 1.0 - col / self.__mask 
                 self.result_image[:, -self.__mask + col] = (self.result_image[:, -self.__mask + col] * intensity_factor).astype(np.uint8) (ImageSide={image_side}).") 

 </code></pre> 


 h2. mainDisplay.py:    Displays the result 

 <pre><code class="python"> 
 #!/usr/bin/env python3 
 #-*- coding: utf-8 -*- 
 #!/usr/bin/env python3 
 # -*- coding: utf-8 -*- 
 """ 
 @file 
 @brief This script defines the MainDisplay class, which handles reading, setting, and displaying images using the OpenCV library. 

 @date January 18, 2024 
 """ 

 import cv2 

 class MainDisplay: 
     """ 
     @brief The MainDisplay class provides functionalities for image handling and display. 

     @details 
     The class includes methods for: 
     - Reading an image from a specified path. 
     - Setting the result image. 
     - Displaying the result image using the OpenCV library. 

     @note 
     Ensure that the OpenCV library is installed (`pip install opencv-python`) before using this class. 

     @date January 18, 2024 
     """ 

     def __init__(self): 
         """ 
         @brief Initializes a MainDisplay object. 

         @details 
         The result_image attribute is initialized to None. 
         """ 
         self.result_image = None 

     def readImage(self, image_path): 
         """ 
         @brief Reads an image from the specified path. 

         @param image_path: The path to the image file. 

         @return The read image. 
         """ 
         image = cv2.imread(image_path) 
         return image 

     def setImage(self, image): 
         """ 
         @brief Sets the result image. 

         @param image: The image to set as the result image. 

         @return The set result image. 
         """ 
         self.result_image = image 
         return self.result_image 

     def saveImage(self): 
         """ 
         @brief Displays and saves the result image. 

         @details 
         The result image is displayed in a window with the window name indicating the displayed image. 
         The window remains open until a key is pressed. Then, it is closed, and the program continues. 

         @note 
         Ensure that the OpenCV window is closed before proceeding with the execution of other code. 

         @warning 
         This method uses blocking code (cv2.waitKey(0)) that might lead to issues in certain scenarios. 
         """ 
         # display image. (window name, image to display) 
         cv2.imshow(f"Displaying {self.result_image}", self.result_image) 
         cv2.waitKey(0) 
         cv2.destroyAllWindows() 

 


 </code></pre>