Last active
September 24, 2018 11:29
-
-
Save MahShaaban/3f075e00237896b40e2bb09cc77f298b to your computer and use it in GitHub Desktop.
Reproduce the output of colocr using only imager.
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
library(imager) | |
# download data | |
if(!file.exists('./stats.csv')) { | |
download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/stats_18.09.02_05.15.01.csv', | |
destfile = './stats.csv') | |
} | |
if(!file.exists('./inputs.csv')) { | |
download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/inputs_18.09.02_05.15.08.csv', | |
destfile = './inputs.csv') | |
} | |
if(!file.exists('./Image0001_.jpg')) { | |
download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0001_.jpg?raw=true', | |
destfile = './Image0001_.jpg') | |
} | |
if(!file.exists('./Image0003_.jpg')) { | |
download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0003_.jpg?raw=true', | |
destfile = './Image0003_.jpg') | |
} | |
# read input and output from the app | |
stats <- read.csv('./stats.csv') | |
inputs <- read.csv('./inputs.csv') | |
# read images | |
lst <- as.list(inputs) | |
num <- list() | |
for(i in 1:2) { | |
fl <- paste0('./', lst$image[i]) | |
img <- load.image(fl) | |
# transform to gray scale | |
img.g <- grayscale(img) | |
# apply threshold | |
img.t <- threshold(img.g, paste0(lst$threshold[i], '%')) | |
# change to pixset | |
px <- as.pixset(1-img.t) | |
# apply shrink | |
px.m <- shrink(px, lst$shrink[i]) | |
# apply grow | |
px.m <- grow(px.m, lst$grow[i]) | |
# apply fill | |
px.m <- fill(px.m, lst$fill[i]) | |
# apply clean | |
px <- clean(px.m, lst$clean[i]) | |
# add labels | |
px.labs <- label(px, tolerance = lst$tolerance[i]) | |
value <- as.data.frame(px.labs)$value | |
ids <- reorder(value, value, length) | |
k <- levels(ids) | |
k <- k[(length(k)-1):(length(k)-lst$roi_num[i])] | |
new.ids <- ifelse(value %in% as.numeric(k), value, 0) | |
f <- as.numeric(factor(new.ids)) | |
new.px <- as.data.frame(px.labs) | |
new.px$value <- f - 1 | |
new.px <- as.cimg(new.px, dim = dim(px)) | |
labs <- as.data.frame(new.px)$value | |
# extract first two channels | |
img1.g <- channel(img, ind = 1) | |
img2.g <- channel(img, ind = 2) | |
# subset and change images to numeric | |
img1.num <- as.numeric(img1.g[as.pixset(new.px)]) | |
img2.num <- as.numeric(img2.g[as.pixset(new.px)]) | |
num[[i]] <- list(img1.num, | |
img2.num, | |
labs[labs != 0]) | |
} | |
# test pcc | |
corr <- list() | |
for(i in 1:2) { | |
lab <- (num[[i]][[3]]) | |
res <- list() | |
for(j in unique(lab)) { | |
c1 <- num[[i]][[1]][lab == j] | |
c2 <- num[[i]][[2]][lab == j] | |
rx <- c1 - mean(c1) | |
gx <- c2 - mean(c2) | |
nom <- sum(rx * gx) | |
den <- sqrt(sum(rx**2) * sum(gx**2)) | |
res[[j]] <-nom/den | |
} | |
corr[[i]] <- res | |
} | |
print( | |
all.equal(round(stats$pcc, 5), round(unlist(corr), 5)) | |
) | |
# test moc | |
corr <- list() | |
for(i in 1:2) { | |
lab <- (num[[i]][[3]]) | |
res <- list() | |
for(j in unique(lab)) { | |
c1 <- num[[i]][[1]][lab == j] | |
c2 <- num[[i]][[2]][lab == j] | |
nom <- sum(c1 * c2) | |
den <- sqrt(sum(c1**2) * sum(c2**2)) | |
res[[j]] <- nom/den | |
} | |
corr[[i]] <- res | |
} | |
print( | |
all.equal(round(stats$moc, 5), round(unlist(corr), 5)) | |
) | |
unlink('./*.csv') | |
unlink('./*.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2018-09-24 by the reprex package (v0.2.0).
Session info