Skip to content

Instantly share code, notes, and snippets.

@vankesteren
Last active April 20, 2018 09:52
Show Gist options
  • Select an option

  • Save vankesteren/bdbcdf6ba218fed1549e1a8d16195e34 to your computer and use it in GitHub Desktop.

Select an option

Save vankesteren/bdbcdf6ba218fed1549e1a8d16195e34 to your computer and use it in GitHub Desktop.
SVD images
library(imager)
img <- load.image("~/hearts.jpg")
mat <- img[,,1,1]
s <- svd(mat)
s$u%*%diag(s$d)%*%t(s$v)
lrd <- s$d
lrd[-c(1:15)] <- 0
newimg <- img
newimg[,,1,1] <- s$u%*%diag(lrd)%*%t(s$v)
par(mfrow = c(2,3))
plot(img, axes = FALSE, main = "original")
for (i in c(1,5,10,15,20)) {
lrd <- s$d
lrd[-c(1:i)] <- 0
newimg <- img
newimg[,,1,1] <- s$u%*%diag(lrd)%*%t(s$v)
plot(newimg, axes = FALSE, main = paste(i, "components"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment