File size: 3,789 Bytes
1dd0038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
import cv2
import numpy as np

def exposure_fusion(images):
    try:
        # Convert PIL images (RGB) to OpenCV format (BGR)
        images_cv = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]
        
        # Align images using AlignMTB
        align_mtb = cv2.createAlignMTB()
        aligned_images = images_cv.copy()
        align_mtb.process(images_cv, aligned_images)
        
        # Merge images using exposure fusion (Mertens)
        merge_mertens = cv2.createMergeMertens()
        fused = merge_mertens.process(aligned_images)
        
        # Convert result from float32 to uint8 and back to RGB
        fused = np.clip(fused * 255, 0, 255).astype('uint8')
        fused = cv2.cvtColor(fused, cv2.COLOR_BGR2RGB)
        return fused
    except Exception as e:
        return f"Error: {e}"

def stabilize_crop_and_exposure_fusion(images):
    try:
        # Convert images from PIL (RGB) to OpenCV format (BGR)
        images_cv = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]

        # Align images using AlignMTB
        align_mtb = cv2.createAlignMTB()
        aligned_images = images_cv.copy()
        align_mtb.process(images_cv, aligned_images)

        # Determine valid regions in each image (to remove black borders)
        bounding_rects = []
        for img in aligned_images:
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # Pixels above a small threshold are considered valid
            _, mask = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
            coords = cv2.findNonZero(mask)
            if coords is not None:
                x, y, w, h = cv2.boundingRect(coords)
                bounding_rects.append((x, y, w, h))
            else:
                bounding_rects.append((0, 0, img.shape[1], img.shape[0]))

        # Compute the common intersection rectangle
        if not bounding_rects:
            return "No valid images provided."
        x_min, y_min, w, h = bounding_rects[0]
        x_max = x_min + w
        y_max = y_min + h
        for (x, y, w, h) in bounding_rects[1:]:
            x_min = max(x_min, x)
            y_min = max(y_min, y)
            x_max = min(x_max, x + w)
            y_max = min(y_max, y + h)
        if x_max <= x_min or y_max <= y_min:
            return "Images do not overlap enough for cropping."

        # Crop each aligned image to the intersection region
        cropped_images = [img[y_min:y_max, x_min:x_max] for img in aligned_images]

        # Merge the cropped images using exposure fusion (Mertens)
        merge_mertens = cv2.createMergeMertens()
        fused = merge_mertens.process(cropped_images)
        fused = np.clip(fused * 255, 0, 255).astype('uint8')
        fused = cv2.cvtColor(fused, cv2.COLOR_BGR2RGB)
        return fused
    except Exception as e:
        return f"Error: {e}"

def process_images(images, advanced):
    if not images:
        return None
    # If advanced option is selected, use stabilization & cropping before fusion.
    if advanced:
        return stabilize_crop_and_exposure_fusion(images)
    else:
        return exposure_fusion(images)

# Gradio Interface: Upload multiple images and choose the processing method.
inputs = [
    gr.File(type="file", label="Upload Images", file_count="multiple"),
    gr.Checkbox(label="Advanced: Stabilize & Crop Before Fusion", value=False)
]

iface = gr.Interface(
    fn=process_images,
    inputs=inputs,
    outputs="image",
    title="Exposure Fusion with Stabilization",
    description=(
        "Upload multiple images with varying exposures. "
        "If 'Advanced: Stabilize & Crop Before Fusion' is selected, "
        "the app aligns the images, crops out extra borders, then fuses them."
    ),
)

iface.launch()