In this project we are at image analysis and k-means clustering.
# URL to load our image from
url <- "http://www.ccs.neu.edu/home/criedl/Bird.jpeg"
# Download the file and save it as "image.jpg" in the current directory
dFile <- download.file(url, mode = "wb", "image.jpg")
img <- readJPEG("image.jpg") # Read the image
imgDm <- dim(img)
imgDm
## [1] 526 800 3
This yields the image as 526 X 800 pixels and the color values, red, green, blue.
# Create data.frame of RGB channels for each pixel
imgRGB <- data.frame(
x = rep(1:imgDm[2], each=imgDm[1]),
y = rep(imgDm[1]:1, imgDm[2]),
R = as.vector(img[,,1]),
G = as.vector(img[,,2]),
B = as.vector(img[,,3])
)
dim(imgRGB)
## [1] 420800 5
summary(imgRGB)
## x y R G
## Min. : 1.0 Min. : 1.0 Min. :0.0000 Min. :0.0000
## 1st Qu.:200.8 1st Qu.:132.0 1st Qu.:0.1686 1st Qu.:0.1490
## Median :400.5 Median :263.5 Median :0.3255 Median :0.2824
## Mean :400.5 Mean :263.5 Mean :0.4388 Mean :0.3188
## 3rd Qu.:600.2 3rd Qu.:395.0 3rd Qu.:0.7804 3rd Qu.:0.4157
## Max. :800.0 Max. :526.0 Max. :1.0000 Max. :1.0000
## B
## Min. :0.0000
## 1st Qu.:0.1373
## Median :0.2667
## Mean :0.3254
## 3rd Qu.:0.4196
## Max. :1.0000
ggplot(data = imgRGB, aes(x = x, y = y)) +
geom_point(colour = rgb(imgRGB[c("R", "G", "B")])) +
labs(title = "Original Image: Colorful Bird") +
xlab("x") +
ylab("y")
k <- 3
kMeans <- kmeans(imgRGB[, c("R", "G", "B")], centers=k, nstart=1)
kColors <- rgb(kMeans$centers[kMeans$cluster,])
ggplot(data = imgRGB, aes(x = x, y = y)) +
geom_point(colour=kColors) +
labs(title = "k-Means Clustering of 3 Colors") +
xlab("x") +
ylab("y")