EBImage, rMiW, BiocStyle
Last modified: 2021-11-03 20:03:15
Compiled: 2021-11-03 21:55:33
#Install packages
install.packages(c("devtools", "BiocManager"), repos="http://cran.r-project.org")
BiocManager::install(c("EBImage", "BioImageDbs"), force = TRUE )
devtools::install_github( "kumeS/rMiW", force = TRUE )
#Load packages
library(EBImage)
#remove.packages("rMiW")
library(rMiW)
BiocManager::install(version = "3.14")
The displayed image (`Mouse01_Kid_x20_z0_RR01.png’) is an image of whole slide imaging (WSI) for observing the kidney tissue of C57BL/6J mouse (male, 10 week-old) stained by H&E.
file <- system.file("extdata", "Mouse01_Kid_x20_z0_RR01.png", package="rMiW")
file
## [1] "/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rMiW/extdata/Mouse01_Kid_x20_z0_RR01.png"
#Read image
Img <- EBImage::readImage(files = file)
str(Img)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
## ..@ colormode: int 2
## ..$ dim: int [1:3] 1257 1038 3
Here, ::
in R script indicates an explicit relation between the package and the functions.
#Visualization
EBImage::display(Img, method = "raster")
In this section, we will handle the Image object of EBIimage and perform image processing.
#Read image: delete the 4th element of 3th dimension
#Img <- EBImage::readImage(files = file)
#Convert to the gray image
GrayImg <- rMiW::toGrayScale(Img, mode = "luminance")
str(GrayImg)
str(Img)
#Visualization
EBImage::display(GrayImg, method = "raster")
#1% area size
Img10 <- EBImage::resize(Img,
w = round(dim(Img)[1]*0.1, 0),
filter = "bilinear")
#6% area size
Img25 <- EBImage::resize(Img,
w = round(dim(Img)[1]*0.25, 0),
filter = "bilinear")
#25% area size
Img50 <- EBImage::resize(Img,
w = round(dim(Img)[1]*0.5, 0),
filter = "bilinear")
#50% area size
Img70 <- EBImage::resize(Img,
w = round(dim(Img)[1]*0.707, 0),
filter = "bilinear")
#Visualization
par(mfrow=c(2,2))
EBImage::display(Img10, method = "raster")
text(x = dim(Img10)[1]/20, y = dim(Img10)[2]/10, label = "1% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img25, method = "raster")
text(x = dim(Img25)[1]/20, y = dim(Img25)[2]/10, label = "6% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img50, method = "raster")
text(x = dim(Img50)[1]/20, y = dim(Img50)[2]/10, label = "25% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img70, method = "raster")
text(x = dim(Img70)[1]/20, y = dim(Img70)[2]/10, label = "50% area size", adj = c(0,0), col = "black", cex = 1.5)
Here we will perform some rotation operations.
#Transpose the image
ImgTrans <- EBImage::transpose(Img)
#Flip or flop the image
Imgflip <- EBImage::flip(Img)
Imgflop <- EBImage::flop(Img)
#Visualization
par(mfrow=c(2,2))
EBImage::display(Img, method = "raster")
text(x = dim(Img)[1]/20, y = dim(Img)[2]/10, label = "Original", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(ImgTrans, method = "raster")
text(x = dim(ImgTrans)[1]/20, y = dim(ImgTrans)[2]/10, label = "Transpose", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflip, method = "raster")
text(x = dim(Imgflip)[1]/20, y = dim(Imgflip)[2]/10, label = "Flip", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflop, method = "raster")
text(x = dim(Imgflop)[1]/20, y = dim(Imgflop)[2]/10, label = "Flop", adj = c(0,0), col = "black", cex = 1.5)
We can save the file in image format (jpeg, png, tiff) or R binary (.Rds).
#Save as PNG
EBImage::writeImage(Img, files = "./Img.png",
type = "png")
#Save as TIFF
EBImage::writeImage(Img, files = "./Img.tif",
type = "tiff")
#Save as R binary for single Objects (.Rda)
saveRDS(Img, "./Img.Rds")
dir()
## [1] "Img.png" "Img.Rds"
## [3] "Img.tif" "rMiW_00_GPU.html"
## [5] "rMiW_00_GPU.Rmd" "rMiW_00_installation.html"
## [7] "rMiW_00_installation.Rmd" "rMiW_01_Basic_eval_files"
## [9] "rMiW_01_Basic_eval.html" "rMiW_01_Basic_eval.Rmd"
## [11] "rMiW_01_Basic.html" "rMiW_01_Basic.Rmd"
## [13] "rMiW_02_BioImageDbs_eval.html" "rMiW_02_BioImageDbs_eval.Rmd"
## [15] "rMiW_02_BioImageDbs.html" "rMiW_02_BioImageDbs.Rmd"
## [17] "rMiW_03_GAN.html" "rMiW_03_GAN.Rmd"
#Read Rds format
ImgRds <- readRDS("./Img.Rds")
str(ImgRds)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
## ..@ colormode: int 2
## ..$ dim: int [1:3] 1257 1038 3
When we save the object/variable in R as a Rds file and load it, the same R object will be reproduced.
Overlap two images and color them.
file <- system.file("extdata", "Cell_Img.Rds", package="rMiW")
#Read image
CellImg <- readRDS(file)
str(CellImg)
## List of 2
## $ X:Formal class 'Image' [package "EBImage"] with 2 slots
## .. ..@ .Data : num [1:512, 1:512, 1] 0.518 0.502 0.502 0.522 0.506 ...
## .. ..@ colormode: int 0
## .. ..$ dim: int [1:3] 512 512 1
## $ Y:Formal class 'Image' [package "EBImage"] with 2 slots
## .. ..@ .Data : num [1:512, 1:512, 1] 0 0 0 0 0 0 0 0 0 0 ...
## .. ..@ colormode: int 0
## .. ..$ dim: int [1:3] 512 512 1
#Cell image
par(mfrow=c(1,1))
EBImage::display(CellImg$X, method = "raster")
#Display them side-by-side.
EBImage::display(EBImage::combine(CellImg$X, CellImg$Y),
nx=2, all=TRUE, spacing = 0.01, margin = 70, method = "raster")
#Overlap them
EBImage::display(EBImage::paintObjects(CellImg$Y,
EBImage::toRGB(CellImg$X),
opac=c(0.2, 0.2),
col=c("red","red"), thick=TRUE, closed=FALSE),
method = "raster")
#rMiW function
rMiW::ImageView2D(ImgArray_x=CellImg$X,
ImgArray_y=CellImg$Y,
ImgN=1,
lab=c("Original", "Overlay", "Ground truth"))
drop=FALSE
to prevent the array from being deformedfile <- system.file("extdata", "Cell_Img.Rds", package="rMiW")
file
## [1] "/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rMiW/extdata/Cell_Img.Rds"
#Read image
CellImg <- readRDS(file)
str(CellImg)
## List of 2
## $ X:Formal class 'Image' [package "EBImage"] with 2 slots
## .. ..@ .Data : num [1:512, 1:512, 1] 0.518 0.502 0.502 0.522 0.506 ...
## .. ..@ colormode: int 0
## .. ..$ dim: int [1:3] 512 512 1
## $ Y:Formal class 'Image' [package "EBImage"] with 2 slots
## .. ..@ .Data : num [1:512, 1:512, 1] 0 0 0 0 0 0 0 0 0 0 ...
## .. ..@ colormode: int 0
## .. ..$ dim: int [1:3] 512 512 1
#drop=TRUE
CellImgT <- CellImg$X[1:512,1:512,,drop=TRUE]
str(CellImgT)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:512, 1:512] 0.518 0.502 0.502 0.522 0.506 ...
## ..@ colormode: int 0
## ..$ dim: int [1:2] 512 512
#drop=FALSE
CellImgF <- CellImg$X[1:512,1:512,,drop=FALSE]
str(CellImgF)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:512, 1:512, 1] 0.518 0.502 0.502 0.522 0.506 ...
## ..@ colormode: int 0
## ..$ dim: int [1:3] 512 512 1
k-means clustering is an unsupervised clustering technique to partition N observations into k clusters in which each observation belongs to the cluster with the nearest mean.
Here, we use the k-means clustering to divide the RGB intensity of the image into three classes.
#Read image: delete the 4th element of 3th dimension
#Load from the R binary
Img <- readRDS(system.file("extdata", "Mouse01_Kid_x20_z0_RR01.Rds", package="rMiW"))
str(Img)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
## ..@ colormode: int 2
## ..$ dim: int [1:3] 1257 1038 3
#Resize 1024x1024 and perform 3 clustering
ImgClus3 <- rMiW::Img2DClustering(x=Img, Cluster = 3, XY=1024)
str(ImgClus3)
## List of 3
## $ Original : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
## $ Cluster : num [1:1257, 1:1038] 0.333 0.333 0.333 0.333 0.333 ...
## $ ClusterNumber: num 3
#Visualize as a color image
rMiW::rasterMiW(ImgClus3, method = "raster")
#Barplot
Calc <- table(unlist(ImgClus3$Cluster)*ImgClus3$ClusterNumber)
barplot(Calc,
ylab="Pixel number of cluster", ylim=c(0, max(Calc)*1.25),
col=colorspace::rainbow_hcl(ImgClus3$ClusterNumber, c = 70))
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Catalina 10.15.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rMiW_0.99.4 EBImage_4.36.0 BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 locfit_1.5-9.4 lattice_0.20-45
## [4] fftwtools_0.9-11 png_0.1-7 visNetwork_2.1.0
## [7] assertthat_0.2.1 zeallot_0.1.0 digest_0.6.28
## [10] utf8_1.2.2 R6_2.5.1 tiff_0.1-8
## [13] evaluate_0.14 highr_0.9 pillar_1.6.4
## [16] tfruns_1.5.0 rlang_0.4.12 whisker_0.4
## [19] jquerylib_0.1.4 magick_2.7.3 Matrix_1.3-4
## [22] reticulate_1.22 rmarkdown_2.11 DiagrammeR_1.0.6.1
## [25] keras_2.6.1 stringr_1.4.0 htmlwidgets_1.5.4
## [28] igraph_1.2.7 RCurl_1.98-1.5 compiler_4.1.1
## [31] xfun_0.27 pkgconfig_2.0.3 BiocGenerics_0.40.0
## [34] base64enc_0.1-3 tensorflow_2.6.0 htmltools_0.5.2
## [37] tidyselect_1.1.1 tibble_3.1.5 bookdown_0.24
## [40] mmand_1.6.1 fansi_0.5.0 crayon_1.4.2
## [43] dplyr_1.0.7 bitops_1.0-7 grid_4.1.1
## [46] jsonlite_1.7.2 lifecycle_1.0.1 DBI_1.1.1
## [49] magrittr_2.0.1 stringi_1.7.5 bslib_0.3.1
## [52] ellipsis_0.3.2 vctrs_0.3.8 generics_0.1.1
## [55] RColorBrewer_1.1-2 tools_4.1.1 glue_1.4.2
## [58] purrr_0.3.4 jpeg_0.1-9 abind_1.4-5
## [61] fastmap_1.1.0 yaml_2.2.1 colorspace_2.0-2
## [64] BiocManager_1.30.16 knitr_1.36 sass_0.4.0