vignettes/rMiW_01_Basic.Rmd
rMiW_01_Basic.Rmd
Last modified: 2021-11-04 21:30:18
Compiled: 2021-11-04 21:31:22
#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 )
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
#Read image
Img <- EBImage::readImage(files = file)
str(Img)
Here, ::
in R script indicates an explicit relation between the package and the functions.
In this section, we will handle the Image object of EBIimage and perform image processing.
#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()
#Read Rds format
ImgRds <- readRDS("./Img.Rds")
str(ImgRds)
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)
#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 deformed
file <- system.file("extdata", "Cell_Img.Rds", package="rMiW")
file
#Read image
CellImg <- readRDS(file)
str(CellImg)
#drop=TRUE
CellImgT <- CellImg$X[1:512,1:512,,drop=TRUE]
str(CellImgT)
#drop=FALSE
CellImgF <- CellImg$X[1:512,1:512,,drop=FALSE]
str(CellImgF)
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)
#Resize 1024x1024 and perform 3 clustering
ImgClus3 <- rMiW::Img2DClustering(x=Img, Cluster = 3, XY=1024)
str(ImgClus3)
#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-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocStyle_2.20.2
##
## loaded via a namespace (and not attached):
## [1] bookdown_0.24 rprojroot_2.0.2 digest_0.6.28
## [4] crayon_1.4.2 R6_2.5.1 magrittr_2.0.1
## [7] evaluate_0.14 stringi_1.7.5 rlang_0.4.12
## [10] cachem_1.0.6 fs_1.5.0 jquerylib_0.1.4
## [13] ragg_1.2.0 rmarkdown_2.11 pkgdown_1.6.1
## [16] textshaping_0.3.6 desc_1.4.0 tools_4.1.1
## [19] stringr_1.4.0 yaml_2.2.1 xfun_0.27
## [22] fastmap_1.1.0 compiler_4.1.1 systemfonts_1.0.3
## [25] BiocManager_1.30.16 memoise_2.0.0 htmltools_0.5.2
## [28] knitr_1.36