Skip to contents
  • analyze_objects() provides tools for counting and extracting object features (e.g., area, perimeter, radius, pixel intensity, Haralick texture, veins, and Fourier descriptors) in an image using an ultra-fast, cache-optimized C++ computer vision engine. See more at the Details section.

  • analyze_objects_iter() provides an iterative section to measure object features using an object with a known area.

  • plot.anal_obj() produces a histogram or density plot for object measures or R, G, and B values when argument object_index is used in analyze_objects().

  • analyze_objects2() is maintained as an exact alias for analyze_objects().

Usage

analyze_objects(
  img,
  foreground = NULL,
  background = NULL,
  pick_palettes = FALSE,
  segment_objects = TRUE,
  viewer = get_pliman_viewer(),
  reference = FALSE,
  reference_area = NULL,
  back_fore_index = "R/(G/B)",
  fore_ref_index = "B-R",
  reference_img = NULL,
  reference_larger = FALSE,
  reference_smaller = FALSE,
  pattern = NULL,
  parallel = FALSE,
  workers = NULL,
  watershed = TRUE,
  veins = FALSE,
  sigma_veins = 1,
  veins_thinning = FALSE,
  veins_rel_erode = 0.005,
  ab_angles = FALSE,
  ab_angles_percentiles = c(0.25, 0.75),
  width_at = FALSE,
  width_at_percentiles = c(0.05, 0.25, 0.5, 0.75, 0.95),
  haralick = FALSE,
  har_nbins = 32,
  har_scales = 1,
  har_band = 1,
  smooth = FALSE,
  pcv = FALSE,
  pcv_niter = 100,
  resize = FALSE,
  trim = FALSE,
  fill_hull = FALSE,
  erode = FALSE,
  dilate = FALSE,
  opening = FALSE,
  closing = FALSE,
  filter = FALSE,
  return_exact = FALSE,
  filter_order = c("erode", "dilate", "opening", "closing", "filter", "fill_hull"),
  invert = FALSE,
  object_size = "medium",
  sensitivity = "medium",
  area_mode = c("contour", "pixel"),
  index = "NB",
  r = 1,
  g = 2,
  b = 3,
  re = 4,
  nir = 5,
  swir = 6,
  object_index = NULL,
  pixel_level_index = FALSE,
  return_mask = FALSE,
  efourier = FALSE,
  nharm = 10,
  threshold = "Otsu",
  k = 0.1,
  windowsize = NULL,
  tolerance = NULL,
  extension = NULL,
  lower_noise = 0.1,
  upper_noise = NULL,
  rel_size = NULL,
  lower_size = NULL,
  upper_size = NULL,
  topn_lower = NULL,
  topn_upper = NULL,
  lower_eccent = NULL,
  upper_eccent = NULL,
  lower_circ = NULL,
  upper_circ = NULL,
  randomize = TRUE,
  nrows = 1000,
  plot = TRUE,
  show_original = TRUE,
  show_chull = FALSE,
  show_contour = FALSE,
  show_bbox = FALSE,
  contour_col = "red",
  contour_size = 1,
  show_lw = FALSE,
  show_background = TRUE,
  show_segmentation = FALSE,
  col_foreground = NULL,
  col_background = NULL,
  marker = "point",
  marker_col = "red",
  marker_size = NULL,
  save_image = FALSE,
  max_pixels = 2e+06,
  prefix = "proc_",
  dir_original = NULL,
  dir_processed = NULL,
  verbose = TRUE
)

analyze_objects2(
  img,
  foreground = NULL,
  background = NULL,
  pick_palettes = FALSE,
  segment_objects = TRUE,
  viewer = get_pliman_viewer(),
  reference = FALSE,
  reference_area = NULL,
  back_fore_index = "R/(G/B)",
  fore_ref_index = "B-R",
  reference_img = NULL,
  reference_larger = FALSE,
  reference_smaller = FALSE,
  pattern = NULL,
  parallel = FALSE,
  workers = NULL,
  watershed = TRUE,
  veins = FALSE,
  sigma_veins = 1,
  veins_thinning = FALSE,
  veins_rel_erode = 0.005,
  ab_angles = FALSE,
  ab_angles_percentiles = c(0.25, 0.75),
  width_at = FALSE,
  width_at_percentiles = c(0.05, 0.25, 0.5, 0.75, 0.95),
  haralick = FALSE,
  har_nbins = 32,
  har_scales = 1,
  har_band = 1,
  smooth = FALSE,
  pcv = FALSE,
  pcv_niter = 100,
  resize = FALSE,
  trim = FALSE,
  fill_hull = FALSE,
  erode = FALSE,
  dilate = FALSE,
  opening = FALSE,
  closing = FALSE,
  filter = FALSE,
  return_exact = FALSE,
  filter_order = c("erode", "dilate", "opening", "closing", "filter", "fill_hull"),
  invert = FALSE,
  object_size = "medium",
  sensitivity = "medium",
  area_mode = c("contour", "pixel"),
  index = "NB",
  r = 1,
  g = 2,
  b = 3,
  re = 4,
  nir = 5,
  swir = 6,
  object_index = NULL,
  pixel_level_index = FALSE,
  return_mask = FALSE,
  efourier = FALSE,
  nharm = 10,
  threshold = "Otsu",
  k = 0.1,
  windowsize = NULL,
  tolerance = NULL,
  extension = NULL,
  lower_noise = 0.1,
  upper_noise = NULL,
  rel_size = NULL,
  lower_size = NULL,
  upper_size = NULL,
  topn_lower = NULL,
  topn_upper = NULL,
  lower_eccent = NULL,
  upper_eccent = NULL,
  lower_circ = NULL,
  upper_circ = NULL,
  randomize = TRUE,
  nrows = 1000,
  plot = TRUE,
  show_original = TRUE,
  show_chull = FALSE,
  show_contour = FALSE,
  show_bbox = FALSE,
  contour_col = "red",
  contour_size = 1,
  show_lw = FALSE,
  show_background = TRUE,
  show_segmentation = FALSE,
  col_foreground = NULL,
  col_background = NULL,
  marker = "point",
  marker_col = "red",
  marker_size = NULL,
  save_image = FALSE,
  max_pixels = 2e+06,
  prefix = "proc_",
  dir_original = NULL,
  dir_processed = NULL,
  verbose = TRUE
)

# S3 method for class 'anal_obj'
plot(
  x,
  which = "measure",
  measure = "area",
  type = c("density", "histogram"),
  ...
)

# S3 method for class 'anal_obj_ls'
plot(
  x,
  which = "measure",
  measure = "area",
  type = c("density", "histogram"),
  ...
)

analyze_objects_iter(pattern, known_area, verbose = TRUE, ...)

Arguments

img

The image to be analyzed.

foreground, background, reference_img

A color palette for the foreground, background, and reference object, respectively (optional). If a character is used (e.g., foreground = "fore"), the function will search in the current working directory a valid image named "fore".

pick_palettes

Logical argument indicating whether the user needs to pick up the color palettes for foreground and background for the image.

segment_objects

Segment objects in the image? Defaults to TRUE. If segment_objects = FALSE, the objects are not segmented and the entire image is analyzed (useful for whole-canopy indices).

viewer

The viewer option for interactive plotting.

reference

Logical to indicate if a reference object is present in the image.

reference_area

The known area of the reference object.

back_fore_index

A character value to indicate the index to segment the foreground (objects and reference) from the background. Defaults to "R/(G/B)".

fore_ref_index

A character value to indicate the index to segment objects and the reference object. Defaults to "B-R".

reference_larger, reference_smaller

Logical argument indicating when the larger/smaller object in the image must be used as the reference object.

pattern

A pattern of file name used to identify images to be imported.

parallel

If TRUE processes the images asynchronously in parallel.

workers

Number of parallel processes.

watershed

If TRUE (default) performs watershed-based object detection.

veins

Logical argument indicating whether vein features are computed per object.

sigma_veins

Fine Gaussian scale parameter for vein detection (default 1).

veins_thinning

Logical value indicating whether Guo-Hall skeleton thinning should be applied to detected veins (default FALSE).

veins_rel_erode

Relative boundary erosion fraction of min(width, height) (default 0.005).

ab_angles

Logical argument indicating whether apex and base angles should be computed.

ab_angles_percentiles

The percentiles indicating the heights of the object for angles.

width_at

Logical. If TRUE, widths at quantiles of height are computed.

width_at_percentiles

A vector of heights along the vertical axis of the object.

haralick

Logical value indicating whether Haralick features are computed.

har_nbins

An integer indicating the number of bins for the Haralick matrix. Defaults to 32.

har_scales

An integer vector indicating the number of scales for Haralick features.

har_band

The band to compute Haralick features (1 = R, 2 = G, 3 = B, or "GRAY").

smooth

Whether the object contours should be smoothed with poly_smooth().

pcv

Computes the Perimeter Complexity Value? Defaults to FALSE.

pcv_niter

An integer specifying the number of smoothing iterations for PCV.

resize

Resize the image before processing?

trim

Number of pixels removed from edges in the analysis.

fill_hull

Fill holes in the binary image? Defaults to FALSE.

opening, closing, filter, erode, dilate

Morphological operations (brush size), applied in hierarchical order.

return_exact

Logical. If TRUE, exact morphological reconstruction is performed.

filter_order

Hierarchical order of morphological operations.

invert

Inverts the binary image if desired.

object_size

The size of the object ("small", "medium", "large", "elarge"). Used to automatically set up tolerance and extension parameters.

sensitivity

Sensitivity level for thresholding ("low", "medium", "high", "extreme", or numeric). Defaults to "medium".

area_mode

The method to compute object area ("contour" or "pixel").

index

A character value specifying the index for conversion to binary image.

r, g, b, re, nir

The red, green, blue, red-edge, and near-infrared bands, respectively.

swir

The short-wave infrared band.

object_index

If informed, the average index value for each object is returned.

pixel_level_index

Return indexes computed in object_index at pixel level?

return_mask

Returns the mask for the analyzed image?

efourier

Logical argument indicating if Elliptical Fourier should be computed.

nharm

Number of harmonics for Fourier analysis. Defaults to 10.

threshold

The threshold method ("Otsu", "Adaptive", or numeric).

k, windowsize

Parameters for adaptive thresholding.

tolerance

Minimum height for watershed segmentation.

extension

Neighborhood radius for watershed segmentation.

lower_noise

Objects with area less than lower_noise * mean area are removed. Defaults to 0.10 (i.e., 10% of the mean area).

upper_noise

Objects with area greater than upper_noise * mean area are removed. Defaults to NULL (no upper noise filter).

rel_size

Relative size threshold as a fraction of mean object area.

lower_size, upper_size

Lower and upper size limits for object filtering.

topn_lower, topn_upper

Select top n objects based on area.

lower_eccent, upper_eccent, lower_circ, upper_circ

Eccentricity and circularity limits.

randomize

Randomize rows before training GLM model?

nrows

Number of rows to sample for GLM training.

plot

Show image after processing?

show_original

Show count objects on the original image?

show_chull

Show convex hull around objects?

show_contour

Show contour line around objects?

show_bbox

Show bounding box around objects?

contour_col, contour_size

Color and size for contour line.

show_lw

Plot length and width lines on each object?

show_background

Show the background?

show_segmentation

Shows object segmentation colored with random permutations?

col_foreground, col_background

Foreground and background colors.

marker, marker_col, marker_size

Type, color, and size of object marker.

save_image

Save the image after processing?

max_pixels

Maximum number of pixels to render when plotting or saving.

prefix

Prefix for processed images.

dir_original, dir_processed

Directories for original and processed images.

verbose

If TRUE (default) shows progress summary in console.

x

An object of class anal_obj.

which

Which to plot. Either 'measure' (object measures) or 'index' (object index). Defaults to "measure".

measure

The measure to plot. Defaults to "area".

type

The type of plot. Either "hist" or "density". Partial matches are recognized.

...

Additional arguments passed to methods.

known_area

Known area of template object in analyze_objects_iter().

Value

An object of class anal_obj (single image) or anal_obj_ls (batch).

Details

A binary image is first generated to segment the foreground and background. The argument index is useful to choose a proper index to segment the image (see image_binary() for more details). It is also possible to provide color palettes for background and foreground (arguments background and foreground, respectively). When this is used, a general linear model (binomial family) is fitted to the RGB values to segment fore- and background.

Then, the number of objects in the foreground is counted. By setting up arguments such as lower_size and upper_size, it is possible to set a threshold for lower and upper sizes of the objects, respectively. The argument object_size or sensitivity can be used to set up pre-defined values of tolerance and extension depending on the image resolution. This will influence the watershed-based object segmentation. Users can also tune up tolerance and extension explicitly for a better precision of watershed segmentation.

If watershed = FALSE is used, all pixels for each connected set of foreground pixels in img are set to a unique object. This is faster, especially for a large number of objects, but it is not able to segment touching objects.

There are some ways to correct the measures based on a reference object. If a reference object with a known area (reference_area) is used in the image and reference = TRUE is used, the measures of the objects will be corrected, considering the unit of measure informed in reference_area. There are two main ways to work with reference objects: * The first is to provide a reference object that has a contrasting color with both the background and object of interest. In this case, the arguments back_fore_index and fore_ref_index can be used to define an index to first segment the reference object and objects to be measured from the background, then the reference object from objects to be measured.

* The second one is to use a reference object that has a similar color to the

objects to be measured, but has a contrasting size. For example, if we are counting small brown grains, we can use a brown reference template that has an area larger (says 3 times the area of the grains) and then uses reference_larger = TRUE. With this, the larger object in the image will be used as the reference object. This is particularly useful when images are captured with background light. The argument reference_smaller can be used in the same way.

By using pattern, it is possible to process several images with common pattern names that are stored in the current working directory or in the subdirectory informed in dir_original. To speed up the computation time, one can set parallel = TRUE.

analyze_objects_iter() can be used to process several images using an object with a known area as a template. In this case, all the images in the current working directory that match the pattern will be processed.

  • Additional measures:

    • If haralick = TRUE, the function computes 13 Haralick texture features for each object based on a gray-level co-occurrence matrix (Haralick et al. 1973).

    • If efourier = TRUE is used, an Elliptical Fourier Analysis (Kuhl and Giardina, 1982) is computed for each object contour using efourier().

    • If veins = TRUE, vein features are computed for each object using Difference of Gaussians (DoG) bandpass filtering.

    • If ab_angles = TRUE, the apex and base angles of each object are computed with poly_apex_base_angle().

    • If width_at = TRUE, the widths at given percentiles of object height are computed.

    • If pcv = TRUE, the Perimeter Complexity Value is computed.

References

Haralick, R.M., K. Shanmugam, and I. Dinstein. 1973. Textural Features for Image Classification. IEEE Transactions on Systems, Man, and Cybernetics SMC-3(6): 610–621. doi:10.1109/TSMC.1973.4309314

Kuhl, F. P., and Giardina, C. R. (1982). Elliptic Fourier features of a closed contour. Computer Graphics and Image Processing 18, 236-258.

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

if (FALSE) { # interactive()
library(pliman)
img <- image_pliman("soybean_touch.jpg")
obj <- analyze_objects(img)
obj$statistics

# Top-5 grains with largest area
top <- analyze_objects(img, marker = "id", topn_upper = 5)
top$results

# Reference object correction
img <- image_pliman("flax_grains.jpg")
res <- analyze_objects(img,
                       index = "GRAY",
                       reference = TRUE,
                       reference_area = 6,
                       reference_larger = TRUE,
                       lower_noise = 0.3)
}
if (FALSE) { # interactive()
library(pliman)

img <- image_pliman("soy_green.jpg")
rgb <- analyze_objects(img,
                       marker = "id",
                       object_index = "B",
                       pixel_level_index = TRUE)
# density of area
plot(rgb)

# histogram of perimeter
plot(rgb, measure = "perimeter", type = "histogram")

# density of the blue (B) index
plot(rgb, which = "index")
}