Load an image

library("png")
library("seriation")

p <- readPNG("horses.png")
dim(p)
## [1] 536 750   3

Show the different channels

pimage(p[,,1], prop = TRUE,
  col = rgb(seq(0,1, length.out = 10), 0,0))

pimage(p[,,2], prop = TRUE,
  col = rgb(0,seq(0,1, length.out = 10),0))

pimage(p[,,3], prop = TRUE,
  col = rgb(0,0,seq(0,1, length.out = 10)))

Prepare image

Create a data frame with RGB channels for each pixel

df <- data.frame(
  red = as.vector(p[,,1]),
  green = as.vector(p[,,2]),
  blue = as.vector(p[,,3])
)

head(df)
##         red     green      blue
## 1 0.3686275 0.3803922 0.2705882
## 2 0.3803922 0.3921569 0.2823529
## 3 0.3490196 0.3607843 0.2431373
## 4 0.3921569 0.4000000 0.2901961
## 5 0.3568627 0.3647059 0.2392157
## 6 0.2862745 0.2980392 0.1568627
dim(df)
## [1] 402000      3

Color distribuion

hist(df[,1], main = "red")

hist(df[,2], main = "green")

hist(df[,3], main = "blue")

plot(df[sample(nrow(df), 1000),])

Cluster with k-means

cl <- kmeans(df, 2)

# convert clusters back into a matrix (with the same layout as
# the original image)
m <- matrix(cl$cluster, nrow= nrow(p))
pimage(m, prop = TRUE)

cl <- kmeans(df, 3)
m <- matrix(cl$cluster, nrow= nrow(p))
pimage(m, prop = TRUE)

cl <- kmeans(df, 5)
m <- matrix(cl$cluster, nrow= nrow(p))
pimage(m, prop = TRUE)