PBL4_2
Loading...
Searching...
No Matches
imageBlender.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4
8
9import cv2 as cv
10import numpy as np
11from gammaCorrector import GammaCorrector
12
13
18 # ˅
19
20 # ˄
21
22
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
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
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 # ˄
A class for blending two images using a specified blend ratio and gamma correction.
process_images(self, left_image_path, right_image_path)
Processes two images by applying gamma correction and blending them.
__init__(self, gamma_corrector, blend_ratio=0.21)
Constructor for ImageBlender.
blend_images(self, left_image, right_image)
Blends two images together using the blend ratio and gamma correction.