Last data update: 2014.03.03

R: Thresholding
createBinaryImageR Documentation

Thresholding

Description

Creates a binary image from a grayscale image by thresholding.

Usage

createBinaryImage(imgG,img=NULL,method="otsu",threshold=NULL,numWindows=1,whitePixelMask=c())

Arguments

img

An Image object or an array.

imgG

The grey valued Image object.

method

Either "otsu" or "phansalkar"

threshold

Fixed threshold

numWindows

Number of windows to use for threshold calculation.

whitePixelMask

Boolean mask of white pixels, if they should be excluded from thresholding

Details

The functions returns the binary image resulting from the thresholding. If threshold is defined, all pixels smaller than this value will be fixed to 1 all other values will be set to 0. If threshold is undefined, the thresholding value is calculated automatically using 'otsu' or 'phansalkar' thresholding.

The function 'otsu' does Otsu thresholding on the grey level histograms of the image. The function 'phansalkar' does thresholding using the mean and standard deviation of a specified window. The thresholding is done on the RGB as well as the LAP color space and the results are ORed. The window size is dim(img)/numWindows. White pixel can be excluded from thresholding (e.g. if white is background) by defining a whitePixelMask

Value

The binary image.

Author(s)

Henrik Failmezger, failmezger@lmb.uni-muenchen.de

References

Neerad Phansalkar, Sumit More, Ashish Sabale, Dr. Madhuri Joshi, "Adaptive Local Thresholding for Detection of Nuclei in Diversly Stained Cytology Images," 2011 IEEE International Conference in Communications and Signal Processing (ICCSP), pp. 218, 10 Feb. 2011

Nobuyuki Otsu: A threshold selection method from grey level histograms. In: IEEE Transactions on Systems, Man, and Cybernetics. New York 9.1979, S.62-66. ISSN 1083-4419

Examples

f= system.file("extdata", "exImg.jpg", package="CRImage")
img=readImage(f)
#conversion to grayscale
imgG=EBImage::channel(img,"gray")
imgB=createBinaryImage(imgG,img=img,method="otsu",numWindows=4)
#white pixel mask
whitePixelMask=img[,,1]>0.85 & img[,,2]>0.85 & img[,,3]>0.85
#exclude white pixels from thresholding
imgB=createBinaryImage(imgG,img=img,method="otsu",numWindows=4,whitePixelMask)
#phansalkar threshold
imgB=createBinaryImage(imgG,img=img,method="phansalkar",numWindows=4)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(CRImage)
Loading required package: EBImage
Loading required package: DNAcopy
Loading required package: aCGH
Loading required package: cluster
Loading required package: survival
Loading required package: multtest
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit

Loading required package: Biobase
Welcome to Bioconductor

    Vignettes contain introductory material; view with
    'browseVignettes()'. To cite Bioconductor, see
    'citation("Biobase")', and for packages 'citation("pkgname")'.


Attaching package: 'Biobase'

The following object is masked from 'package:EBImage':

    channel


Attaching package: 'aCGH'

The following object is masked from 'package:stats':

    heatmap

> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/CRImage/createBinaryImage.Rd_%03d_medium.png", width=480, height=480)
> ### Name: createBinaryImage
> ### Title: Thresholding
> ### Aliases: createBinaryImage
> ### Keywords: misc
> 
> ### ** Examples
> 
> f= system.file("extdata", "exImg.jpg", package="CRImage")
> img=readImage(f)
> #conversion to grayscale
> imgG=EBImage::channel(img,"gray")
> imgB=createBinaryImage(imgG,img=img,method="otsu",numWindows=4)
thresholding
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
Otsu threshold
> #white pixel mask
> whitePixelMask=img[,,1]>0.85 & img[,,2]>0.85 & img[,,3]>0.85
> #exclude white pixels from thresholding
> imgB=createBinaryImage(imgG,img=img,method="otsu",numWindows=4,whitePixelMask)
thresholding
use fixed threshold
> #phansalkar threshold
> imgB=createBinaryImage(imgG,img=img,method="phansalkar",numWindows=4)
thresholding
Phansalkar threshold
convert RGB to LAB
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>