Skip to content

Instantly share code, notes, and snippets.

@stephaniehicks
Created February 8, 2017 20:10
Show Gist options
  • Save stephaniehicks/9831e9658f00f650288131478647bf07 to your computer and use it in GitHub Desktop.
Save stephaniehicks/9831e9658f00f650288131478647bf07 to your computer and use it in GitHub Desktop.
Sample points from an image based on intensity
library(jpeg) # or png, depending on your image (replace readJPEG with readPNG)
y <- readJPEG("rafa.jpeg") # You can get this off Google if you want to test it.
jpeg("channels1to3.jpeg", width = 1200, height = 800)
par(mfrow=c(2,3))
image(y[,,1], main = "Channel 1")
image(y[,,2], main = "Channel 2")
image(y[,,3], main = "Channel 3")
image(y[,,1], main = "Channel 1", col = rgb(seq(0, 1, by = 0.05), 0, 0))
image(y[,,2], main = "Channel 2", col = rgb(0, seq(0, 1, by = 0.05), 0))
image(y[,,3], main = "Channel 3", col = rgb(0, 0, seq(0, 1, by = 0.05)))
dev.off()
gs <- 1-(y[,,1] + y[,,2] + y[,,3])/3 # get intensity of blackness by averaging RGB channels
# pick contrast: power determines contrast, may need tinkering depending on your initial image, skin tone, background, etc.
jpeg("contrasts.jpeg", width = 800, height = 800)
par(mfrow=c(2,2))
image(gs, main = "power determines contrast (original scale)")
image(gs^2, main = "power determines contrast (power of 2)")
image(gs^4, main = "power determines contrast (power of 4)")
image(gs^8, main = "power determines contrast (power of 8)")
dev.off()
jpeg("samplepoints.jpeg", width = 800, height = 800)
par(mfrow=c(2,2))
prob <- gs
pts <- sample(length(prob), 10000, prob=prob, replace=TRUE) # sampling points proportional to the (powered) intensity
coords <- jitter(arrayInd(pts, .dim=dim(gs))) # making coordinates with some random jitter
plot(coords[,2], -coords[,1], pch=16, cex=0.5, main = "Points sampled on original intensity scale")
prob <- gs^2
pts <- sample(length(prob), 10000, prob=prob, replace=TRUE) # sampling points proportional to the (powered) intensity
coords <- jitter(arrayInd(pts, .dim=dim(gs))) # making coordinates with some random jitter
plot(coords[,2], -coords[,1], pch=16, cex=0.5, main = "Points samples on powered intensity scale\n(power of 2)")
prob <- gs^4
pts <- sample(length(prob), 10000, prob=prob, replace=TRUE) # sampling points proportional to the (powered) intensity
coords <- jitter(arrayInd(pts, .dim=dim(gs))) # making coordinates with some random jitter
plot(coords[,2], -coords[,1], pch=16, cex=0.5, main = "Points samples on powered intensity scale\n(power of 4)")
prob <- gs^8
pts <- sample(length(prob), 10000, prob=prob, replace=TRUE) # sampling points proportional to the (powered) intensity
coords <- jitter(arrayInd(pts, .dim=dim(gs))) # making coordinates with some random jitter
plot(coords[,2], -coords[,1], pch=16, cex=0.5, main = "Points samples on powered intensity scale\n(power of 8)")
dev.off()
jpeg("rafa.jpeg", width = 400, height = 450)
par(mfrow=c(1,1))
prob <- gs^8
pts <- sample(length(prob), 10000, prob=prob, replace=TRUE) # sampling points proportional to the (powered) intensity
coords <- jitter(arrayInd(pts, .dim=dim(gs))) # making coordinates with some random jitter
plot(coords[,2], -coords[,1], pch=16, cex=0.5, ann = FALSE)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment