Type: Package
Title: An R Port of the 'ImageJ' Plugin 'Auto Threshold'
Version: 1.4.2
Maintainer: Rory Nolan <rorynoolan@gmail.com>
Description: Algorithms for automatically finding appropriate thresholds for numerical data, with special functions for thresholding images. Provides the 'ImageJ' 'Auto Threshold' plugin functionality to R users. See https://imagej.net/plugins/auto-threshold and Landini et al. (2017) <doi:10.1111/jmi.12474>.
License: GPL-3
URL: https://rorynolan.github.io/autothresholdr/, https://github.com/rorynolan/autothresholdr#readme
BugReports: https://github.com/rorynolan/autothresholdr/issues
Depends: R (≥ 3.5)
Imports: checkmate (≥ 1.9.3), ijtiff (≥ 2.2), magrittr (≥ 1.5), purrr, Rcpp (≥ 1.0.11), rlang (≥ 0.3.3), stats, strex (≥ 1.4.1), stringr (≥ 1.4)
Suggests: covr, dplyr, ggplot2, knitr, rmarkdown, spelling, styler (≥ 1.3.2), testthat (≥ 3.0), utils
LinkingTo: Rcpp (≥ 1.0.11)
VignetteBuilder: knitr
Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3
NeedsCompilation: yes
Packaged: 2023-12-13 05:28:35 UTC; rorynolan
Author: Rory Nolan ORCID iD [aut, cre, trl], Luis Alvarez ORCID iD [ctb], Sergi Padilla-Parra ORCID iD [ctb, ths], Gabriel Landini ORCID iD [ctb, cph]
Repository: CRAN
Date/Publication: 2023-12-13 06:00:02 UTC

Automatically threshold an array of non-negative integers.

Description

These functions apply the ImageJ "Auto Threshold" plugin's image thresholding methods. The available methods are "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle" and "Yen". Read about them at https://imagej.net/plugins/auto-threshold.

Usage

auto_thresh(
  int_arr,
  method,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

auto_thresh_mask(
  int_arr,
  method,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

auto_thresh_apply_mask(
  int_arr,
  method,
  fail = NA,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

mask(
  int_arr,
  method,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

apply_mask(
  int_arr,
  method,
  fail = NA,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

Arguments

int_arr

An array (or vector) of non-negative integers.

method

The name of the thresholding method you wish to use. The available methods are "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle" and "Yen". Partial matching is performed i.e. method = "h" is enough to get you "Huang" and method = "in" is enough to get you "Intermodes". To perform manual thresholding (where you set the threshold yourself), supply the threshold here as a number e.g. method = 3; so note that this would not select the third method in the above list of methods.

ignore_black

Ignore black pixels/elements (zeros) when performing the thresholding?

ignore_white

Ignore white pixels when performing the thresholding? If set to TRUE, the function makes a good guess as to what the white (saturated) value would be (see 'Details'). If this is set to a number, all pixels with value greater than or equal to that number are ignored.

ignore_na

This should be TRUE if NAs in int_arr should be ignored or FALSE if you want the presence of NAs in int_arr to throw an error.

fail

When using auto_thresh_apply_mask(), to what value do you wish to set the pixels which fail to exceed the threshold? fail = 'saturate' sets them to saturated value (see "Details"). fail = 'zero' sets them to zero. You can also specify directly here a natural number (must be between 0 and 2^16 - 1) to use.

Details

Value

auto_thresh() returns an object of class th containing the threshold value. Pixels exceeding this threshold pass the thresholding, pixels at or below this level fail.

auto_thresh_mask() returns an object of class masked_arr which is a binarized version of the input, with a value of TRUE at points which exceed the threshold and FALSE at those which do not.

auto_thresh_apply_mask() returns and object of class threshed_arr which is the original input masked by the threshold, i.e. all points not exceeding the threshold are set to a user-defined value (default NA).

mask() is the same as auto_thresh_mask() and apply_mask() is the same as auto_thresh_apply_mask().

Acknowledgements

Gabriel Landini coded all of these functions in Java. These java functions were then translated to C++.

References

Examples


img_location <- system.file("extdata", "eg.tif", package = "autothresholdr")
img <- ijtiff::read_tif(img_location)
auto_thresh(img, "huang")
img_value_count <- magrittr::set_names(as.data.frame(table(img)),
                                      c("value", "n"))
print(head(img_value_count))
auto_thresh(img_value_count, "Huang")
auto_thresh(img, "tri")
auto_thresh(img, "Otsu")
auto_thresh(img, 9)
mask <- auto_thresh_mask(img, "huang")
ijtiff::display(mask[, , 1, 1])
masked <- auto_thresh_apply_mask(img, "huang")
ijtiff::display(masked[, , 1, 1])
masked <- auto_thresh_apply_mask(img, 25)
ijtiff::display(masked[, , 1, 1])


Masked array class.

Description

A mask of an array with respect to a given threshold is found by taking the original array and setting all elements falling below the threshold to FALSE and the others to TRUE. An object of class masked_arr has the attribute thresh detailing the threshold value that was applied.

Usage

masked_arr(arr, thresh)

Arguments

arr

An array of logicals (the mask).

thresh

The threshold. Either a scalar or an object of class th.

Value

An object of class masked_arr.


Threshold every image frame in an image stack based on their mean.

Description

An ijtiff_img is a 4-dimensional array indexed by img[y, x, channel, frame]. For each channel (which consists of a stack of frames), this function finds a threshold based on the sum all of the frames, uses this to create a mask and then applies this mask to every frame in the stack (so for a given pillar in the image stack, either all the pixels therein are thresholded away or all are untouched, where pillar ⁠x,y⁠ of channel ch is img[y, x, ch, ]).

Usage

mean_stack_thresh(
  img,
  method,
  fail = NA,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

Arguments

img

A 4-dimensional array in the style of an ijtiff_img (indexed by img[y, x, channel, frame]) or a 3-dimensional array which is a single channel of an ijtiff_img (indexed by img[y, x, frame]).

method

The name of the thresholding method you wish to use. The available methods are "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle" and "Yen". Partial matching is performed i.e. method = "h" is enough to get you "Huang" and method = "in" is enough to get you "Intermodes". To perform manual thresholding (where you set the threshold yourself), supply the threshold here as a number e.g. method = 3.8 (so note that this would not select the third method in the above list of methods). This manual threshold will then be used to threshold the sum stack to create a 2D mask and then this mask will be applied to all frames in the stack. If you want a different method for each channel, specify this parameter as a vector or list, one element per channel.

fail

When using auto_thresh_apply_mask(), to what value do you wish to set the pixels which fail to exceed the threshold? fail = 'saturate' sets them to saturated value (see 'Details'). fail = 'zero' sets them to zero. You can also specify directly here a natural number (must be between 0 and 2^16 - 1) to use.

ignore_black

Ignore black pixels/elements (zeros) when performing the thresholding?

ignore_white

Ignore white pixels when performing the thresholding? If set to TRUE, the function makes a good guess as to what the white (saturated) value would be (see 'Details'). If this is set to a number, all pixels with value greater than or equal to that number are ignored.

ignore_na

This should be TRUE if NAs in int_arr should be ignored or FALSE if you want the presence of NAs in int_arr to throw an error.

Details

It's called mean_stack_thresh() and not sum_stack_thresh() because its easier for people to visualize the mean of an image series than to visualize the sum, but for the sake of this procedure, both are equivalent, except for the fact that the thresholding routine invoked inside this function prefers integers, which we get by using a sum but not by using a mean.

Value

An object of class stack_threshed_img which is the thresholded image (an array in the style of an ijtiff_img). Pillars not exceeding the threshold are set to the fail value (default NA).

Examples


img <- ijtiff::read_tif(system.file("extdata", "50.tif",
  package = "autothresholdr"
))
ijtiff::display(img[, , 1, 1])
img_thresh_mask <- mean_stack_thresh(img, "Otsu")
ijtiff::display(img_thresh_mask[, , 1, 1])
ijtiff::display(img[, , 1, 1])
img_thresh_mask <- mean_stack_thresh(img, "Huang")
ijtiff::display(img_thresh_mask[, , 1, 1])


Threshold every image frame in a stack based on their median.

Description

An ijtiff_img is a 4-dimensional array indexed by img[y, x, channel, frame]. For each channel (which consists of a stack of frames), this function finds a threshold based on all of the frames, then takes the median of all the frames in the stack image, uses this to create a mask with the found threshold and then applies this mask to every frame in the stack (so for a given pillar in the image stack, either all the pixels therein are thresholded away or all are untouched, where pillar ⁠x,y⁠ of channel ch is img[y, x, ch, ]).

Usage

med_stack_thresh(
  img,
  method,
  fail = NA,
  ignore_black = FALSE,
  ignore_white = FALSE,
  ignore_na = FALSE
)

Arguments

img

A 3-dimensional array (the image stack, possibly a time-series of images) where the nth slice is the nth image in the stack.

method

The name of the thresholding method you wish to use. The available methods are "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle" and "Yen". Partial matching is performed i.e. method = "h" is enough to get you "Huang" and method = "in" is enough to get you "Intermodes". To perform manual thresholding (where you set the threshold yourself), supply the threshold here as a number e.g. method = 3 (so note that this would not select the third method in the above list of methods). This manual threshold will then be used to threshold the median stack to create a 2D mask and then this mask will be applied to all frames in the stack. If you want a different method for each channel, specify this parameter as a vector or list, one element per channel.

fail

When using auto_thresh_apply_mask(), to what value do you wish to set the pixels which fail to exceed the threshold? fail = 'saturate' sets them to saturated value (see 'Details'). fail = 'zero' sets them to zero. You can also specify directly here a natural number (must be between 0 and 2^32 - 1) to use.

ignore_black

Ignore black pixels/elements (zeros) when performing the thresholding?

ignore_white

Ignore white pixels when performing the thresholding? If set to TRUE, the function makes a good guess as to what the white (saturated) value would be (see 'Details').

ignore_na

This should be TRUE if NAs in int_arr should be ignored or FALSE if you want the presence of NAs in int_arr to throw an error.

Details

Value

An object of class stack_threshed_img which is the thresholded image (an array in the style of an ijtiff_img). Pillars not exceeding the threshold are set to the fail value (default NA).

Examples


img <- ijtiff::read_tif(system.file("extdata", "50.tif",
  package = "autothresholdr"
))
ijtiff::display(img[, , 1, 1])
img_thresh_mask <- med_stack_thresh(img, "Otsu")
ijtiff::display(img_thresh_mask[, , 1, 1])
ijtiff::display(img[, , 1, 1])
img_thresh_mask <- med_stack_thresh(img, "Triangle")
ijtiff::display(img_thresh_mask[, , 1, 1])


Stack-thresholded image class.

Description

A stack-thresholded array is an array which has had stack-thresholding applied to it. See mean_stack_thresh(). It has 3 necessary attributes:

Usage

stack_threshed_img(img, thresh, fail_value, stack_thresh_method)

Arguments

img

A 4-dimensional array in the style of an ijtiff_img (indexed by img[y, x, channel, frame]) or a 3-dimensional array which is a single channel of an ijtiff_img (indexed by img[y, x, frame]).

thresh

The threshold that was used. Either a number or an object of class th.

fail_value

The value to which elements of the array which failed the thresholding were set.

stack_thresh_method

This must be set to either "mean" or "median" to tell which stacked-thresholding method was employed.

Value

An object of class stack_threshed_img.

See Also

threshed_arr, mean_stack_thresh(), med_stack_thresh().


Automatically found threshold class.

Description

A threshold found automatically via auto_thresh(). It is a number (the value of the threshold) with 4 attributes:

Usage

th(thresh, ignore_black, ignore_white, ignore_na, autothresh_method)

Arguments

thresh

A scalar. The threshold.

ignore_black

TRUE if black values were ignored during the thresholding and FALSE otherwise.

ignore_white

TRUE if white values were ignored during the thresholding and FALSE otherwise.

ignore_na

TRUE if NA values were ignored during the thresholding and FALSE otherwise.

autothresh_method

The name of the automatic thresholding method used.

Value

An object of class th.


Thresholded array class.

Description

A thresholded array is an array which has had a threshold applied to it. It has an attribute thresh which is the threshold that was applied which can be a number or an object of class th.

Usage

threshed_arr(arr, thresh)

Arguments

arr

The thresholded array (not the original array).

thresh

The threshold that was used. Either a number or an object of class th.

Details

The term 'array' is used loosely here in that vectors and matrices qualify as arrays.

Value

An object of class threshed_arr.

See Also

stack_threshed_img, apply_mask().