Compute mean and standard deviation (std) per channel give a list of images
"""Give a list of image filenames, this script compute the mean and std over all the images. | |
Requires: | |
* OpenCV | |
* tqdm | |
In this example, I use `Stanford Dogs Datasets" (~20k images) | |
Example Outputs: (8 cores CPU i7 4970K) | |
(env) dat@desktop:****/StanfordDogs$ python compute_mean_std.py | |
100%|██████████████████████████████████████████████████████████| 20580/20580 [01:06<00:00, 308.06it/s] | |
[0.4761392 0.45182742 0.39101657] [0.23364353 0.2289059 0.22732813] | |
"""" | |
import os | |
import cv2 | |
import tqdm | |
import multiprocessing as mp | |
import scipy.io as sio # read matlab file | |
import numpy as np | |
def compute_func(filename): | |
img = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) / 255. | |
return np.mean(img, (0, 1)), np.std(img, (0, 1)) | |
def main(): | |
# Read filenames | |
matfile = sio.loadmat('file_list.mat') | |
fnames = [os.path.join('Images', str(i[0][0])) for i in matfile['file_list']] | |
with mp.Pool(mp.cpu_count()) as pool: | |
result = list(tqdm.tqdm(pool.imap( | |
func=compute_func, | |
iterable=[i for i in fnames]), total=len(fnames))) | |
mean, std = zip(*result) | |
print(np.mean(mean, 0), np.mean(std, 0)) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment