All pixels for each connected set of foreground (non-zero) pixels in x are
set to an unique increasing integer, starting from 1. Hence, max(x) gives the
number of connected objects in x. This is a wrapper to EBImage::bwlabel or
EBImage::watershed (if watershed = TRUE
).
Usage
object_label(
img,
index = "B",
invert = FALSE,
fill_hull = FALSE,
threshold = "Otsu",
k = 0.1,
windowsize = NULL,
opening = FALSE,
closing = FALSE,
filter = FALSE,
erode = FALSE,
dilate = FALSE,
watershed = FALSE,
tolerance = NULL,
extension = NULL,
object_size = "medium",
plot = TRUE,
ncol = NULL,
nrow = NULL,
verbose = TRUE
)
Arguments
- img
An image object.
- index
A character value (or a vector of characters) specifying the target mode for conversion to binary image. See the available indexes with
pliman_indexes()
andimage_index()
for more details.- invert
Inverts the binary image, if desired.
- fill_hull
Fill holes in the objects? Defaults to
FALSE
.- threshold
The theshold method to be used.
By default (
threshold = "Otsu"
), a threshold value based on Otsu's method is used to reduce the grayscale image to a binary image. If a numeric value is informed, this value will be used as a threshold.If
threshold = "adaptive"
, adaptive thresholding (Shafait et al. 2008) is used, and will depend on thek
andwindowsize
arguments.If any non-numeric value different than
"Otsu"
and"adaptive"
is used, an iterative section will allow you to choose the threshold based on a raster plot showing pixel intensity of the index.
- k
a numeric in the range 0-1. when
k
is high, local threshold values tend to be lower. whenk
is low, local threshold value tend to be higher.- windowsize
windowsize controls the number of local neighborhood in adaptive thresholding. By default it is set to
1/3 * minxy
, whereminxy
is the minimum dimension of the image (in pixels).- erode, dilate, opening, closing, filter
Morphological operations (brush size)
dilate
puts the mask over every background pixel, and sets it to foreground if any of the pixels covered by the mask is from the foreground.erode
puts the mask over every foreground pixel, and sets it to background if any of the pixels covered by the mask is from the background.opening
performs an erosion followed by a dilation. This helps to remove small objects while preserving the shape and size of larger objects.closing
performs a dilatation followed by an erosion. This helps to fill small holes while preserving the shape and size of larger objects.filter
performs median filtering in the binary image. Provide a positive integer > 1 to indicate the size of the median filtering. Higher values are more efficient to remove noise in the background but can dramatically impact the perimeter of objects, mainly for irregular perimeters such as leaves with serrated edges.
Hierarchically, the operations are performed as opening > closing > filter. The value declared in each argument will define the brush size.
- watershed
If
TRUE
(default) performs watershed-based object detection. This will detect objects even when they are touching one other. IfFALSE
, all pixels for each connected set of foreground pixels are set to a unique object. This is faster but is not able to segment touching objects.- tolerance
The minimum height of the object in the units of image intensity between its highest point (seed) and the point where it contacts another object (checked for every contact pixel). If the height is smaller than the tolerance, the object will be combined with one of its neighbors, which is the highest.
- extension
Radius of the neighborhood in pixels for the detection of neighboring objects. Higher value smooths out small objects.
- object_size
The size of the object. Used to automatically set up
tolerance
andextension
parameters. One of the following."small"
(e.g, wheat grains),"medium"
(e.g, soybean grains),"large"
(e.g, peanut grains), and"elarge"
(e.g, soybean pods)`.- plot
Show image after processing?
- nrow, ncol
The number of rows or columns in the plot grid. Defaults to
NULL
, i.e., a square grid is produced.- verbose
If
TRUE
(default) a summary is shown in the console.
Examples
if (interactive() && requireNamespace("EBImage")) {
img <- image_pliman("soybean_touch.jpg")
# segment the objects using the "B" (blue) band.
object_label(img, index = "B")
object_label(img, index = "B", watershed = TRUE)
}