Project

General

Profile

Codes » main_stitcher.py

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

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
from config import Config
4
from img_processor import ImgProcessor
5
from display_img import DisplayImg
6
import cv2
7

    
8
class MainStitcher:
9
    """!
10
    @brief A class responsible for stitching and displaying images in a projection system.
11

    
12
    The class integrates configuration settings from the `Config` class and display properties
13
    from the `DisplayImg` class to handle the main stitching and display logic.
14
    """
15

    
16
    def __init__(self, dImg):
17
        """!
18
        @brief Initializes the MainStitcher with configuration and display image settings.
19
        @param config An instance of the Config class containing projection configuration.
20
        @param dImg An instance of the DisplayImg class containing display settings.
21
        """
22
        self.__displayImg = dImg
23

    
24

    
25
    #for single monitor/mirroring
26
    def singleDisplay(self):
27
        """!
28
        @brief use two laptops to project the final processed images.
29
        """
30
        img = self.__displayImg.getImg()
31
        side = self.__displayImg.getSide()
32

    
33
        cv2.namedWindow(side, cv2.WINDOW_NORMAL)
34
        cv2.imshow(side, img)
35
        cv2.setWindowProperty(side, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
36

    
37

    
38
    #for double extended monitor
39
    def doubleDisplay(self, monitorWidth):
40
        """!
41
        @brief use two laptops to project the final processed images.
42
        """
43
        img = self.__displayImg.getImg()
44
        side = self.__displayImg.getSide()
45

    
46
        """
47
        In moveWindow(name, x, y), we can replace x with the window size or something
48
        """
49
        if(side == "Left"):
50
            cv2.namedWindow(side, cv2.WINDOW_NORMAL)
51
            cv2.moveWindow(side, 0, 0)
52
            cv2.imshow(side, img)
53
            cv2.resizeWindow(side, monitorWidth, 1080)
54
            cv2.setWindowProperty(side, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
55

    
56
        elif(side == "Right"):
57
            cv2.namedWindow(side, cv2.WINDOW_NORMAL)
58
            cv2.moveWindow(side, monitorWidth, 0)
59
            cv2.imshow(side, img)
60
            cv2.resizeWindow(side, monitorWidth, 1080)
61
            cv2.setWindowProperty(side, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
62

    
63
    def save(self):
64
        cv2.imwrite("./img/processed/" + self.__displayImg.getSide() + "_img.png", self.__displayImg.getImg())
65

    
66

    
67
def main():
68
    config = Config.readConfigFile()
69

    
70
    imgProcessor = ImgProcessor(config)
71

    
72
    l_img, r_img = imgProcessor.cropImage()
73
    l_alpha_processed, r_alpha_processed = imgProcessor.alphaBlend(l_img, r_img)
74

    
75

    
76
    l_alpha_gamma_processed = imgProcessor.GammaCorrection(l_alpha_processed)
77
    r_alpha_gamma_processed = imgProcessor.GammaCorrection(r_alpha_processed)
78

    
79
    l_displayImg = DisplayImg(l_alpha_gamma_processed.shape[1] ,l_alpha_gamma_processed.shape[0], config.getOverlapWidth(), l_alpha_gamma_processed, "Left")
80
    r_displayImg = DisplayImg(r_alpha_gamma_processed.shape[1] ,r_alpha_gamma_processed.shape[0], config.getOverlapWidth(), r_alpha_gamma_processed, "Right")
81

    
82
    l_stitcher = MainStitcher(l_displayImg)
83
    r_stitcher = MainStitcher(r_displayImg)
84

    
85

    
86
    print(config.getIsDualMonitor())
87

    
88
    if config.getIsDualMonitor():
89
        l_stitcher.doubleDisplay(config.getMonitorWidth())
90
        r_stitcher.doubleDisplay(config.getMonitorWidth())
91
    else:
92
        if config.getSide().lower() == "right":
93
            r_stitcher.singleDisplay()
94
        else:
95
            l_stitcher.singleDisplay()
96

    
97
    cv2.waitKey(0)
98
    cv2.destroyAllWindows()
99

    
100

    
101
    l_stitcher.save()
102
    r_stitcher.save()
103

    
104
if __name__ == "__main__":
105
    main()
(5-5/5)