Created
March 15, 2020 17:10
-
-
Save InsiderPhD/0dab6ab615f70126101a3a0d77f80b55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Drawing a scatter plot of raster images | |
doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
toInstall <- c("png", "devtools", "MASS", "RCurl") | |
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")} | |
lapply(toInstall, library, character.only = TRUE) | |
# Some helper functions, lineFinder and makeTable | |
source_gist("818983") | |
source_gist("818986") | |
files <- list.files("E:\\Downloads\\yarn") | |
pngList <- list() | |
for(filename in files){ | |
tempPNG <- readJPEG(paste0("E:\\Downloads\\yarn\\",filename)) # Downloads & loads PNGs | |
pngList[[filename]] <- tempPNG # And assigns them to a list. | |
} | |
# Very simple dimension reduction -- just the mean R, G, and B values | |
RBGPos <- t(sapply(pngList, function(ll){ | |
ll[, , -4][1:3] | |
})) | |
RBGPos <- as.data.frame(RBGPos) | |
meanRGB <- as.data.frame(meanRGB) | |
PBGPos <- cbind(RBGPos, meanRGB) | |
colnames(RBGPos) <- c("R", "B", "G") | |
RBGPos$file <- rownames(RBGPos) | |
# The dimensions of each item are equal to the pixel dimensions of the .PNG | |
flagDimensions <- t(sapply(pngList, function(ll){ | |
dim<- | |
})) | |
# Similarity, through Kruskal non-metric MDS | |
distance <- dist(meanRGB) | |
distance[distance <= 0] <- 1e-10 | |
MDS <- isoMDS(distance)$points | |
plot(meanRGB, col = rgb(meanRGB), pch = 20, cex = 2) | |
meanRGB[,2] <- 1 | |
meanRGB <- t(sapply(pngList, function(ll){ | |
apply(ll[, , -4], 3, mean) | |
})) | |
#RBG to XYZ | |
for(ii in 1:length(pngList)){ | |
tempName <- rownames(meanRGB)[ii] | |
#meanRGB[tempName,1] <- gamma.correct(meanRGB[tempName,1]) | |
#meanRGB[tempName,2] <- gamma.correct(meanRGB[tempName,2]) | |
#meanRGB[tempName,3] <- gamma.correct(meanRGB[tempName,3]) | |
x <- meanRGB[tempName,1] * 0.649926 + meanRGB[tempName,2] * 0.103455 + meanRGB[tempName,3] * 0.197109 | |
y <- meanRGB[tempName,1] * 0.234327 + meanRGB[tempName,2] * 0.743075 + meanRGB[tempName,3] * 0.022598 | |
z <- meanRGB[tempName,1] * 0.0000000 + meanRGB[tempName,2] * 0.053077 + meanRGB[tempName,3] * 1.035763 | |
meanRGB[tempName, 1] <- x / (x + y + z); | |
meanRGB[tempName, 2] <- y / (x + y + z); | |
} | |
# Plot: | |
boxParameter <- 2000 #6000 # To alter dimensions of raster image bounding box | |
par(bg = gray(8/9)) | |
plot(meanRGB, type = "n", asp = 1) | |
for(ii in 1:length(pngList)){ # Go through each flag | |
tempName <- rownames(meanRGB)[ii] | |
Coords <- meanRGB[tempName, 1:2] # Get coordinates from MDS | |
rasterImage(pngList[[tempName]], # Plot each flag with these boundaries: | |
Coords[1]-40/boxParameter, Coords[2]-40/boxParameter, | |
Coords[1]+40/boxParameter, Coords[2]+40/boxParameter) | |
} | |
boxParameter <- 2000 #6000 # To alter dimensions of raster image bounding box | |
par(bg = gray(8/9)) | |
plot(meanRGB, type = "n", asp = 1) | |
for(ii in 1:length(pngList)){ # Go through each flag | |
tempName <- rownames(meanRGB)[ii] | |
Coords <- meanRGB[tempName, 1:2] # Get coordinates from MDS | |
Dims <- flagDimensions[tempName, ] # Get pixel dimensions | |
rasterImage(pngList[[tempName]], # Plot each flag with these boundaries: | |
Coords[1]-75/boxParameter, Coords[2]-75/boxParameter, | |
Coords[1]+75/boxParameter, Coords[2]+75/boxParameter) | |
} | |
gamma.correct <- function(color) { | |
if(color > 0.04045) { | |
return((color + 0.055) / (1.0 + 0.055) ^ 2.4) | |
} else { | |
return(color / 12.92) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment