Skip to content

Commit

Permalink
version 0.2.0 contains the possibility to draw scale bars when conver…
Browse files Browse the repository at this point in the history
…ting from czi to tif
  • Loading branch information
buddekai committed Jun 30, 2022
1 parent 8cf1e60 commit 3536ebd
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: readCzi
Type: Package
Title: Read czi image files, convert them to tifs, and save metadata
Version: 0.1.17
Version: 0.2.0
Author: c(person("Kai", "Budde", email = "kai.budde@uni-rostock.de",
role = c("aut", "cre"))
Maintainer: Kai Budde <kai.budde@uni-rostock.de>
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(addScaleBar)
export(convertCziToTif)
export(normalizeIntensity)
export(readCzi)
export(readCziMetadata)
export(resizeImage)
export(stackLayers)
103 changes: 103 additions & 0 deletions R/addScaleBar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' @title addScaleBar
#' @description Add a horizontal scale bare to image
#' @details Add a scale bar to a tiff image depending on the meta data of
#' the czi file
#' @aliases addscaleBar addScalebar addscalebar
#' @author Kai Budde
#' @export addScaleBar
#' @param image An array (2 or 3 dimensional array, could also be of
#' formal class Image by package EBImage)
#' @param length_per_pixel_in_um A number (length per pixel in um)
#' @param distance_from_border A number (distance in pixels for the scale bar)
#' @param number_size_factor A number (factor for resizing the number)


addScaleBar <- function(image = NULL,
length_per_pixel_in_um = NULL,
distance_from_border = 20,
number_size_factor = 1){

# dim_x <- dim(image)[2]
# dim_y <- dim(image)[1]
# dim_z <- dim(image)[3]
dim_x <- dim(image)[1]
dim_y <- dim(image)[2]
dim_z <- dim(image)[3]


dim_x_microns <- dim_x * length_per_pixel_in_um
length_scale_bar_microns <- round(dim_x_microns / 10)

if(length_scale_bar_microns < 10){
length_scale_bar_microns <- 10
}else if(length_scale_bar_microns < 20){
length_scale_bar_microns <- 20
}else if(length_scale_bar_microns < 50){
length_scale_bar_microns <- 50
}else if(length_scale_bar_microns < 100){
length_scale_bar_microns <- 100
}else if(length_scale_bar_microns < 200){
length_scale_bar_microns <- 200
}else if(length_scale_bar_microns < 500){
length_scale_bar_microns <- 500
}

length_scale_bar_pixels <- length_scale_bar_microns /
(length_per_pixel_in_um)

length_scale_bar_pixels <- as.integer(round(length_scale_bar_pixels))
heigth_scale_bar_pixels <- as.integer(round(dim_y/150))

# Add the scale_bar
# image[(dim_y-distance_from_border-heigth_scale_bar_pixels):(dim_y-distance_from_border),
# (dim_x-distance_from_border-length_scale_bar_pixels):(dim_x-distance_from_border),
# 1:dim_z] <- 1
image[(dim_x-distance_from_border-length_scale_bar_pixels):(dim_x-distance_from_border),
(dim_y-distance_from_border-heigth_scale_bar_pixels):(dim_y-distance_from_border),
1:dim_z] <- 1

# Add length and unit
# number_pos_x <- dim_y-distance_from_border-heigth_scale_bar_pixels
# number_pos_y <- dim_x-distance_from_border-0.5*length_scale_bar_pixels
number_pos_x <- dim_x-distance_from_border-0.5*length_scale_bar_pixels
number_pos_y <- dim_y-distance_from_border-heigth_scale_bar_pixels

# Get the file names with the text
scale_legend_path <- paste(length_scale_bar_microns, "microns.tif", sep="")
scale_legend_path <- system.file("scale", scale_legend_path, package = "cellPixels")

scale_legend_image <- tiff::readTIFF(source = scale_legend_path, convert = TRUE,
info = FALSE)
# Only keep the black layer
scale_legend_image <- scale_legend_image[,,4]

# Rescale image
resize_factor <- dim_x/15/dim(scale_legend_image)[2]
scale_legend_image <- resizeImage(image = scale_legend_image,
resize_factor = resize_factor)

dim_legend_x <- dim(scale_legend_image)[2]
dim_legend_y <- dim(scale_legend_image)[1]
# dim_legend_x <- dim(scale_legend_image)[1]
# dim_legend_y <- dim(scale_legend_image)[2]

# Add number images to image
start_x <- dim_x-distance_from_border-0.5*length_scale_bar_pixels-0.5*dim_legend_x
start_y <- dim_y-distance_from_border-heigth_scale_bar_pixels-1.1*dim_legend_y
# start_x <- dim_x-distance_from_border-0.5*length_scale_bar_pixels-0.5*dim_legend_x
# start_y <- dim_y-distance_from_border-heigth_scale_bar_pixels-1.1*dim_legend_y

# for(row in 1:dim_legend_y){
# for(col in 1:dim_legend_x){
for(col in 1:dim_legend_x){
for(row in 1:dim_legend_y){
if(scale_legend_image[row, col] > 0){
# if(scale_legend_image[col, row] > 0){
# image[start_y+row, start_x+col, 1:dim_z] <- scale_legend_image[row, col]
image[start_x+col, start_y+row, 1:dim_z] <- scale_legend_image[row, col]
}
}
}

return(image)
}
45 changes: 43 additions & 2 deletions R/convertCziToTif.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#' of the z-stack or original image if it is not a z stack)
#' @param normalize_stack A logical (T/F for normalizing intensities of stack)
#' @param change_layers A character (either "none" or, e.g., "green<->red")
#' @param add_scale_bar A logic (add scale bar to all images that are saved
#' if true)

# TODO: stack_method: average OR max (instead of maxprojection)

Expand All @@ -28,7 +30,8 @@ convertCziToTif <- function(input_file = NULL,
higher_contrast_slices = FALSE,
higher_contrast_stack = TRUE,
normalize_stack = TRUE,
change_layers = "none") {
change_layers = "none",
add_scale_bar = TRUE) {
if(is.null(input_file)){
print("Please call function with input file.")
return()
Expand All @@ -43,7 +46,7 @@ convertCziToTif <- function(input_file = NULL,

# Load image and convert it to Image class -------------------------------
# Dimensions of the image: 1: row, 2: col, 3: channels (r,g,b), 4: z-layer,
image_data <- readCzi(input_file = input_file)
image_data <- readCzi::readCzi(input_file = input_file)
dim_z <- dim(image_data)[4]

# Drop z-layer of multidimensional array if dim_z == 1
Expand Down Expand Up @@ -92,12 +95,28 @@ convertCziToTif <- function(input_file = NULL,
# Convert to EBImage
Image_Data <- EBImage::Image(data = image_data, colormode = "Color")

# Save length per pixel in x-direction
df_metadata <- readCzi::readCziMetadata(input_file = input_file, save_metadata = FALSE)
length_per_pixel_x_in_um <- df_metadata$scaling_x_in_um[1]

# Check that pixels in x and y direction have the same lengths
length_per_pixel_y_in_um <- df_metadata$scaling_y_in_um[1]

if(length_per_pixel_x_in_um != length_per_pixel_y_in_um){
print("Dimension in x- and y-directions are different! ERROR!")
}

# Save all slices --------------------------------------------------------

if(dim_z > 1 && convert_all_slices){
tif_file_names <- rep(image_name_wo_czi, dim(image_data)[4])
tif_file_names <- paste(output_dir, "/", tif_file_names, "_z", 1:dim(image_data)[4], ".tif", sep="")

if(add_scale_bar){
Image_Data <- addScaleBar(image = Image_Data,
length_per_pixel = length_per_pixel_x_in_um)
}

EBImage::writeImage(x = Image_Data, files = tif_file_names, type = "tiff", bits.per.sample = 8)
}

Expand All @@ -108,12 +127,22 @@ convertCziToTif <- function(input_file = NULL,
stack_method = stack_method)

stack_file_name <- paste(output_dir, "/", image_name_wo_czi, "_zstack.tif", sep="")

if(add_scale_bar){
Image_Stack <- addScaleBar(image = Image_Stack,
length_per_pixel = length_per_pixel_x_in_um)
}
EBImage::writeImage(x = Image_Stack, files = stack_file_name, type = "tiff", bits.per.sample = 8)

}else{
# Not a z-stack image (dim_z==1)

output_file_name <- paste(output_dir, "/", image_name_wo_czi, ".tif", sep="")

if(add_scale_bar){
Image_Data <- addScaleBar(image = Image_Data,
length_per_pixel = length_per_pixel_x_in_um)
}
EBImage::writeImage(x = Image_Data, files = output_file_name, type = "tiff", bits.per.sample = 8)

}
Expand All @@ -128,6 +157,10 @@ convertCziToTif <- function(input_file = NULL,
tif_file_names <- rep(image_name_wo_czi, dim(image_data)[4])
tif_file_names <- paste(output_dir, "/", tif_file_names, "_z", 1:dim(image_data)[4], "_histogram_equalized.tif", sep="")

if(add_scale_bar){
Image_Data <- addScaleBar(image = Image_Data,
length_per_pixel = length_per_pixel_x_in_um)
}
EBImage::writeImage(x = Image_Data, files = tif_file_names, type = "tiff", bits.per.sample = 8)

}
Expand All @@ -145,6 +178,10 @@ convertCziToTif <- function(input_file = NULL,
output_file_name <- paste(output_dir, "/", image_name_wo_czi, "_histogram_equalized.tif", sep="")
}

if(add_scale_bar){
Image_Stack_histogram_equalization <- addScaleBar(image = Image_Stack_histogram_equalization,
length_per_pixel = length_per_pixel_x_in_um)
}
EBImage::writeImage(x = Image_Stack_histogram_equalization, files = output_file_name, type = "tiff", bits.per.sample = 8)

}
Expand Down Expand Up @@ -194,6 +231,10 @@ convertCziToTif <- function(input_file = NULL,
output_file_name <- paste(output_dir, "/", image_name_wo_czi, "_histogram_equalized_normalized.tif", sep="")
}

if(add_scale_bar){
Image_Stack_normalized <- addScaleBar(image = Image_Stack_normalized,
length_per_pixel = length_per_pixel_x_in_um)
}
EBImage::writeImage(x = Image_Stack_normalized, files = output_file_name, type = "tiff", bits.per.sample = 8)

}
Expand Down
50 changes: 50 additions & 0 deletions R/resizeImage.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' @title resizeImage
#' @description Resizes an image
#' @details Resizes an image with a given scaling factor
#' @aliases resizeimage
#' @author Kai Budde
#' @export resizeImage
#' @param image An one to three-dimensional array of numbers between 0 and 1
#' @param resize_factor A number (factor for resizing an image)

resizeImage <- function(image = NULL,
resize_factor = 1){

# Default values for missing arguments -----------------------------------
if(is.null(image)){
print(paste("Please call the function with an image.", sep=""))
return()
}
if(is.null(resize_factor)){
print(paste("Please call the function with a number", sep=""))
return()
}

if(resize_factor < 0){
print(paste("The factor may only be a number > 0", sep=""))
return()
}

# Leave image as is if factor == 1 ---------------------------------------
if(resize_factor == 1){
return(image)
}

# Shrink image if resize_factor </> 1 ---------------------------------
if(resize_factor > 0){

new_height <- round(resize_factor * dim(image)[1])
new_width <- round(resize_factor * dim(image)[2])

new_img = apply(image, 2, function(y){return (stats::spline(y, n = new_height)$y)})
new_img = t(apply(new_img, 1, function(y){return (stats::spline(y, n = new_width)$y)}))

new_img[new_img < 0] = 0
new_img = round(new_img)

return(new_img)
}

print("Something went wrong.")
return(0)
}
36 changes: 36 additions & 0 deletions man/addScaleBar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/convertCziToTif.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/resizeImage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3536ebd

Please sign in to comment.