File size: 4,755 Bytes
7b41155 7ceff64 7b41155 f513d1e 7b41155 f513d1e 7b41155 d461a84 e78a26e 80bc771 7ceff64 d461a84 7ceff64 80bc771 d461a84 7b41155 f513d1e 7b41155 f513d1e 7ceff64 f513d1e 7ceff64 f513d1e 7b41155 96c568f f513d1e 7b41155 f513d1e 61e169d 96c568f 7ceff64 96c568f 7ceff64 f513d1e 7b41155 96c568f f513d1e 7b41155 f513d1e 61e169d 7ceff64 96c568f bc3cc8a 96c568f 7ceff64 f513d1e 7ceff64 d461a84 7b41155 96c568f d461a84 7b41155 88fd8df d461a84 e78a26e d461a84 9945008 7ceff64 96c568f d4df246 96c568f 7ceff64 f513d1e 7b41155 f513d1e 7b41155 7842fd9 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import gradio as gr
from skimage import color
import cv2
import numpy as np
# 邊緣檢測函式
def edge_detection(image, threshold1, threshold2):
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
edges = cv2.Canny(gray_image, threshold1, threshold2)
return edges
# 圖像分割函式
def image_segmentation(image, compactness):
from skimage.segmentation import slic
segments = slic(image, compactness=compactness, n_segments=200)
return color.label2rgb(segments, image, kind='avg')
# 顏色範圍調整函式
def adjust_color(image, r_min, r_max, g_min, g_max, b_min, b_max):
if image.shape[-1] == 4: # 如果圖片有 alpha 通道
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
image = image.astype(np.float32)
image[:, :, 0] = np.clip(image[:, :, 0], r_min, r_max) # 調整 R 通道
image[:, :, 1] = np.clip(image[:, :, 1], g_min, g_max) # 調整 G 通道
image[:, :, 2] = np.clip(image[:, :, 2], b_min, b_max) # 調整 B 通道
image = image.astype(np.uint8)
return image
# 主應用程式
def app():
with gr.Blocks() as demo:
gr.Markdown("# 影像處理功能展示")
gr.Markdown("本應用程式展示了邊緣檢測、圖像分割和顏色範圍調整功能,每個功能附帶範例圖片和測試結果。")
# 邊緣檢測功能
with gr.Tab("邊緣檢測"):
with gr.Row():
input_image = gr.Image(label="輸入圖片")
edge_result = gr.Image(label="邊緣檢測結果")
with gr.Row():
threshold1 = gr.Slider(0, 255, value=100, step=1, label="閾值1")
threshold2 = gr.Slider(0, 255, value=200, step=1, label="閾值2")
edge_button = gr.Button("執行邊緣檢測")
edge_button.click(
fn=edge_detection,
inputs=[input_image, threshold1, threshold2],
outputs=edge_result
)
# 顯示範例圖片和直接測試結果
gr.Examples(
examples=[
["simple_plain_pikachu_by_titanplakinside_dexx027.png", 100, 200]
],
inputs=[input_image, threshold1, threshold2],
outputs=edge_result,
fn=edge_detection,
cache_examples=True
)
# 圖像分割功能
with gr.Tab("圖像分割"):
with gr.Row():
input_image = gr.Image(label="輸入圖片")
seg_result = gr.Image(label="分割結果")
with gr.Row():
compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
seg_button = gr.Button("執行圖像分割")
seg_button.click(
fn=image_segmentation,
inputs=[input_image, compactness],
outputs=seg_result
)
gr.Examples(
examples=[
["simple_plain_pikachu_by_titanplakinside_dexx027.png", 100]
],
inputs=[input_image, compactness],
outputs=seg_result,
fn=image_segmentation,
cache_examples=True
)
# 顏色範圍調整功能
with gr.Tab("顏色範圍調整"):
with gr.Row():
input_image = gr.Image(label="輸入圖片")
adjusted_result = gr.Image(label="調整後的圖片")
with gr.Row():
r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
r_max = gr.Slider(0, 255, value=255, step=1, label="R 最大值")
g_min = gr.Slider(0, 255, value=0, step=1, label="G 最小值")
g_max = gr.Slider(0, 255, value=255, step=1, label="G 最大值")
b_min = gr.Slider(0, 255, value=0, step=1, label="B 最小值")
b_max = gr.Slider(0, 255, value=255, step=1, label="B 最大值")
adjust_button = gr.Button("調整顏色範圍")
adjust_button.click(
fn=adjust_color,
inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
outputs=adjusted_result
)
gr.Examples(
examples=[
["simple_plain_pikachu_by_titanplakinside_dexx027.png", 0, 255, 0, 255, 255, 255]
],
inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
outputs=adjusted_result,
fn=adjust_color,
cache_examples=True
)
return demo
# 啟動應用程式
if __name__ == "__main__":
app().launch()
|