1
|
#!/usr/bin/env python
|
2
|
# -*- coding: utf-8 -*-
|
3
|
|
4
|
##
|
5
|
# @file image_blender.py
|
6
|
# @brief This script defines the ImageBlender class, which is used for blending two images together with adjustable blending ratio and gamma correction.
|
7
|
#
|
8
|
|
9
|
import cv2 as cv
|
10
|
import numpy as np
|
11
|
from gammaCorrector import GammaCorrector
|
12
|
|
13
|
##
|
14
|
# @class ImageBlender
|
15
|
# @brief A class for blending two images using a specified blend ratio and gamma correction.
|
16
|
#
|
17
|
class ImageBlender:
|
18
|
# ˅
|
19
|
|
20
|
# ˄
|
21
|
|
22
|
##
|
23
|
# @fn __init__(self, gamma_corrector, blend_ratio=0.21)
|
24
|
# @brief Constructor for ImageBlender.
|
25
|
# @param gamma_corrector An instance of GammaCorrector for applying gamma correction.
|
26
|
# @param blend_ratio The ratio at which images will be blended, default is 0.21.
|
27
|
#
|
28
|
def __init__(self, gamma_corrector, blend_ratio=0.21):
|
29
|
# ˅
|
30
|
self.gamma_corrector = gamma_corrector
|
31
|
self.blend_ratio = blend_ratio
|
32
|
# ˄
|
33
|
|
34
|
##
|
35
|
# @fn blend_images(self, left_image, right_image)
|
36
|
# @brief Blends two images together using the blend ratio and gamma correction.
|
37
|
# @param left_image The left image to be blended.
|
38
|
# @param right_image The right image to be blended.
|
39
|
# @return A tuple containing the blended left and right images.
|
40
|
#
|
41
|
def blend_images(self, left_image, right_image):
|
42
|
# ˅
|
43
|
height, width, _ = left_image.shape
|
44
|
blend_width = int(self.blend_ratio * width)
|
45
|
|
46
|
alpha_left = np.linspace(1, 0, blend_width).reshape(1, blend_width, 1)
|
47
|
alpha_left = np.repeat(alpha_left, height, axis=0)
|
48
|
alpha_right = np.linspace(0, 1, blend_width).reshape(1, blend_width, 1)
|
49
|
alpha_right = np.repeat(alpha_right, height, axis=0)
|
50
|
|
51
|
left_overlap = left_image[:, -blend_width:]
|
52
|
right_overlap = right_image[:, :blend_width]
|
53
|
gamma_corrected_left_overlap = self.gamma_corrector.gamma_correction(left_overlap)
|
54
|
gamma_corrected_right_overlap = self.gamma_corrector.gamma_correction(right_overlap)
|
55
|
|
56
|
left_image[:, -blend_width:] = gamma_corrected_left_overlap
|
57
|
right_image[:, :blend_width] = gamma_corrected_right_overlap
|
58
|
|
59
|
left_image[:, -blend_width:] = (left_image[:, -blend_width:].astype(np.float32) * alpha_left).astype(np.uint8)
|
60
|
right_image[:, :blend_width] = (right_image[:, :blend_width].astype(np.float32) * alpha_right).astype(np.uint8)
|
61
|
|
62
|
return left_image, right_image
|
63
|
# ˄
|
64
|
|
65
|
##
|
66
|
# @fn process_images(self, left_image_path, right_image_path)
|
67
|
# @brief Processes two images by applying gamma correction and blending them.
|
68
|
# @param left_image_path Path to the left image.
|
69
|
# @param right_image_path Path to the right image.
|
70
|
# @return A tuple of adjusted left, adjusted right, and blended images.
|
71
|
# @exception FileNotFoundError If one of the input images is not found.
|
72
|
#
|
73
|
def process_images(self, left_image_path, right_image_path):
|
74
|
# ˅
|
75
|
left_image = cv.imread(left_image_path, cv.IMREAD_COLOR)
|
76
|
right_image = cv.imread(right_image_path, cv.IMREAD_COLOR)
|
77
|
|
78
|
if left_image is None or right_image is None:
|
79
|
raise FileNotFoundError("One of the input images is not found.")
|
80
|
|
81
|
gamma_corrected_left = self.gamma_corrector.gamma_correction(left_image)
|
82
|
gamma_corrected_right = self.gamma_corrector.gamma_correction(right_image)
|
83
|
|
84
|
adjusted_left, adjusted_right = self.blend_images(gamma_corrected_left, gamma_corrected_right)
|
85
|
|
86
|
height, width, _ = left_image.shape
|
87
|
blend_width = int(self.blend_ratio * width)
|
88
|
|
89
|
left_non_overlap = adjusted_left[:, :-blend_width]
|
90
|
right_non_overlap = adjusted_right[:, blend_width:]
|
91
|
|
92
|
left_blend = adjusted_left[:, -blend_width:]
|
93
|
right_blend = adjusted_right[:, :blend_width]
|
94
|
blended_area = left_blend + right_blend
|
95
|
|
96
|
blended_image = np.hstack((left_non_overlap, blended_area, right_non_overlap))
|
97
|
|
98
|
return adjusted_left, adjusted_right, blended_image
|
99
|
# ˄
|
100
|
|
101
|
# ˅
|
102
|
|
103
|
# ˄
|