Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from skimage import color
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
|
|
@@ -12,39 +11,30 @@ def edge_detection(image, threshold1, threshold2):
|
|
| 12 |
# 圖像分割函式
|
| 13 |
def image_segmentation(image, compactness):
|
| 14 |
from skimage.segmentation import slic
|
|
|
|
| 15 |
segments = slic(image, compactness=compactness, n_segments=200)
|
| 16 |
return color.label2rgb(segments, image, kind='avg')
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
def
|
| 20 |
# 確保圖片格式為 RGB
|
| 21 |
if image.shape[-1] == 4: # 如果圖片有 alpha 通道
|
| 22 |
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
mask = (
|
| 31 |
-
(r_channel >= r_min) & (r_channel <= r_max) &
|
| 32 |
-
(g_channel >= g_min) & (g_channel <= g_max) &
|
| 33 |
-
(b_channel >= b_min) & (b_channel <= b_max)
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# 創建結果圖片,保留符合範圍的顏色,其他設為灰階
|
| 37 |
-
result = image.copy()
|
| 38 |
-
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 39 |
-
result[~mask] = np.stack([gray_image] * 3, axis=-1)[~mask]
|
| 40 |
-
|
| 41 |
-
return result
|
| 42 |
|
| 43 |
# 主應用程式
|
| 44 |
def app():
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("# 影像處理功能展示")
|
| 47 |
-
gr.Markdown("本應用程式展示了使用
|
| 48 |
|
| 49 |
with gr.Tab("邊緣檢測"):
|
| 50 |
with gr.Row():
|
|
@@ -73,10 +63,10 @@ def app():
|
|
| 73 |
outputs=seg_result
|
| 74 |
)
|
| 75 |
|
| 76 |
-
with gr.Tab("
|
| 77 |
with gr.Row():
|
| 78 |
input_image = gr.Image(label="輸入圖片")
|
| 79 |
-
|
| 80 |
with gr.Row():
|
| 81 |
r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
|
| 82 |
r_max = gr.Slider(0, 255, value=255, step=1, label="R 最大值")
|
|
@@ -84,11 +74,11 @@ def app():
|
|
| 84 |
g_max = gr.Slider(0, 255, value=255, step=1, label="G 最大值")
|
| 85 |
b_min = gr.Slider(0, 255, value=0, step=1, label="B 最小值")
|
| 86 |
b_max = gr.Slider(0, 255, value=255, step=1, label="B 最大值")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
fn=
|
| 90 |
inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
|
| 91 |
-
outputs=
|
| 92 |
)
|
| 93 |
|
| 94 |
return demo
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
| 11 |
# 圖像分割函式
|
| 12 |
def image_segmentation(image, compactness):
|
| 13 |
from skimage.segmentation import slic
|
| 14 |
+
from skimage import color
|
| 15 |
segments = slic(image, compactness=compactness, n_segments=200)
|
| 16 |
return color.label2rgb(segments, image, kind='avg')
|
| 17 |
|
| 18 |
+
# 顏色範圍調整函式
|
| 19 |
+
def adjust_color(image, r_min, r_max, g_min, g_max, b_min, b_max):
|
| 20 |
# 確保圖片格式為 RGB
|
| 21 |
if image.shape[-1] == 4: # 如果圖片有 alpha 通道
|
| 22 |
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 23 |
|
| 24 |
+
# 調整 R、G、B 通道的範圍
|
| 25 |
+
image = image.astype(np.float32) # 將圖像轉為浮點數進行處理
|
| 26 |
+
image[:, :, 0] = np.clip(image[:, :, 0], r_min, r_max) # 調整 R 通道
|
| 27 |
+
image[:, :, 1] = np.clip(image[:, :, 1], g_min, g_max) # 調整 G 通道
|
| 28 |
+
image[:, :, 2] = np.clip(image[:, :, 2], b_min, b_max) # 調整 B 通道
|
| 29 |
+
image = image.astype(np.uint8) # 轉回整數型圖像
|
| 30 |
|
| 31 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# 主應用程式
|
| 34 |
def app():
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
gr.Markdown("# 影像處理功能展示")
|
| 37 |
+
gr.Markdown("本應用程式展示了使用 OpenCV 實現的影像處理功能,包括動態調整 RGB 範圍的功能。")
|
| 38 |
|
| 39 |
with gr.Tab("邊緣檢測"):
|
| 40 |
with gr.Row():
|
|
|
|
| 63 |
outputs=seg_result
|
| 64 |
)
|
| 65 |
|
| 66 |
+
with gr.Tab("顏色範圍調整"):
|
| 67 |
with gr.Row():
|
| 68 |
input_image = gr.Image(label="輸入圖片")
|
| 69 |
+
adjusted_result = gr.Image(label="調整後的圖片")
|
| 70 |
with gr.Row():
|
| 71 |
r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
|
| 72 |
r_max = gr.Slider(0, 255, value=255, step=1, label="R 最大值")
|
|
|
|
| 74 |
g_max = gr.Slider(0, 255, value=255, step=1, label="G 最大值")
|
| 75 |
b_min = gr.Slider(0, 255, value=0, step=1, label="B 最小值")
|
| 76 |
b_max = gr.Slider(0, 255, value=255, step=1, label="B 最大值")
|
| 77 |
+
adjust_button = gr.Button("調整顏色範圍")
|
| 78 |
+
adjust_button.click(
|
| 79 |
+
fn=adjust_color,
|
| 80 |
inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
|
| 81 |
+
outputs=adjusted_result
|
| 82 |
)
|
| 83 |
|
| 84 |
return demo
|