Skip to content
Snippets Groups Projects

Update auto_crop.R

Closed Wayne Crismani requested to merge (removed):wcrismani-master-patch-00495 into master
+ 25
8
#' A cropping function
#'
#' This function identifies meiotic nuclei and crops them.
#' @keywords synapsis
#' @export
#' @examples
#' To use the "crop" function on a folder of images, apply the following three lines of code
#' path = "/folder_of_your_images"
#' files <- list.files(path)
#' crop(files, "/folder_of_your_images")
#'
#' # set the path below to where your jpegs are located
#' # to save the cropped images, make a folder called "crops" which in "/folder_of_your_images" in other words "/folder_of_your_images/crops"
crop <- function(file_list, img_path, crop_method = "regular")
{
# input :
@@ -11,8 +24,8 @@ crop <- function(file_list, img_path, crop_method = "regular")
cell_count <- 0
image_count <-0
pair <- 0
## for each image that is *-dna.jpeg,
# I think that what is being referred to as ".dna" below should be referred to as "sycp3", or perhaps "axis". Happy to discuss more about this important meiosis-nerd distinction.
## Pairing multiple channels for each image e.g. the 594 axis marker and the 488 recombination marker
for (file in file_list){
setwd(img_path)
if(grepl("*dna.jpeg$", file)){
@@ -31,7 +44,7 @@ crop <- function(file_list, img_path, crop_method = "regular")
}
if(pair ==1){
#### function: blur the image
#### function: blur the image
## call it on img_orig, optional offset
blob_th <- get_blobs(img_orig)
@@ -39,8 +52,7 @@ crop <- function(file_list, img_path, crop_method = "regular")
blob_label <- channel(blob_label, "gray")
candidate <- bwlabel(blob_label)
## function: remove things that aren't cells
## function: remove things that aren't cells (#I am confused about something called "keep cells" being assigned to a variable called "removed".)
removed <- keep_cells(candidate)
### crop over each cell
@@ -81,6 +93,7 @@ print("viable cells")
### add all the functions used here
#################################### new function ####################################
# this function blurs nuclei that have been stained with antibodies against axis proteins
get_blobs <- function(img_orig, crop_method = "regular"){
# input:
@@ -110,6 +123,7 @@ get_blobs <- function(img_orig, crop_method = "regular"){
}
#################################### new function ####################################
# this function does "ABC"
keep_cells <- function(candidate){
# input:
@@ -120,12 +134,14 @@ keep_cells <- function(candidate){
# delete everything that's too small
colorimg<- colorLabels(candidate, normalize = TRUE)
x <- computeFeatures.shape(candidate)
x <- computeFeatures.shape(candidate) # uses function from ebimage. give output kind of related to how circular the object is, size, perimeter, radius average etc
x <- data.frame(x)
OOI <- width(x)
OOI <- width(x) #Object Of Interest
counter <- 0
removed <- candidate
# loops over each Object of Interest (OOI)
while(counter<OOI){
counter <- counter+1
pixel_area = x$s.area[counter]
@@ -145,7 +161,7 @@ keep_cells <- function(candidate){
return(removed)
}
# function to help not count the same object twice
crop_single_object <- function(removed, OOI_final,counter_final,img_orig,img_orig_foci,file,cell_count){
tmp_img <- removed
## have a single object
@@ -162,6 +178,7 @@ crop_single_object <- function(removed, OOI_final,counter_final,img_orig,img_ori
}
# put nucleus mask on original image of the axis-marker or foci channel. matrix stuff which crops it to a square
## function: remove noise
noise_gone <- bwlabel(tmp_img)*as.matrix(img_orig)
noise_gone_foci <- bwlabel(tmp_img)*as.matrix(img_orig_foci)
Loading