I am attempting to apply global contrast normalization (GCN) followed by ZCA whitening on a singular image. However, I'm finding that the final pixel values are too small (close to 0). This issue causes the outputted image to be completely black.
For ZCA, I am using the following code:
def flatten_matrix(matrix):
vector = matrix.flatten(1)
vector = vector.reshape(1, len(vector))
return vector
def zca_whitening(inputs):
sigma = np.dot(inputs, inputs.T)/inputs.shape[1] #Correlation matrix
U,S,V = np.linalg.svd(sigma) #Singular Value Decomposition
epsilon = 0.1 #Whitening constant, it prevents division by zero
ZCAMatrix = np.dot(np.dot(U, np.diag(1.0/np.sqrt(np.diag(S) + epsilon))), U.T) #ZCA Whitening matrix
return np.dot(ZCAMatrix, inputs) #Data whitening
So, to GCN and ZCA whiten an image, I am using the following code:
def testWhitenGCN():
img = cv2.imread(IMG) #openCV
flatten = flatten_matrix(img)
gcn = global_contrast_normalize(flatten, scale=55, sqrt_bias=10, use_std=True)
white_gcn = zca_whitening(gcn)
img_out = white_gcn.reshape(img.shape[0], img.shape[1], img.shape[2], order='F')
cv2.imwrite(OUT_IMG, img_out)
Note, if I perform one operation or the other without combining the two then the resulting image looks fine. The problem occurs when both operations are applied on the image one after another.
As far as choosing the params for the global_contrast_normalize method, I have no real intuition for the choices I made. Trial and error and info from this group led me to these values. These params seemed to work well when I performed this operation individually on an image.
Any suggestions on what I'm doing wrong or what I can try next would be greatly appreciated.
Thanks in advance.