File size: 3,690 Bytes
7b41155 f513d1e 7b41155 f513d1e 7b41155 d461a84 7b41155 d461a84 e78a26e 80bc771 d461a84 80bc771 d461a84 7b41155 f513d1e 7b41155 f513d1e d461a84 f513d1e 7b41155 f513d1e 7b41155 f513d1e 61e169d f513d1e 7b41155 f513d1e 7b41155 f513d1e 61e169d f513d1e d461a84 7b41155 f513d1e d461a84 7b41155 e78a26e d461a84 e78a26e d461a84 9945008 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 | import gradio as gr
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
from skimage import color
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):
# 確保圖片格式為 RGB
if image.shape[-1] == 4: # 如果圖片有 alpha 通道
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
# 調整 R、G、B 通道的範圍
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("本應用程式展示了使用 OpenCV 實現的影像處理功能,包括動態調整 RGB 範圍的功能。")
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
)
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
)
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
)
return demo
# 啟動應用程式
if __name__ == "__main__":
app().launch()
|