3

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

Actual Reduced

Then I learned about Random Projection to compare with svd, I followed the below steps for the same Image

  1. Convert the image into matrix [465 X 600]
  2. Create a Random matrix of size [600 X 300] filled with +1 and -1 and equal probability
  3. 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 After Random Projection

Whether my steps for performing Random projection is correct or wrong? Where have I done a mistake?

Siddhu
  • 1,188
  • 2
  • 14
  • 24
  • 2
    the spatial correlation in the image is not retained after projection – Sandipan Dey Jan 13 '17 at 12:06
  • Your code uses the function `form_sparse_matrix`. I cannot find that in any package. Is that a function that you wrote? Also, I notice that your SVD "reduced dimension" object photo1 takes up twice as much space as the original img – G5W Jan 20 '17 at 19:37
  • form_sparse_matrix() is user defined function , – Siddhu Jan 23 '17 at 06:38

0 Answers0