Back to Part 1

Impressionist image

It turns out that for relevant images one can compress them in a similar way, so that they resemble impressionistic paintings. For a better comparison, apart from the compressed and original images, I also included an impressionist paintings in a similar style.

garden <- readJPEG('garden.jpg')

red_garden <- garden[,,1]
green_garden <- garden[,,2]
blue_garden <- garden[,,3]

pca_red_garden <- prcomp(red_garden, center = FALSE)
pca_green_garden <- prcomp(green_garden, center = FALSE)
pca_blue_garden <- prcomp(blue_garden, center = FALSE)

compressed_garden <- abind(pca_red_garden$x[,1:35] %*% t(pca_red_garden$rotation[,1:35]),
                        pca_green_garden$x[,1:35] %*% t(pca_green_garden$rotation[,1:35]),
                        pca_blue_garden$x[,1:35] %*% t(pca_blue_garden$rotation[,1:35]),
                        along = 3)
plot(image_read(compressed_garden))
title("Compressed image \n (35 Components)")

par(mfrow = c(1,2), mar = c(0,0.2,3,0.2))

plot(image_read(garden))
title("Original image")

plot(image_read(readJPEG('blooming_garden.jpg')))
title("Famous painting \n in similar style")
Original image source Famous painting source
boats <- readJPEG('boats.jpg')

red_boats <- boats[,,1]
green_boats <- boats[,,2]
blue_boats <- boats[,,3]

pca_red_boats <- prcomp(red_boats, center = FALSE)
pca_green_boats <- prcomp(green_boats, center = FALSE)
pca_blue_boats <- prcomp(blue_boats, center = FALSE)

compressed_boats <- abind(pca_red_boats$x[,1:15] %*% t(pca_red_boats$rotation[,1:15]),
                        pca_green_boats$x[,1:15] %*% t(pca_green_boats$rotation[,1:15]),
                        pca_blue_boats$x[,1:15] %*% t(pca_blue_boats$rotation[,1:15]),
                        along = 3)
plot(image_read(compressed_boats))
title("Compressed image \n (15 Components)")

par(mfrow = c(1,2), mar = c(0,0.2,3,0.2))

plot(image_read(boats))
title("Original image")

plot(image_read(readJPEG('boats_regatta.jpg')))
title("Famous painting \n in similar style")

Original image source Famous painting source

Some extension

Even though image compression throughout the above analysis was done assuming the same number of principal components for all three colors, this is not a necessary assumption. It is also possible to compress an image using different numbers of components depending on the color. In particular, one can compress a matrix corresponding only to a single color, which was shown below. PCA was performed here only for the green color level.

compressed_nature_green <- abind(red_nature,
                               pca_green_nature$x[,1:5] %*% t(pca_green_nature$rotation[,1:5]),
                               blue_nature,
                               along = 3)

plot(image_read(compressed_nature_green))
title("Compressed image with 5 green components")
Original image source

Summary

As it was shown in the above paper, PCA is a useful technique not only for finding patterns in multidimensional data, but also for, among others, image compression. In particular, it is this application that was presented in the above work together with an exemplary image analysis. As it turns out, image compression can also be an interesting tool for playing a little artist.