Project

General

Profile

Codes » img_processor.py

Kentaro HARATAKE, 01/16/2025 01:31 PM

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3

    
4
# ˅
5

    
6
import numpy as np
7
import cv2
8
import os
9

    
10
class ImgProcessor:
11
    """!
12
    @brief Performs alpha blending and gamma correction.
13

    
14
    This class provides methods for image processing, including alpha blending 
15
    of overlapping image regions and gamma correction for brightness adjustment.
16
    """
17

    
18
    def __init__(self, config):
19
        """!
20
        @brief Initializes the ImgProcessor object.
21

    
22
        Sets the configuration object to retrieve parameters like overlap width 
23
        and gamma values.
24

    
25
        @param config Configuration object containing parameters.
26
        """
27
        self.__config = config
28

    
29
    def alphaBlend(self, l_img, r_img):
30
        """!
31
        @brief Performs alpha blending on overlapping regions of two images.
32

    
33
        Blends the overlapping regions of the left and right images using 
34
        a linear gradient alpha mask.
35

    
36
        @param l_img Left image.
37
        @param r_img Right image.
38
        @return Tuple containing processed left and right images.
39
        """
40
        overlap_width = self.__config.getOverlapWidth()
41
        
42
        l_overlap = l_img[:, -overlap_width:, :].copy()
43
        r_overlap = r_img[:, :overlap_width, :].copy()
44

    
45
        height = l_overlap.shape[0]
46

    
47
        alpha = np.linspace(1, 0, overlap_width).reshape(1, overlap_width, 1)
48
        alpha = np.tile(alpha, (height, 1, 3))
49

    
50
        l_overlap = (alpha * l_overlap).astype(np.uint8)
51
        r_overlap = ((1 - alpha) * r_overlap).astype(np.uint8)
52

    
53
        l_common = l_img[:, :-overlap_width, :]
54
        r_common = r_img[:, overlap_width:, :]
55
        l_processed = np.concatenate([l_common, l_overlap], axis=1)
56
        r_processed = np.concatenate([r_overlap, r_common], axis=1)
57

    
58
        return l_processed, r_processed
59

    
60
    def GammaCorrection(self, img):
61
        """!
62
        @brief Applies gamma correction to an image.
63

    
64
        Adjusts the brightness of the image using gamma correction based 
65
        on the gamma value in the configuration.
66

    
67
        @param img Input image.
68
        @return Gamma-corrected image.
69
        """
70
        gamma = self.__config.getGamma()
71
        inv_gamma = 1.0 / gamma
72
        lookup_table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]).astype("uint8")
73
        corrected_image = cv2.LUT(img, lookup_table)
74
        return corrected_image
75

    
76
    def cropImage(self):
77
        """!
78
        @brief Crops the left and right halves of an image with overlap.
79

    
80
        Reads the image from the specified path, splits it into left and 
81
        right halves with the overlap region, and returns the cropped images.
82

    
83
        @return Tuple containing cropped left and right images.
84
        """
85
        path = str(os.getcwd() + self.__config.getImgPath())
86
        overlap_width = self.__config.getOverlapWidth()
87
        image = cv2.imread(path)
88
        width = image.shape[1]
89
        l_img = image[:, :width // 2 + overlap_width, :]
90
        r_img = image[:, width // 2 - overlap_width:, :]
91
        return l_img, r_img
(3-3/5)