AaronWu901225 commited on
Commit
e78a26e
·
verified ·
1 Parent(s): 80bc771

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -16,30 +16,27 @@ def image_segmentation(image, compactness):
16
  return color.label2rgb(segments, image, kind='avg')
17
 
18
  # 色彩濾鏡函式
19
- def color_filter(image, color_choice):
20
- # 將圖像轉換為 HSV 格式
21
- hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
 
22
 
23
- # 定義顏色範圍(H: 色調,S: 飽和度,V: 明度)
24
- if color_choice == "紅色":
25
- lower_bound = np.array([0, 50, 50]) # 紅色範圍
26
- upper_bound = np.array([10, 255, 255])
27
- elif color_choice == "綠色":
28
- lower_bound = np.array([40, 50, 50]) # 綠色範圍
29
- upper_bound = np.array([80, 255, 255])
30
- elif color_choice == "藍色":
31
- lower_bound = np.array([100, 50, 50]) # 藍色範圍
32
- upper_bound = np.array([140, 255, 255])
33
- else:
34
- return "未支援的顏色範圍選擇"
35
 
36
- # 創建遮罩,過濾選定的顏色範圍
37
- mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
 
 
 
 
38
 
39
- # 保留選定顏色,將其他部分設為灰階
40
  result = image.copy()
41
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
42
- result[mask == 0] = np.stack([gray_image] * 3, axis=-1)[mask == 0]
43
 
44
  return result
45
 
@@ -76,21 +73,33 @@ def app():
76
  outputs=seg_result
77
  )
78
 
79
- with gr.Tab("色彩濾鏡"):
 
 
 
 
80
  with gr.Row():
81
  input_image = gr.Image(label="輸入圖片")
82
  filter_result = gr.Image(label="濾鏡結果")
83
  with gr.Row():
84
- color_choice = gr.Radio(["紅色", "綠色", "藍色"], label="選擇顏色", value="紅色")
 
 
 
 
 
85
  filter_button = gr.Button("應用濾鏡")
86
  filter_button.click(
87
- fn=color_filter,
88
- inputs=[input_image, color_choice],
89
  outputs=filter_result
90
  )
91
 
92
  return demo
93
 
 
 
 
94
  # 啟動應用程式
95
  if __name__ == "__main__":
96
  app().launch()
 
16
  return color.label2rgb(segments, image, kind='avg')
17
 
18
  # 色彩濾鏡函式
19
+ def color_filter_with_rgb(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
+ r_channel = image[:, :, 0]
26
+ g_channel = image[:, :, 1]
27
+ b_channel = image[:, :, 2]
 
 
 
 
 
 
 
 
28
 
29
+ # 建立遮罩,篩選符合 RGB 範圍的像素
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
 
 
73
  outputs=seg_result
74
  )
75
 
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("# 影像處理功能展示")
78
+ gr.Markdown("本應用程式展示了使用 OpenCV 實現的影像處理功能,包括手動調整 RGB 範圍的色彩濾鏡功能。")
79
+
80
+ with gr.Tab("色彩濾鏡 (手動調整 RGB)"):
81
  with gr.Row():
82
  input_image = gr.Image(label="輸入圖片")
83
  filter_result = gr.Image(label="濾鏡結果")
84
  with gr.Row():
85
+ r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
86
+ r_max = gr.Slider(0, 255, value=255, step=1, label="R 最大值")
87
+ g_min = gr.Slider(0, 255, value=0, step=1, label="G 最小值")
88
+ g_max = gr.Slider(0, 255, value=255, step=1, label="G 最大值")
89
+ b_min = gr.Slider(0, 255, value=0, step=1, label="B 最小值")
90
+ b_max = gr.Slider(0, 255, value=255, step=1, label="B 最大值")
91
  filter_button = gr.Button("應用濾鏡")
92
  filter_button.click(
93
+ fn=color_filter_with_rgb,
94
+ inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
95
  outputs=filter_result
96
  )
97
 
98
  return demo
99
 
100
+
101
+ return demo
102
+
103
  # 啟動應用程式
104
  if __name__ == "__main__":
105
  app().launch()