Project

General

Profile

Actions

Coding » History » Revision 3

« Previous | Revision 3/10 (diff) | Next »
Ayato KOTSUGI , 01/18/2024 01:41 PM


Coding

main.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#˅
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}).")

MaskCreator.py
import cv2
import numpy as np

class MaskCreator:
import cv2
import numpy as np

class MaskCreator:

    def __init__(self, image):
        self.__image = image
        self.__alpha_gradient = None
        self.__gamma_corrected = None
        self.result_image = None
        self.__mask = None

    def create_mask(self, image_side, mask_width, image_width):
        self.__mask = self.__image.shape[1] * mask_width // image_width
        if image_side == 1:
            self.__alpha_gradient = np.linspace(1, 0, self.__mask)
        elif image_side == 0:
            self.__alpha_gradient = np.linspace(0, 1, self.__mask)

    def gammaCorrection(self, gamma):
            self.__gamma_corrected = np.uint8(cv2.pow(self.__image / 255.0, gamma) * 255)

    def alpha_blending(self, image_side):
        if image_side == 1:
            for col in range(self.__mask):
                alpha = self.__alpha_gradient[-self.__mask + col]
                self.result_image[:, col] = alpha * self.__gamma_corrected[:, col] + (1 - alpha) * self.result_image[:, col]
        elif image_side == 0:
            for col in range(self.__mask):
                alpha = self.__alpha_gradient[-self.__mask + col]
                self.result_image[:, -self.__mask + col] = alpha * self.__gamma_corrected[:, -self.__mask + col] + (1 - alpha) * self.result_image[:, -self.__mask + col]

    def mod_intensity(self, image_side):
        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)

mainDisplay.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import cv2

class MainDisplay:

    def __init__(self):
        self.result_image = None

    def readImage(self, image_path):
        image = cv2.imread(image_path)
        return image

    def setImage(self, image):
        self.result_image = image
        return self.result_image

    def saveImage(self):
        # display image. (window name, image to display)
        cv2.imshow(f"Displaying {self.result_image}", self.result_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

Updated by Ayato KOTSUGI over 1 year ago · 3 revisions