Let's go to 02.General anatomy.ipynb (also in this repo under notebook_basics)
In [ ]:
## Turn this cell into a markdown cell and render
In [22]:
# Keep as code cell and indent the part in braces with indent shortcut
library(foreach)
x <- foreach(i = seq(0, 1, by = 0.25)) %do% {
y <- i^2 # indent me!
}
x
Out[22]:
In [23]:
IRdisplay::display_html("<img width = 200 src = 'http://pages.discovery.wisc.edu/~avelten/images/zebrafish_brain_10x.jpeg'>")
In [ ]:
# code up your solution here...
SOLUTION
In [1]:
IRdisplay::display_html("<img width = 500 src = 'images/05_Expanded_Image_Analysis_Lab/a_query.jpg'>")
In [32]:
R.version.string
Out[32]:
In [40]:
if ("jpeg" %in% rownames(installed.packages()) == FALSE) {
install.packages("jpeg"),
}
EXERCISE: Tab completion and function introspection
TAB delete this and hit Tab
In [1]:
# load the package jpeg
library(jpeg)
# Andreas Velten Lab - confocal images of zebrafish CNS
url <- "http://pages.discovery.wisc.edu/~avelten/images/zebrafish_brain_10x.jpeg"
# A different zebrafish - entire body
# url <- "http://pages.discovery.wisc.edu/~avelten/images/zebrafish_stitched.png"
# download the file and save it as "Image.jpg" in the current directory
image.down <- download.TAB(urTAB, "image.jpg")
# read the image
img <- readJPEG(TAB "image.jpg") # Read the image
Out[1]:
In [ ]:
# load the package jpeg
library(jpeg)
# Andreas Velten Lab - confocal images of zebrafish CNS
url <- "http://pages.discovery.wisc.edu/~avelten/images/zebrafish_brain_10x.jpeg"
# A different zebrafish - entire body
# url <- "http://pages.discovery.wisc.edu/~avelten/images/zebrafish_stitched.png"
# download the file and save it as "Image.jpg" in the current directory
image.down <- download.(url, "image.jpg")
# read the image
img <- readJPEG("image.jpg") # Read the image
In [8]:
img <- readJPEG("images/03_Image_Analysis_with_k-Means/zebrafish_brain_10x.jpeg") # Read the image
# Obtain the dimension
imgDm <- dim(img)
# Assign RGB channels to data frame
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])
)
In [ ]:
In [24]:
library(ggplot2)
# customize some ggplot settings
plotTheme <- function() {
theme(
panel.background = element_rect(
size = 3,
colour = "black",
fill = "white"),
axis.ticks = element_line(
size = 2),
panel.grid.major = element_line(
colour = "gray80",
linetype = "dotted"),
panel.grid.minor = element_line(
colour = "gray90",
linetype = "dashed"),
axis.title.x = element_text(
size = rel(1.2),
face = "bold"),
axis.title.y = element_text(
size = rel(1.2),
face = "bold"),
plot.title = element_text(
size = 20,
face = "bold",
vjust = 1.5)
)
}
In [27]:
# Plot the image - this is a re-rendering of original by the way
ggplot(data = imgRGB, aes(x = x, y = y)) +
geom_point(color = rgb(imgRGB[c("R", "G", "B")])) +
labs(title = "Original Image: Zebrafish Brain") +
xlab("x") +
ylab("y") +
plotTheme()
In [14]:
kClusters <- 4
kMeans <- kmeans(imgRGB[, c("R", "G", "B")], centers = kClusters)
kColors <- rgb(kMeans$centers[kMeans$cluster,])
In [15]:
head(kMeans$centers)
Out[15]:
In [16]:
head(kMeans$cluster)
Out[16]:
EXERCISE
ggplot(data = ___, aes(x = x, y = y)) +
geom_point(color = ___) +
labs(title = paste("k-Means Clustering of", ___, "Colors")) +
xlab("x") +
ylab("y") +
plotTheme()
In [17]:
ggplot(data = imgRGB, aes(x = x, y = y)) +
geom_point(color = kColors) +
labs(title = paste("k-Means Clustering of", kClusters, "Colors")) +
xlab("x") +
ylab("y") +
plotTheme()
QUESTION: How would you quantify each "channel" or signal associated with a cluster?
https://www.linkedin.com/pulse/image-processing-r-part-1-anuj-kumar
http://www.r-bloggers.com/new-package-for-image-processing-in-r/
In [ ]: