Skip to content

Commit

Permalink
add script for patch mean & std calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
PingjunChen committed Sep 2, 2019
1 parent 37a3f96 commit 02057c0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions patches/cal_patch_mean_std.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

import os, sys
import numpy as np
from skimage import io
import pydaily

data_root = "../data/Patches"
tumor_type = "whole"
train_dir = os.path.join(data_root, tumor_type, 'train')
val_dir = os.path.join(data_root, tumor_type, 'val')


def get_mean_and_std(img_dir, suffix):
mean, std = np.zeros(3), np.zeros(3)
filelist = pydaily.filesystem.find_ext_files(img_dir, suffix)

for idx, filepath in enumerate(filelist):
cur_img = io.imread(filepath) / 255.0
for i in range(3):
mean[i] += np.mean(cur_img[:,:,i])
std[i] += cur_img[:,:,i].std()
mean = [ele * 1.0 / len(filelist) for ele in mean]
std = [ele * 1.0 / len(filelist) for ele in std]
return mean, std

rgb_mean, rgb_std = get_mean_and_std(os.path.join(train_dir, "imgs"), suffix=".jpg")
print("mean rgb: {}".format(rgb_mean))
print("std rgb: {}".format(rgb_std))

0 comments on commit 02057c0

Please sign in to comment.