One way to group objects is to use clustering
library(jpeg)
library(cluster)
beksinski <- readJPEG("s-2265.jpeg")
dim <- dim(beksinski); dim[1:2]
## [1] 1253 1000
rgbbeksinski <- data.frame(
x=rep(1:dim[2], each=dim[1]),
y=rep(dim[1]:1, dim[2]),
r.value=as.vector(beksinski[,,1]),
g.value=as.vector(beksinski[,,2]),
b.value=as.vector(beksinski[,,3]))
head(rgbbeksinski)
## x y r.value g.value b.value
## 1 1 1253 0.5098039 0.3725490 0.01960784
## 2 1 1252 0.5137255 0.3764706 0.02352941
## 3 1 1251 0.5137255 0.3764706 0.02352941
## 4 1 1250 0.5176471 0.3803922 0.02745098
## 5 1 1249 0.5215686 0.3843137 0.03137255
## 6 1 1248 0.5215686 0.3843137 0.03137255
plot(y~x, data=rgbbeksinski, main="Beksinski Painting",
col= rgb(rgbbeksinski[c("r.value","g.value","b.value")]),
asp= 1, pch= ".")
# Applying CLARA and Shiluoette
# empty vector to save results
a <- c()
# number of clusters to consider
for (i in 1:10) {
cl<-clara(rgbbeksinski[, c("r.value", "g.value", "b.value")], i)
# saving silhouette to vector
a[i]<-cl$silinfo$avg.width
}
plot(a, type='l', main="Optimal number of clusters", xlab="Number of clusters", ylab="Average silhouette", col="blue")
points(a, pch=21, bg="navyblue")
abline(h=(1:30)*5/100, lty=3, col="grey50")
clara <- clara(rgbbeksinski[,3:5],5)
plot(silhouette(clara))
colours <- rgb(clara$medoids[clara$clustering,])
plot(rgbbeksinski$y~rgbbeksinski$x,col=colours, pch=".", cex=2, asp=1,main="5 colours" )
cols.org<-rgb(rgbbeksinski[,3:5])
head(cols.org)
## [1] "#825F05" "#836006" "#836006" "#846107" "#856208" "#856208"
length(unique(cols.org))
## [1] 52784