Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Prepare for the 0.2.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoOlivoto committed May 13, 2021
1 parent 3427474 commit 0d50b21
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 199 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Depends:
R (>= 4.0.0)
Imports:
EBImage,
ggplot2,
magrittr
ggplot2
Suggests:
knitr,
rmarkdown
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

S3method(plot,image_index)
S3method(plot,objects_rgb)
export("%>%")
export(cm_to_dpi)
export(cm_to_pixels)
export(count_lesions)
Expand Down Expand Up @@ -45,7 +44,6 @@ importFrom(graphics,par)
importFrom(graphics,points)
importFrom(graphics,text)
importFrom(grid,grid.raster)
importFrom(magrittr,"%>%")
importFrom(parallel,clusterExport)
importFrom(parallel,detectCores)
importFrom(parallel,makeCluster)
Expand Down
84 changes: 39 additions & 45 deletions R/count_lesions.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,14 @@ count_lesions <- function(img,
################## no background #############
if(is.null(img_background)){
sadio_sintoma <-
rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "img_healthy", 1, 0))
transform(rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,]),
Y = ifelse(CODE == "img_healthy", 1, 0))
sadio_sintoma$CODE <- NULL
usef_area <- nrow(original$df_in)
model <-
glm(Y ~ R + G + B, family = binomial("logit"), data = sadio_sintoma) %>%
suppressWarnings()
model <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"), data = sadio_sintoma))
# isolate plant
pred1 <- predict(model, newdata = original$df_in, type="response") %>% round(0)
pred1 <- round(predict(model, newdata = original$df_in, type="response"), 0)
plant_symp <- matrix(pred1, ncol = ncol(original$R))
plant_symp <- 1 - image_correct(plant_symp, perc = 0.01)
ID <- c(plant_symp == 0)
Expand Down Expand Up @@ -282,30 +280,28 @@ count_lesions <- function(img,
fundo <- image_to_mat(img_background)
# separate image from background
fundo_resto <-
rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,],
fundo$df_in[sample(1:nrow(fundo$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "img_background", 0, 1))
modelo1 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = fundo_resto) %>%
suppressWarnings()
pred1 <- predict(modelo1, newdata = original$df_in, type="response") %>% round(0)
transform(rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,],
fundo$df_in[sample(1:nrow(fundo$df_in)),][1:nrows,]),
Y = ifelse(CODE == "img_background", 0, 1))
modelo1 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"),
data = fundo_resto))
pred1 <- round(predict(modelo1, newdata = original$df_in, type="response"), 0)
plant_background <- matrix(pred1, ncol = ncol(original$R))
plant_background <- image_correct(plant_background, perc = 0.009)
plant_background[plant_background == 1] <- 2
sadio_sintoma <-
rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "img_healthy", 1, 0))
transform(rbind(sadio$df_in[sample(1:nrow(sadio$df_in)),][1:nrows,],
sintoma$df_in[sample(1:nrow(sintoma$df_in)),][1:nrows,]),
Y = ifelse(CODE == "img_healthy", 1, 0))
sadio_sintoma$CODE <- NULL
modelo2 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = sadio_sintoma) %>%
suppressWarnings()
modelo2 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"),
data = sadio_sintoma))
# isolate plant
ID <- c(plant_background == 2)
usef_area <- nrow(original$df_in[ID,])
pred2 <- predict(modelo2, newdata = original$df_in[ID,], type="response") %>% round(0)
pred3 <- predict(modelo2, newdata = original$df_in, type="response") %>% round(0)
pred2 <- round(predict(modelo2, newdata = original$df_in[ID,], type="response"), 0)
pred3 <- round(predict(modelo2, newdata = original$df_in, type="response"), 0)
pred3[!ID] <- 1
leaf_sympts <- matrix(pred3, ncol = ncol(original$R))
leaf_sympts <- 1 - image_correct(leaf_sympts, perc = 0.009)
Expand Down Expand Up @@ -510,21 +506,21 @@ count_lesions <- function(img,
dev.off()
}
stats <-
data.frame(area = c(n = length(shape$s.area),
min(shape$s.area),
mean(shape$s.area),
max(shape$s.area),
sd(shape$s.area),
sum(shape$s.area),
sum(shape$s.area) /usef_area * 100),
perimeter = c(NA,
min(shape$s.perimeter),
mean(shape$s.perimeter),
max(shape$s.perimeter),
sd(shape$s.perimeter),
sum(shape$s.perimeter),
NA)) %>%
transform(statistics = c("n", "min", "mean", "max", "sd", "sum", "prop"))
transform(data.frame(area = c(n = length(shape$s.area),
min(shape$s.area),
mean(shape$s.area),
max(shape$s.area),
sd(shape$s.area),
sum(shape$s.area),
sum(shape$s.area) /usef_area * 100),
perimeter = c(NA,
min(shape$s.perimeter),
mean(shape$s.perimeter),
max(shape$s.perimeter),
sd(shape$s.perimeter),
sum(shape$s.perimeter),
NA)),
statistics = c("n", "min", "mean", "max", "sd", "sum", "prop"))
stats <- stats[c(3, 1, 2)]
shape <- shape[,c(1:6, 8:9, 7)]
colnames(shape) <- c("id", "x", "y", "area", "perimeter", "radius_mean",
Expand Down Expand Up @@ -565,7 +561,7 @@ count_lesions <- function(img,
"check_names_dir", "file_extension", "image_import",
"image_binary", "watershed", "distmap", "computeFeatures.moment",
"computeFeatures.shape", "colorLabels", "image_show",
"%>%", "image_to_mat", "image_correct", "bwlabel"),
"image_to_mat", "image_correct", "bwlabel"),
envir=environment())
on.exit(stopCluster(clust))
if(verbose == TRUE){
Expand Down Expand Up @@ -598,17 +594,15 @@ count_lesions <- function(img,
stats <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["statistics"]] %>%
transform(id = names(results[i])) %>%
.[,c(4, 1, 2, 3)]
transform(results[[i]][["statistics"]],
id = names(results[i]))[,c(4, 1, 2, 3)]
})
)
results <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["results"]] %>%
transform(img = names(results[i])) %>%
.[, c(10, 1:9)]
transform(results[[i]][["results"]],
img = names(results[i]))[, c(10, 1:9)]
})
)
return(list(statistics = stats,
Expand Down
61 changes: 28 additions & 33 deletions R/count_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@ count_objects <- function(img,
foreground <- image_to_mat(foreground)
background <- image_to_mat(background)
back_fore <-
rbind(foreground$df_in[sample(1:nrow(foreground$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "background", 0, 1))
modelo1 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = back_fore) %>%
suppressWarnings()
pred1 <- predict(modelo1, newdata = original$df_in, type="response") %>% round(0)
transform(rbind(foreground$df_in[sample(1:nrow(foreground$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]),
Y = ifelse(CODE == "background", 0, 1))
modelo1 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"), data = back_fore))
pred1 <- round(predict(modelo1, newdata = original$df_in, type="response"), 0)
foreground_background <- matrix(pred1, ncol = ncol(original$R))
foreground_background <- image_correct(foreground_background, perc = 0.02)
ID <- c(foreground_background == 1)
Expand Down Expand Up @@ -242,7 +240,7 @@ count_objects <- function(img,
parms2 <- parms[parms$object_size == object_size,]
rowid <-
which(sapply(as.character(parms2$resolution), function(x) {
eval(parse(text=x))}))
eval(parse(text=x))}))
ext <- ifelse(is.null(extension), parms2[rowid, 3], extension)
tol <- ifelse(is.null(tolerance), parms2[rowid, 4], tolerance)
nmask <- watershed(distmap(img2),
Expand Down Expand Up @@ -360,27 +358,27 @@ count_objects <- function(img,
image_show(im2)
text(shape[,2],
shape[,3],
col = marker_col,
pch = 16,
cex = marker_size)
col = marker_col,
pch = 16,
cex = marker_size)

}
dev.off()
}
stats <-
data.frame(area = c(n = length(shape$s.area),
min(shape$s.area),
mean(shape$s.area),
max(shape$s.area),
sd(shape$s.area),
sum(shape$s.area)),
perimeter = c(NA,
min(shape$s.perimeter),
mean(shape$s.perimeter),
max(shape$s.perimeter),
sd(shape$s.perimeter),
sum(shape$s.perimeter))) %>%
transform(statistics = c("n", "min", "mean", "max", "sd", "sum"))
transform(data.frame(area = c(n = length(shape$s.area),
min(shape$s.area),
mean(shape$s.area),
max(shape$s.area),
sd(shape$s.area),
sum(shape$s.area)),
perimeter = c(NA,
min(shape$s.perimeter),
mean(shape$s.perimeter),
max(shape$s.perimeter),
sd(shape$s.perimeter),
sum(shape$s.perimeter))),
statistics = c("n", "min", "mean", "max", "sd", "sum"))
stats <- stats[c(3, 1, 2)]
shape <- shape[,c(1:6, 8:9, 7)]
colnames(shape) <- c("id", "x", "y", "area", "perimeter", "radius_mean",
Expand Down Expand Up @@ -424,8 +422,7 @@ count_objects <- function(img,
varlist = c("names_plant", "help_count", "file_name",
"check_names_dir", "file_extension", "image_import",
"image_binary", "watershed", "distmap", "computeFeatures.moment",
"computeFeatures.shape", "colorLabels", "image_show",
"%>%"),
"computeFeatures.shape", "colorLabels", "image_show"),
envir=environment())
on.exit(stopCluster(clust))
if(verbose == TRUE){
Expand Down Expand Up @@ -461,20 +458,18 @@ count_objects <- function(img,
stats <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["statistics"]] %>%
transform(id = names(results[i])) %>%
.[,c(4, 1, 2, 3)]
transform(results[[i]][["statistics"]],
id = names(results[i]))[,c(4, 1, 2, 3)]
})
)
results <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["results"]] %>%
transform(img = names(results[i])) %>%
.[, c(10, 1:9)]
transform(results[[i]][["results"]],
img = names(results[i]))[, c(10, 1:9)]
})
)
summ <- stats[stats$statistics == "n",c(1,3)]
summ <- stats[stats$statistics == "n",c(1,3)]
if(verbose == TRUE){
names(summ) <- c("Image", "Objects")
cat("--------------------------------------------\n")
Expand Down
34 changes: 15 additions & 19 deletions R/leaf_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,28 @@ leaf_area <- function(img,
background <- image_to_mat(img_background)
# separate image from background
background_resto <-
rbind(leaf$df_in[sample(1:nrow(leaf$df_in)),][1:nrows,],
template$df_in[sample(1:nrow(template$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "img_background", 1, 0))
transform(rbind(leaf$df_in[sample(1:nrow(leaf$df_in)),][1:nrows,],
template$df_in[sample(1:nrow(template$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]),
Y = ifelse(CODE == "img_background", 1, 0))
background_resto$CODE <- NULL
modelo1 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = background_resto) %>%
suppressWarnings()
pred1 <- predict(modelo1, newdata = original$df_in, type="response") %>% round(0)
modelo1 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"), data = background_resto))
pred1 <- round(predict(modelo1, newdata = original$df_in, type="response"), 0)
plant_background <- matrix(pred1, ncol = ncol(original$R))
plant_background <- image_correct(plant_background, perc = 0.009)
plant_background[plant_background == 1] <- 2
# image_show(plant_background!=2)
# separate leaf from template
leaf_template <-
rbind(leaf$df_in[sample(1:nrow(leaf$df_in)),][1:nrows,],
template$df_in[sample(1:nrow(template$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "img_leaf", 0, 1))
transform(rbind(leaf$df_in[sample(1:nrow(leaf$df_in)),][1:nrows,],
template$df_in[sample(1:nrow(template$df_in)),][1:nrows,]),
Y = ifelse(CODE == "img_leaf", 0, 1))
background_resto$CODE <- NULL
modelo2 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = leaf_template) %>%
suppressWarnings()
modelo2 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"), data = leaf_template))
# isolate plant
ID <- c(plant_background == 0)
pred2 <- predict(modelo2, newdata = original$df_in[ID,], type="response") %>% round(0)
pred3 <- predict(modelo2, newdata = original$df_in, type="response") %>% round(0)
pred2 <- round(predict(modelo2, newdata = original$df_in[ID,], type="response"), 0)
pred3 <- round(predict(modelo2, newdata = original$df_in, type="response"), 0)
leaf_template <- matrix(pred3, ncol = ncol(original$R))
leaf_template <- image_correct(leaf_template, perc = 0.009)
plant_background[leaf_template == 1] <- 3
Expand Down Expand Up @@ -218,8 +214,8 @@ leaf_area <- function(img,
shape <- rbind(shape_leaf, shape_template)
shape$id <- 1:nrow(shape)
shape <-
shape[, c(10, 7, 8, 1, 9, 2:6)] %>%
transform(label = paste(id, "|", round(area, text_digits), sep = ""))
transform(shape[, c(10, 7, 8, 1, 9, 2:6)],
label = paste(id, "|", round(area, text_digits), sep = ""))
if(show_original == TRUE){
im2 <- img
if(!is.null(col_background)){
Expand Down Expand Up @@ -304,7 +300,7 @@ leaf_area <- function(img,
"check_names_dir", "file_extension", "image_import",
"image_binary", "watershed", "distmap", "computeFeatures.moment",
"computeFeatures.shape", "colorLabels", "image_show",
"%>%", "image_to_mat", "image_correct", "bwlabel"),
"image_to_mat", "image_correct", "bwlabel"),
envir=environment())
on.exit(stopCluster(clust))
if(verbose == TRUE){
Expand Down
24 changes: 10 additions & 14 deletions R/objects_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ objects_rgb <- function(img,
foreground <- image_to_mat(foreground)
background <- image_to_mat(background)
back_fore <-
rbind(foreground$df_in[sample(1:nrow(foreground$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]) %>%
transform(Y = ifelse(CODE == "background", 0, 1))
modelo1 <-
glm(Y ~ R + G + B, family = binomial("logit"), data = back_fore) %>%
suppressWarnings()
pred1 <- predict(modelo1, newdata = original$df_in, type="response") %>% round(0)
transform(rbind(foreground$df_in[sample(1:nrow(foreground$df_in)),][1:nrows,],
background$df_in[sample(1:nrow(background$df_in)),][1:nrows,]),
Y = ifelse(CODE == "background", 0, 1))
modelo1 <- suppressWarnings(glm(Y ~ R + G + B, family = binomial("logit"), data = back_fore))
pred1 <- round(predict(modelo1, newdata = original$df_in, type="response"), 0)
foreground_background <- matrix(pred1, ncol = ncol(original$R))
foreground_background <- image_correct(foreground_background, perc = 0.02)
ID <- c(foreground_background == 1)
Expand Down Expand Up @@ -369,7 +367,7 @@ objects_rgb <- function(img,
"check_names_dir", "file_extension", "image_import",
"image_binary", "watershed", "distmap", "computeFeatures.moment",
"computeFeatures.shape", "colorLabels", "image_show",
"%>%", "image_resize", "detectCores", "makeCluster", "clusterExport",
"image_resize", "detectCores", "makeCluster", "clusterExport",
"stopCluster", "parLapply"),
envir=environment())
on.exit(stopCluster(clust))
Expand Down Expand Up @@ -401,17 +399,15 @@ objects_rgb <- function(img,
objects <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["objects"]] %>%
transform(img = names(results[i])) %>%
.[,c(10, 1:9)]
transform(results[[i]][["objects"]],
img = names(results[i]))[,c(10, 1:9)]
})
)
indexes <-
do.call(rbind,
lapply(seq_along(results), function(i){
results[[i]][["indexes"]] %>%
transform(img = names(results[i])) %>%
.[, c(3, 1:2)]
transform(results[[i]][["indexes"]],
img = names(results[i]))[, c(3, 1:2)]
})
)
invisible(list(objects = objects,
Expand Down
Loading

0 comments on commit 0d50b21

Please sign in to comment.