Last active
April 9, 2020 16:08
-
-
Save JBed/5673060beac474805e38 to your computer and use it in GitHub Desktop.
1/f whitening for large natural images
This file contains 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
#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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?