public
Last active

Image Manipulation, Part 4

  • Download Gist
bright_object.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ReadImages", "reshape", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
imageLoader <- function(url){ # This function takes a URL, and generates a data.frame with pixel locations and colors
# Download to disk, load
download.file(url, "tempPicture.jpg", mode = "wb") # Stash image locally
readImage <- read.jpeg("tempPicture.jpg")
longImage <- melt(readImage)
rgbImage <- reshape(longImage, timevar = "X3",
idvar = c("X1", "X2"), direction = "wide")
rgbImage$X1 <- -rgbImage$X1
return(rgbImage)
}
 
##########
# Part 4 # Finding a bright object
##########
 
# Load NASA's image
rgbImage <- imageLoader("http://www.nasa.gov/images/content/694811main_pia16225-43_full.jpg")
with(rgbImage, plot(X2, X1, col = rgb(rgbImage[, 3:5]), asp = 1, pch = "."))
 
# Look at a region of only sand
sandOnly <- with(rgbImage, rgbImage[X2 > 1000 & X1 > -300, ])
with(sandOnly, plot(X2, X1, col = rgb(sandOnly[, 3:5]), asp = 1, pch = "."))
 
sandMean <- apply(sandOnly[, 3:5], 2, mean) # Find typical sand color values
sandSD <- apply(sandOnly[, 3:5], 2, sd)
colorZ <- sweep(rgbImage[, 3:5], 2, sandMean, "-") # How much do all colors
colorZ <- sweep(colorZ, 2, sandSD, "/") # deviate from sand colors?
plot(density(rowSums(colorZ)))
 
# Plot a "binarized" image, based on color Z-scores
with(rgbImage, plot(X2, X1, col = rgb(colorZ>4), asp = 1, pch = "."))
points(830, -920, cex = 4, col = "RED")
# That little blue dot, at about 830, -920, is the anomaly! Science!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.