Skip to content

Instantly share code, notes, and snippets.

@JBed
Last active April 9, 2020 16:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBed/5673060beac474805e38 to your computer and use it in GitHub Desktop.
Save JBed/5673060beac474805e38 to your computer and use it in GitHub Desktop.
1/f whitening for large natural images
#PCA whitening involves finding the inverse square root of the covariance matrix
#of a set of observations, which is prohibitively expensive when dealing
#with natural images
#starting with a path to a single image (img_path)
import numpy as np
from PIL import Image
from sklearn import preprocessing
def f_whitening(img_path):
img = Image.open(open(img_path))
img = np.asarray(img, dtype='uint8').transpose(2, 0, 1)
whitened = []
for ii in range(3):#loop over color channels
ch = img[ii] - img[ii].mean()#center the data
aa = np.fft.fft2(a)
spectr = np.sqrt(np.mean(np.dot(abs(aa),abs(aa))))
out = np.fft.ifft2(np.dot(aa,1./spectr))
whitened.append(preprocessing.scale(abs(out)))
return np.asarray(whitened)
@guillaumefrd
Copy link

Thanks for this gist!
I spotted a small error line 17, it should be aa = np.fft.fft2(ch)
Also, do you have any ressource that could explain your code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment