Compresión con SVD

svd_greco

lo he creado con

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
library(png)

tmp.file <- tempfile()
download.file("http://datanalytics.com/uploads/greco.png", tmp.file)
m <- readPNG(tmp.file)

svd.m <- svd(m)

filtra.svd <- function(svd, k){
  tmp <- svd
  tmp$d[(k+1):length(tmp$d)] <- 0
  res <- tmp$u %*% diag(tmp$d) %*% t(tmp$v)

  res[res > 1] <- 1
  res[res < 0] <- 0

  plot(1:2, type='n', xlab = "",
        ylab = "", xaxt = "n", yaxt = "n",
        main = paste(k, "primeras componentes", sep = " "))
  rasterImage(res, 1, 1, 2, 2)
}

layout(matrix(1:9, 3, 3, byrow = T))

sapply(c(1,2,3,5,10,15,20,30,50),
        function(k) filtra.svd(svd.m, k))