Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
-
|
5 |
matrices = {
|
6 |
-
'true': [
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
}
|
12 |
|
13 |
def make_anaglyph(left_img, right_img, color_method):
|
@@ -24,14 +50,14 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
24 |
# Resize right image to match left image dimensions
|
25 |
right = right.resize(left.size, Image.LANCZOS)
|
26 |
|
27 |
-
# Create a
|
28 |
-
|
29 |
|
30 |
# Get the pixel maps
|
31 |
width, height = left.size
|
32 |
leftMap = left.load()
|
33 |
rightMap = right.load()
|
34 |
-
|
35 |
|
36 |
# Use the selected color matrix
|
37 |
m = matrices[color_method]
|
@@ -41,14 +67,15 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
41 |
for x in range(0, width):
|
42 |
r1, g1, b1 = leftMap[x, y]
|
43 |
r2, g2, b2 = rightMap[x, y]
|
44 |
-
|
|
|
45 |
int(r1*m[0][0] + g1*m[0][1] + b1*m[0][2] + r2*m[1][0] + g2*m[1][1] + b2*m[1][2]),
|
46 |
int(r1*m[0][3] + g1*m[0][4] + b1*m[0][5] + r2*m[1][3] + g2*m[1][4] + b2*m[1][5]),
|
47 |
int(r1*m[0][6] + g1*m[0][7] + b1*m[0][8] + r2*m[1][6] + g2*m[1][7] + b2*m[1][8])
|
48 |
)
|
49 |
|
50 |
# Convert back to numpy array for Gradio
|
51 |
-
return np.array(
|
52 |
|
53 |
def make_stereopair(left_img, right_img, color_method):
|
54 |
"""Generate a stereo pair from left and right images"""
|
@@ -124,7 +151,7 @@ with gr.Blocks(css=css) as app:
|
|
124 |
)
|
125 |
|
126 |
color_method = gr.Radio(
|
127 |
-
["optimized", "true", "mono", "color", "halfcolor"],
|
128 |
label="Color Method",
|
129 |
value="optimized",
|
130 |
info="Select the color processing method"
|
|
|
1 |
import gradio as gr
|
2 |
+
# Modified matrices with improved red/cyan separation
|
|
|
|
|
3 |
matrices = {
|
4 |
+
'true': [
|
5 |
+
[0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0],
|
6 |
+
[0, 0, 0, 0, 0, 0, 0.299, 0.587, 0.114]
|
7 |
+
],
|
8 |
+
'mono': [
|
9 |
+
[0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0],
|
10 |
+
[0, 0, 0, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114]
|
11 |
+
],
|
12 |
+
'color': [
|
13 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0],
|
14 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 1]
|
15 |
+
],
|
16 |
+
'halfcolor': [
|
17 |
+
[0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0],
|
18 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 1]
|
19 |
+
],
|
20 |
+
'optimized': [
|
21 |
+
[0, 0.7, 0.3, 0, 0, 0, 0, 0, 0],
|
22 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 1]
|
23 |
+
],
|
24 |
+
# New matrices with improved red/cyan separation
|
25 |
+
'enhanced': [
|
26 |
+
[0.9, 0, 0, 0, 0, 0, 0, 0, 0], # Left image: strong red only
|
27 |
+
[0, 0, 0, 0, 0.95, 0, 0, 0, 0.95] # Right image: strong green and blue
|
28 |
+
],
|
29 |
+
'true_enhanced': [
|
30 |
+
[0.4, 0.5, 0.1, 0, 0, 0, 0, 0, 0], # Left image: red channel from grayscale
|
31 |
+
[0, 0, 0, 0, 0, 0, 0, 0.6, 0.4] # Right image: only blue and green
|
32 |
+
],
|
33 |
+
'dark_red': [
|
34 |
+
[0.7, 0, 0, 0, 0, 0, 0, 0, 0], # Left image: slightly reduced red
|
35 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 1] # Right image: full green and blue
|
36 |
+
]
|
37 |
}
|
38 |
|
39 |
def make_anaglyph(left_img, right_img, color_method):
|
|
|
50 |
# Resize right image to match left image dimensions
|
51 |
right = right.resize(left.size, Image.LANCZOS)
|
52 |
|
53 |
+
# Create a new image for the result (important change)
|
54 |
+
result = Image.new("RGB", left.size)
|
55 |
|
56 |
# Get the pixel maps
|
57 |
width, height = left.size
|
58 |
leftMap = left.load()
|
59 |
rightMap = right.load()
|
60 |
+
resultMap = result.load()
|
61 |
|
62 |
# Use the selected color matrix
|
63 |
m = matrices[color_method]
|
|
|
67 |
for x in range(0, width):
|
68 |
r1, g1, b1 = leftMap[x, y]
|
69 |
r2, g2, b2 = rightMap[x, y]
|
70 |
+
|
71 |
+
resultMap[x, y] = (
|
72 |
int(r1*m[0][0] + g1*m[0][1] + b1*m[0][2] + r2*m[1][0] + g2*m[1][1] + b2*m[1][2]),
|
73 |
int(r1*m[0][3] + g1*m[0][4] + b1*m[0][5] + r2*m[1][3] + g2*m[1][4] + b2*m[1][5]),
|
74 |
int(r1*m[0][6] + g1*m[0][7] + b1*m[0][8] + r2*m[1][6] + g2*m[1][7] + b2*m[1][8])
|
75 |
)
|
76 |
|
77 |
# Convert back to numpy array for Gradio
|
78 |
+
return np.array(result)
|
79 |
|
80 |
def make_stereopair(left_img, right_img, color_method):
|
81 |
"""Generate a stereo pair from left and right images"""
|
|
|
151 |
)
|
152 |
|
153 |
color_method = gr.Radio(
|
154 |
+
["optimized", "true", "mono", "color", "halfcolor", "enhanced", "true_enhanced", "dark_red"],
|
155 |
label="Color Method",
|
156 |
value="optimized",
|
157 |
info="Select the color processing method"
|