-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_mean_std.py
More file actions
38 lines (29 loc) · 1.14 KB
/
Copy pathcompute_mean_std.py
File metadata and controls
38 lines (29 loc) · 1.14 KB
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
import os
from PIL import Image
import numpy as np
def main():
img_channels = 3
img_dir = "./kneedata/train_image"
roi_dir = "./kneedata/train_label"
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(".png")]
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(".png", ".png"))
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()
# kneedata
# mean: [0.39744121 0.39744121 0.39744121]
# std: [0.14962013 0.14962013 0.14962013]