Getting started
A
polygonis a plane figure that is described by a finite number of straight-line segments connected to form a closed polygonal chain (Singer, 1993)1.
Given the above, we may conclude that image objects can be expressed
as polygons with n vertices. pliman has a set
of useful functions (draw_*()) to draw common shapes such
as circles, squares, triangles, rectangles and n-tagons.
Another group of poly_*() functions can be used to analyze
polygons. Let’s start with a simple example, related to the area and
perimeter of a square.
library(pliman)
#> ╭ Welcome to pliman version "4.0.0"! ──────────────────────────────╮
#> │ │
#> │ Developed collaboratively by NEPEM <https://nepemufsc.com> │
#> │ Group lead: Prof. Tiago Olivoto │
#> │ For citation, type `citation('pliman')` │
#> │ We welcome your feedback and suggestions! │
#> │ │
#> ╰────────────── Simplifying high-throughput plant phenotyping in R ╯
#>
#> Attaching package: 'pliman'
#> The following objects are masked from 'package:graphics':
#>
#> contour, image
square <- draw_square(side = 1)
poly_area(square)
#> [1] 1
poly_perimeter(square)
#> [1] 3Now, Let’s see what happens when we start with a hexagon and increase the number of sides up to 1000.
shapes <- list(side6 <- draw_n_tagon(6, plot = FALSE),
side12 <- draw_n_tagon(12, plot = FALSE),
side24 <- draw_n_tagon(24, plot = FALSE),
side100 <- draw_n_tagon(100, plot = FALSE),
side500 <- draw_n_tagon(500, plot = FALSE),
side100 <- draw_n_tagon(1000, plot = FALSE))
plot_polygon(shapes, merge = FALSE)
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> NULL
#>
#> [[4]]
#> NULL
#>
#> [[5]]
#> NULL
#>
#> [[6]]
#> NULL
poly_area(shapes)
#> [1] 2.598076 3.000000 3.105829 3.139526 3.141510 3.141572
poly_perimeter(shapes)
#> [1] 5.000000 5.694019 6.004205 6.219330 6.270578 6.276892
Note that when
,
the sum of sides becomes the circumference of the circle, given by
,
and the area becomes
.
This is cool, but pliman is mainly designed to analyze
plant image analysis. So, why would we use polygons? Let’s see how we
can use these functions to obtain useful information.
leaves <- image_pliman("leaves.jpeg", plot = TRUE)
cont <- object_contour(leaves, watershed = FALSE, index = "HI")
# plotting the polygon
plot_polygon(cont)
Object measures
Nice! We can use the contour of any object to obtain useful information related to its shape. To reduce the amount of output, I will only use five of them.
cont <- cont[order(lengths(cont), decreasing = TRUE)[1:5]]
plot_polygon(cont)
In the current version of pliman, you will be able to
compute the following measures. For more details, see Chen & Wang
(2005)2,
Claude (2008)3, and Montero et al. 20094.
Area
The area of a shape is computed using Shoelace Formula (Lee and Lim, 2017)5, as follows
poly_area(cont)
#> [1] 1020578.5 177.0 90.5 82.0 74.0Perimeter
The perimeter is computed as the sum of the euclidean distance
between every point of a shape. The distances can be obtained with
poly_distpts().
poly_perimeter(cont)
#> 1 95 17 115 135
#> 4503.66400 117.94113 70.52691 66.28427 60.48528
# perimeter of a circle with radius equals to 2
circle <- draw_circle(radius = 2, plot = FALSE)
poly_perimeter(circle)
#> [1] 12.56635
# check the result
2*pi*2
#> [1] 12.56637Radius
The radius of a pixel in the object contour is computed as its
distance to the object centroid (also called as ‘center of mass’). These
distances can be obtained with poly_centdist(). The
average, maximum and minimum radius can be obtained.
dists <- poly_centdist(cont)
# statistics for the radius
mean_list(dists)
#> 1 95 17 115 135
#> 580.048069 11.729163 5.649805 6.020957 6.418077
min_list(dists)
#> 1 95 17 115 135
#> 412.9528481 0.2849949 0.8549909 1.0618614 0.3639137
max_list(dists)
#> 1 95 17 115 135
#> 775.540691 21.879120 8.807367 10.609818 11.687900
sd_list(dists)
#> 1 95 17 115 135
#> 85.630135 5.949371 1.981965 2.455154 3.065029
# average radius of the circle above
poly_centdist(circle) |> mean_list()
#> [1] 1.999998Length and width
The length and width of an object are computed with
poly_lw() as the difference between the maximum and minimum
of x and y coordinates after the object has
been aligned with poly_align().
aligned <- poly_align(cont)
# compute length and width
poly_lw(cont)
#> length width
#> [1,] 1305.36943 1131.394445
#> [2,] 41.29059 10.856791
#> [3,] 15.52387 15.272231
#> [4,] 19.84345 11.756852
#> [5,] 22.56955 8.434874Circularity, eccentricity, caliper, and elongation
Circularity measure (Montero et al. 2009)6 is also called shape compactness, or roundness measure of an object. It is given by , where is the perimeter and is the area of the object.
poly_circularity(cont)
#> 1 95 17 115 135
#> 19.87401 78.58819 54.96183 53.58054 49.43877Since the above measure is dependent on the scale, the normalized circularity can be used. In this case, it is assumed to be unity for a circle. This measure is invariant under translation, rotation, scaling transformations, and dimensionless. It is given by:
poly_circularity_norm(cont)
#> 1 95 17 115 135
#> 0.6323017 0.1599015 0.2286382 0.2345323 0.2541805
# normalized circularity for different shapes
draw_square(plot = FALSE) |> poly_circularity_norm()
#> [1] 1.396263
draw_circle(plot = FALSE) |> poly_circularity_norm()
#> [1] 0.9999967poly_circularity_haralick() computes the Haralick’s
circularity (CH). The method is based on the computation of all the
Euclidean distances from the object centroid to each boundary pixel.
With this set of distances, the mean
()
and the standard deviation
()
are computed. These statistical parameters are used on a ratio that
calculates the circularity, CH, of a shape, as
poly_circularity_haralick(cont)
#> 1 95 17 115 135
#> 6.773878 1.971496 2.850608 2.452374 2.093970poly_convexity() Computes the convexity of a shape using
a ratio between the perimeter of the convex hull and the perimeter of
the polygon.
poly_convexity(cont)
#> 1 95 17 115 135
#> 0.7022901 0.5186956 0.5207442 0.5148885 0.8031362poly_eccentricity() Computes the eccentricity of a shape
using the ratio of the eigenvalues (inertia axes of coordinates).
poly_eccentricity(cont)
#> [,1]
#> [1,] 0.69689131
#> [2,] 0.04774861
#> [3,] 0.81567653
#> [4,] 0.31765702
#> [5,] 0.12387388poly_elongation() Computes the elongation of a shape as
1 - width / length
poly_elongation(cont)
#> [,1]
#> [1,] 0.13327644
#> [2,] 0.73706381
#> [3,] 0.01621003
#> [4,] 0.40751972
#> [5,] 0.62627193poly_caliper() Computes the caliper (Also called the
Feret’s diameter).
poly_caliper(cont)
#> 1 95 17 115 135
#> 1439.35576 41.43670 15.52417 19.92486 22.67157Users can use the function poly_measures() to compute
most of the object measures in a single call.
(measures <- poly_measures(cont))
#> id x y area area_ch perimeter radius_mean radius_min
#> 1 1 572.1465 451.8326 1020578.5 1022024.5 4503.66400 580.048069 412.9528481
#> 2 2 474.8192 627.8625 177.0 330.5 117.94113 11.729163 0.2849949
#> 3 3 435.5691 380.4622 90.5 149.5 70.52691 5.649805 0.8549909
#> 4 4 416.6199 719.3516 82.0 151.0 66.28427 6.020957 1.0618614
#> 5 5 336.9865 816.8378 74.0 139.0 60.48528 6.418077 0.3639137
#> radius_max radius_sd radius_ratio diam_mean diam_min diam_max
#> 1 775.540691 85.630135 1.878037 1160.09614 825.9056962 1551.08138
#> 2 21.879120 5.949371 76.770204 23.45833 0.5699899 43.75824
#> 3 8.807367 1.981965 10.301125 11.29961 1.7099817 17.61473
#> 4 10.609818 2.455154 9.991716 12.04191 2.1237227 21.21964
#> 5 11.687900 3.065029 32.117227 12.83615 0.7278275 23.37580
#> caliper length width solidity convexity elongation circularity
#> 1 1449.47232 1305.36943 1131.394445 0.9985852 0.9019053 0.13327644 19.87401
#> 2 41.43670 41.29059 10.856791 0.5355522 0.7815386 0.73706381 78.58819
#> 3 15.52417 15.52387 15.272231 0.6053512 0.6475649 0.01621003 54.96183
#> 4 19.92486 19.84345 11.756852 0.5430464 0.7581516 0.40751972 53.58054
#> 5 22.67157 22.56955 8.434874 0.5323741 0.8498984 0.62627193 49.43877
#> circularity_haralick circularity_norm eccentricity maj_axis min_axis
#> 1 6.773878 0.6323017 0.5505531 450.108779 375.750882
#> 2 1.971496 0.1599015 0.9758337 12.836500 2.804962
#> 3 2.850608 0.2286382 0.4293291 4.439479 4.009507
#> 4 2.452374 0.2345323 0.8260405 5.657693 3.188737
#> 5 2.093970 0.2541805 0.9360161 6.698419 2.357557
#> theta pcv
#> 1 -0.2265245 NA
#> 2 -1.5094870 NA
#> 3 0.2543769 NA
#> 4 -0.3963076 NA
#> 5 -0.7529142 NAIf the image resolution is known, then, the measures can be corrected
with get_measures(). The image resolution can be obtained
using a known distance in the image. In the example, the white square
has a side of 5 cm. So, using dpi() the resolution can be
obtained. In this case, the dpi is ~50.
(measures_cor <- get_measures(measures, dpi = 50))
#> id x y area area_ch perimeter radius_mean radius_min
#> 1 1 572.1465 451.8326 2633.74570 2637.47731 228.78613 29.46644 20.97800
#> 2 2 474.8192 627.8625 0.45677 0.85290 5.99141 0.59584 0.01448
#> 3 3 435.5691 380.4622 0.23355 0.38581 3.58277 0.28701 0.04343
#> 4 4 416.6199 719.3516 0.21161 0.38968 3.36724 0.30586 0.05394
#> 5 5 336.9865 816.8378 0.19097 0.35871 3.07265 0.32604 0.01849
#> radius_max radius_sd radius_ratio diam_mean diam_min diam_max caliper
#> 1 39.39747 4.35001 0.09540 58.93288 41.95601 78.79493 73.63319
#> 2 1.11146 0.30223 3.89993 1.19168 0.02896 2.22292 2.10498
#> 3 0.44741 0.10068 0.52330 0.57402 0.08687 0.89483 0.78863
#> 4 0.53898 0.12472 0.50758 0.61173 0.10789 1.07796 1.01218
#> 5 0.59375 0.15570 1.63156 0.65208 0.03697 1.18749 1.15172
#> length width solidity convexity elongation circularity
#> 1 66.31277 57.47484 0.05073 0.90191 0.13328 19.87401
#> 2 2.09756 0.55152 0.02721 0.78154 0.73706 78.58819
#> 3 0.78861 0.77583 0.03075 0.64756 0.01621 54.96183
#> 4 1.00805 0.59725 0.02759 0.75815 0.40752 53.58054
#> 5 1.14653 0.42849 0.02704 0.84990 0.62627 49.43877
#> circularity_haralick circularity_norm eccentricity maj_axis min_axis
#> 1 6.77388 0.63230 0.55055 450.10878 375.75088
#> 2 1.97150 0.15990 0.97583 12.83650 2.80496
#> 3 2.85061 0.22864 0.42933 4.43948 4.00951
#> 4 2.45237 0.23453 0.82604 5.65769 3.18874
#> 5 2.09397 0.25418 0.93602 6.69842 2.35756
#> theta pcv pcv
#> 1 -0.22652 NA NA
#> 2 -1.50949 NA NA
#> 3 0.25438 NA NA
#> 4 -0.39631 NA NA
#> 5 -0.75291 NA NAA little bit more!
Some useful functions can be used to manipulate coordinates. In the
following example, I will show some features implemented in
pliman. Just for simplicity, I will use only object 2.
o2 <- cont[[2]]
plot_polygon(o2)
Rotate polygons
poly_rotate() can be used to rotate the polygon
coordinates by a angle (0-360 degrees) in the trigonometric
direction (anti-clockwise).
rot <- poly_rotate(o2, angle = 45)
Flip polygons
poly_flip_x() and poly_flip_y() can be used
to flip shapes along the x and y axis, respectively.
flip <- list(
fx = poly_flip_x(o2),
fy = poly_flip_y(o2)
)
plot_polygon(flip, merge = FALSE, aspect_ratio = 1)
#> $fx
#> NULL
#>
#> $fy
#> NULL
Sample points
poly_sample() samples n coordinates among
existing points, and poly_sample_prop() samples a
proportion of coordinates among existing.
# sample 50 coordinates
poly_sample(o2, n = 50) |> plot_polygon()
# sample 10% of the coordinates
poly_sample_prop(o2, prop = 0.1) |> plot_polygon()
Smooth polygons
poly_smooth() smooths a polygon contour by combining
sampling prop coordinate points and interpolating them
using vertices vertices.
smooths <-
list(
s1 <- poly_smooth(o2, prop = 0.2, plot = FALSE),
s2 <- poly_smooth(o2, prop = 0.1, plot = FALSE),
s1 <- poly_smooth(o2, prop = 0.05, plot = FALSE)
)
plot_polygon(smooths, merge = FALSE, ncol = 3)
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> NULL
Add noise to a polygon
poly_jitter() adds a small amount of noise to a set of
point coordinates. See base::jitter() for more details.
poly_jitter(o2, noise_x = 5, noise_y = 5) |> plot_polygon()
