-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompute_mean_std.py
33 lines (26 loc) · 1.05 KB
/
compute_mean_std.py
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
import os
from PIL import Image
import numpy as np
def main():
img_channels = 3
img_dir = "./DRIVE/training/images"
roi_dir = "./DRIVE/training/mask"
assert os.path.exists(img_dir), f"image dir: '{img_dir}' does not exist."
assert os.path.exists(roi_dir), f"roi dir: '{roi_dir}' does not exist."
img_name_list = [i for i in os.listdir(img_dir) if i.endswith(".tif")]
cumulative_mean = np.zeros(img_channels)
cumulative_std = np.zeros(img_channels)
for img_name in img_name_list:
img_path = os.path.join(img_dir, img_name)
ori_path = os.path.join(roi_dir, img_name.replace(".tif", "_mask.gif"))
img = np.array(Image.open(img_path)) / 255.
roi_img = np.array(Image.open(ori_path).convert('L'))
img = img[roi_img == 255]
cumulative_mean += img.mean(axis=0)
cumulative_std += img.std(axis=0)
mean = cumulative_mean / len(img_name_list)
std = cumulative_std / len(img_name_list)
print(f"mean: {mean}")
print(f"std: {std}")
if __name__ == '__main__':
main()