I am learning about dimension reduction techniques in R. I take one image as input and I have reduced dimension using svd using this code
library(raster)
img <- raster("C:/Users/***/Pictures/pansy.jpg")
img_flip <- flip(img, direction = "y")
img <- t(as.matrix(img_flip))
dim(img)
image(img,col=grey(seq(0,1,length=256))) # Actual
img_svd <- svd(img)
u <- img_svd$u
d <- diag(img_svd$d)
v <- img_svd$v
u1 <- as.matrix(u[, 1:50])
d1 <- as.matrix(d[1:50, 1:50])
v1 <- as.matrix(v[, 1:50])
photo1 <- u1 %*% d1 %*% t(v1)
image(photo1, col = grey(seq(0, 1, length = 256))) #Reduced
After that, I got Output Like this
Then I learned about Random Projection to compare with svd, I followed the below steps for the same Image
- Convert the image into matrix [465 X 600]
- Create a Random matrix of size [600 X 300] filled with +1 and -1 and equal probability
- Multiply both the matrix and I get a matrix of size [465 X 300]
Code:
rp_img <- raster("C:/Users/***/Pictures/pansy.jpg")
img_flip <- flip(rp_img, direction = "y")
rp_img <- t(as.matrix(img_flip))
rm <- form_sparse_matrix(600,n_cols = ncol(rp_img),TRUE,0.5,method = "probability") # To create a random matrix with +1 and -1 of equal probability
photo2 <- rp_img %*% rm
image(photo2, col = grey(seq(0, 1, length = 256)))
After that, I view the image, It show like below
Whether my steps for performing Random projection is correct or wrong? Where have I done a mistake?