xiaoming32236046 commited on
Commit
0821733
·
verified ·
1 Parent(s): c617fab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -72
app.py CHANGED
@@ -1,85 +1,45 @@
1
- import numpy as np
2
- import cv2
3
- import matplotlib.pyplot as plt
4
  import gradio as gr
5
- import tempfile
6
- import os
7
  from PIL import Image, ImageDraw
 
8
 
9
- def calculate_snr(channel, roi_points):
10
- # 创建信号和背景掩码
11
- mask = Image.new('L', (channel.shape[1], channel.shape[0]), 0)
12
- ImageDraw.Draw(mask).polygon(roi_points, outline=1, fill=1)
13
- signal_mask = np.array(mask)
14
-
15
- # 计算信号和背景区域
16
- signal = np.where(signal_mask, channel, 0)
17
- background = np.where(~signal_mask, channel, 0)
18
 
19
- # 计算信号区域的平均强度
20
- signal_mean = np.mean(signal[signal_mask == 1])
 
21
 
22
- # 计算背景区域的标准差
23
- background_std = np.std(background[signal_mask == 0])
 
24
 
25
- # 计算SNR
26
- snr = signal_mean / background_std if background_std != 0 else float('inf')
27
 
28
- return signal_mean, background_std, snr, signal, background
29
 
30
- def process_image(image, roi):
31
- # 将图像转换为numpy数组
32
- img_array = np.array(image)
 
33
 
34
- # 判断图像是单通道还是多通道
35
- if len(img_array.shape) == 2:
36
- channels = [img_array]
37
- channel_names = ['灰度']
38
- else:
39
- channels = cv2.split(img_array)
40
- channel_names = ['Red', 'Green', 'Blue']
41
 
 
42
  results = []
43
- result_images = []
44
-
45
- for i, (channel, name) in enumerate(zip(channels, channel_names)):
46
- signal_mean, background_std, snr, signal, background = calculate_snr(channel, roi)
47
-
48
- results.append(f"{name} channel:\n"
49
- f"信号平均强度(Signal): {signal_mean:.2f}\n"
50
- f"背景标准差(Noise): {background_std:.2f}\n"
51
- f"信噪比(SNR): {snr:.2f}\n")
52
-
53
- # 创建结果图像
54
- fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
55
- ax1.imshow(signal, cmap='gray')
56
- ax1.set_title(f'{name} Signal ROI')
57
- ax2.imshow(background, cmap='gray')
58
- ax2.set_title(f'{name} Background ROI')
59
-
60
- # 保存图像结果到临时文件
61
- result_filename = tempfile.mktemp(suffix='.png')
62
- plt.savefig(result_filename)
63
- plt.close(fig)
64
-
65
- result_images.append(result_filename)
66
-
67
- return "\n".join(results), result_images
68
 
69
- # 创建Gradio接口
70
- iface = gr.Interface(
71
- fn=process_image,
72
- inputs=[
73
- gr.Image(label="上传图像", type="pil"),
74
- gr.Image(label="绘制ROI", type="pil", tool="sketch", interactive=True)
75
- ],
76
- outputs=[
77
- gr.Textbox(label="结果"),
78
- gr.Gallery(label="ROI 图像")
79
- ],
80
- title="图像SNR计算器",
81
- description="上传一张图像,在第二个框中绘制感兴趣区域,然后点击提交按钮计算SNR。支持单通道和多通道图像。"
82
- )
83
 
84
- # 运行Gradio应用
85
- iface.launch()
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
 
3
  from PIL import Image, ImageDraw
4
+ import cv2
5
 
6
+ def calculate_snr(image, roi):
7
+ # Convert image to grayscale if it's not already
8
+ if len(image.shape) == 3:
9
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10
+ roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
 
 
 
 
11
 
12
+ # Create a mask for the ROI
13
+ mask = np.zeros_like(image)
14
+ cv2.fillPoly(mask, [roi], 255)
15
 
16
+ # Calculate signal and noise
17
+ signal = np.mean(image[mask > 0])
18
+ noise = np.std(image[mask == 0])
19
 
20
+ # Calculate SNR
21
+ snr = signal / noise
22
 
23
+ return signal, noise, snr
24
 
25
+ def process_image(input_image, roi_points):
26
+ # Load the input image
27
+ image = Image.open(input_image)
28
+ image = np.array(image)
29
 
30
+ # Load the ROI image
31
+ roi_image = Image.new('L', (image.shape[1], image.shape[0]))
32
+ draw = ImageDraw.Draw(roi_image)
33
+ draw.polygon(roi_points, fill=255)
34
+ roi_image = np.array(roi_image)
 
 
35
 
36
+ # Calculate SNR for each channel
37
  results = []
38
+ for i in range(image.shape[2]):
39
+ signal, noise, snr = calculate_snr(image[:, :, i], roi_image)
40
+ results.append((signal, noise, snr))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ return results, roi_image
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ iface = gr.Interface(fn=process_image, inputs=[gr.inputs.Image(), gr.inputs.Image()], outputs=[gr.outputs.Textbox(), gr.outputs.Image()], title="图像SNR计算器")
45
+ iface.launch()