Segments image objects using clustering by the k-means clustering algorithm
image_segment_kmeans(
img,
bands = 1:3,
nclasses = 2,
invert = FALSE,
opening = FALSE,
closing = FALSE,
filter = FALSE,
erode = FALSE,
dilate = FALSE,
fill_hull = FALSE,
plot = TRUE
)
A list with the following values:
image
The segmented image considering only two classes (foreground and
background)
clusters
The class of each pixel. For example, if ncluster = 3
,
clusters
will be a two-way matrix with values ranging from 1 to 3.
masks
A list with the binary matrices showing the segmentation.
An Image
object.
A numeric integer/vector indicating the RGB band used in the
segmentation. Defaults to 1:3
, i.e., all the RGB bands are used.
The number of desired classes after image segmentation.
Invert the segmentation? Defaults to FALSE
. If TRUE
the
binary matrix is inverted.
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.
Fill holes in the objects? Defaults to FALSE
.
Plot the segmented image?
Hartigan, J. A. and Wong, M. A. (1979). Algorithm AS 136: A K-means clustering algorithm. Applied Statistics, 28, 100–108. tools:::Rd_expr_doi("10.2307/2346830")
if (interactive() && requireNamespace("EBImage")) {
img <- image_pliman("la_leaves.jpg", plot = TRUE)
seg <- image_segment_kmeans(img)
seg <- image_segment_kmeans(img, fill_hull = TRUE, invert = TRUE, filter = 10)
}
Run the code above in your browser using DataLab