A Julia package for analyzing a one-dimensional histogram and automatically choosing a threshold which partitions the histogram into two parts.
A full list of algorithms can be found in the documentation. The algorithms were devised in the context of image processing applications but could prove useful in a variety of scenarios.
The general usage pattern is:
t = find_threshold(histogram::AbstractArray, edges::AbstractRange, algorithm::AbstractThresholdAlgorithm)
where length(histogram)
must match length(edges)
. You can use the build_histogram
function to construct the histogram.
Alternatively, you can supply an image and the histogram is built implicitly:
t = find_threshold(img, algorithm::AbstractThresholdAlgorithm)
Suppose one wants to binarize an image. Binarization requires choosing a grey level (a threshold t
) such that all pixel intensities below that threshold are set to black and all intensities equal or above the threshold are set to white. One can attempt to choose a reasonable threshold automatically by analyzing the distribution of intensities in the image.
using HistogramThresholding
using TestImages # For the moonsurface image.
img = testimage("moonsurface")
edges, counts = build_histogram(img,256)
#=
The `counts` array stores at index 0 the frequencies that were below the
first bin edge. Since we are seeking a threshold over the interval
partitioned by `edges` we need to discard the first bin in `counts`
so that the dimensions of `edges` and `counts` match.
=#
t = find_threshold(counts[1:end], edges, UnimodalRosin())
# The threshold `t` can now be used to determine which intensities should be
# set to 0 (black), and which intensities should be set to 1 (white).