Created
March 30, 2022 20:35
-
-
Save nazarovsky/7d06e03cc8b34b07ce9ce316990e6cb0 to your computer and use it in GitHub Desktop.
Calculate mean and var values for all images in the folder by combining mean and var of individual images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# variance calculation algorithm for two datasets of different lengths is taken from | |
# Chan, Tony F.; Golub, Gene H.; LeVeque, Randall J. (1979), "Updating Formulae and a Pairwise Algorithm for Computing Sample Variances." (PDF), Technical Report STAN-CS-79-773, Department of Computer Science, Stanford University. | |
# http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf | |
import os, sys | |
import numpy as np | |
import tifffile as tiff # pip install tifffile | |
TIF_FOLDER = 'img' | |
EXT = '.tiff' | |
def load_tiff(filename): | |
image = tiff.imread(filename) | |
imin, imax, imean, imed, istd, n = np.min(image), np.max(image), np.mean(image), np.median(image), np.std(image), image.size | |
return imin, imax, imean, imed, istd, n | |
def calculate_block_mean_and_var(MEANS,VARS,NS): | |
TS = np.multiply(MEANS,NS) | |
SS = np.multiply(VARS,NS) | |
TA = TS[0] | |
NA = NS[0] | |
SA = SS[0] | |
for i in range(1,len(SS),1): | |
NB = NS[i] | |
TB = TS[i] | |
SB = SS[i] | |
TAB = TA + TB | |
SAB = SA + SB + NA/(NB*(NA+NB)) * (NB/NA*TA-TB)**2 | |
NA = NA + NB | |
TA = TAB | |
SA = SAB | |
MAB = TAB/np.sum(NS) | |
VAB = SAB/np.sum(NS) | |
return MAB,VAB | |
FLIST = [] | |
for root, folders, files in os.walk(TIF_FOLDER): | |
for f in files: | |
if(f.endswith(EXT)): | |
FLIST.append(f) | |
# flattened true version | |
FL = np.array([],dtype=np.float64) | |
FL_SIZE = 0 | |
IMGS = [] | |
for f in FLIST: | |
# print(f) | |
img = tiff.imread(os.path.join(TIF_FOLDER,f)) | |
IMGS.append(img) | |
fl = img.flatten().astype(np.float64) | |
FL_SIZE += fl.size | |
FL = np.concatenate([FL,fl]) | |
MA_TRUE = np.mean(FL) | |
VA_TRUE = np.var(FL) | |
print('TRUE MEAN = {}'.format(MA_TRUE)) | |
print('TRUE VAR = {}'.format(VA_TRUE)) | |
NS = [] | |
IMEANS = [] | |
IVARS = [] | |
for img in IMGS: | |
imean, ivar, n = np.mean(img), np.var(img), img.size | |
NS.append(n) | |
IMEANS.append(imean) | |
IVARS.append(ivar) | |
MAB,VAB = calculate_block_mean_and_var(IMEANS,IVARS,NS) | |
print('BLOCK MEAN = {} delta = {}'.format(MAB,MAB-MA_TRUE)) | |
print('BLOCK VAR = {} delta = {}'.format(VAB,VAB-VA_TRUE)) | |
print('WRONG MEAN = {}'.format(np.mean(IMEANS))) | |
print('WRONG VAR = {}'.format(np.mean(IVARS))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment