I'm trying to implement ZCA Whitening algorithm like shown here: How to implement ZCA Whitening? Python with opencv in Scala (using Java api) but I cannot find the most of the functions used there (python with numpy).
So far I tried with this:
Loading image as Mat:
val input = Imgcodecs.imread(img)
Imgproc.cvtColor(input, input, Imgproc.COLOR_BGR2GRAY)
input.convertTo(input, CvType.CV_64FC1)
Then apply the algorithm:
//Covariance matrix
val covar, mean = new Mat()
Core.calcCovarMatrix(input, covar, mean, Core.COVAR_NORMAL | Core.COVAR_ROWS)
Core.divide(covar, new Scalar(input.rows - 1), covar)
//Singular Value Decomposition
val w, u, vt = new Mat()
Core.SVDecomp(covar, w, u, vt, Core.SVD_FULL_UV)
//#Whitening constant, it prevents division by zero
val epsilon = 1e-5
To implement the last trasformation
ZCAMatrix = np.dot(U, np.dot(np.diag(1.0/np.sqrt(S + epsilon)), U.T))
I tried with:
var ZCAMatrix = new Mat
Core.add(w, new Scalar(epsilon), ZCAMatrix)
Core.sqrt(ZCAMatrix, ZCAMatrix)
Core.divide(1.0, ZCAMatrix, ZCAMatrix)
Core.gemm(Mat.diag(ZCAMatrix), u.t, 1, new Mat, 0, ZCAMatrix)
Core.gemm(u, ZCAMatrix, 1, new Mat, 0, ZCAMatrix)
Then apply the trasformation to the original image:
val output = new Mat
Core.gemm(ZCAMatrix, input, 1, new Mat, 0, output)
The result of thei transformation is:
which is not exactly what it's supposed to be. Can someone help?