Computes the center of gravity (COG) of a character-like binary image using its outer contour.
cog_contour(img, origin = c("bottomleft", "topleft"))A list containing:
statisticsA data frame with the following components:
center_x, center_y: The (x, y) coordinates of the COG in pixel coordinates of the input image.
center_x_trim, center_y_trim: The COG coordinates relative to the trimmed glyph region, excluding image margins.
center_x_std, center_y_std: The standardized COG coordinates ranging from 0 to 1, based on the trimmed region's width and height.
margin_left, margin_right, margin_top, margin_bottom: Margins between the glyph and the image boundary.
width_original, height_original: Dimensions of the original image.
width_trim, height_trim: Width and height of the trimmed glyph region, excluding margins.
area: The number of pixels inside the convex hull (i.e., the total mass used to compute the COG).
pointsA data frame of (x, y) coordinates representing the contour points of the convex polygon.
An image input, either a file path to an image file (e.g., PNG, JPEG),
or a cimg object from the imager package. The image should be in binary form,
with foreground (glyph) values not equal to 1 and background values equal to 1.
A character string indicating the location of the image origin.
Use "bottomleft" (default) if the y-axis increases upward (Cartesian),
or "topleft" if the y-axis increases downward (as in image arrays).
In the contour-based method, the center of gravity (COG) is defined as the geometrical centroid of the convex hull (the smallest convex polygon) that encloses the stroke region of the character. The convex hull is estimated by tracing the outer contour of the glyph and computing the minimal convex polygon that wraps all stroke pixels, i.e., the foreground pixels whose intensity values are not equal to 1 (pure white). The centroid is then calculated as the arithmetic mean of the (x, y) coordinates of all pixels located within the interior of the convex polygon.
Mathematically, the contour-based center of gravity \((G_x, G_y)\) is defined as the weighted mean of pixel coordinates within the polygon region, where each pixel contributes a value of 1 (unit mass) and background pixels are excluded. Specifically, let \(p(x, y)\) be an indicator function such that \(p(x, y) = 1\) if the pixel \((x, y)\) lies inside the convex polygon and \(p(x, y) = 0\) otherwise. Then the horizontal and vertical components of the COG are computed as:
$$ G_x = (\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y) \cdot x) / (\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y)) $$ $$ G_y = (\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y) \cdot y) / (\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y)) $$
where \(w\) and \(h\) denote the width and height of the image, respectively.
This method was originally proposed by Kotani and colleagues (2004, 2011) and has been used in character analysis and font design to reflect the perceived shape of glyphs more robustly than simple stroke averaging.
Kotani, A. (2011). Contour-based evaluation method of center of gravity on characters and its application to font development. Memoirs of Shonan Institute of Technology, 45(1), 23–33. https://shonan-it.repo.nii.ac.jp/?action=repository_view_main_item_detail&item_id=368
Kotani, A., Asai, Y., Nakamura, Y., Otuka, M., Mituyama, Y., & Onoye, T. (2004). Contour-based evaluation method of center of gravity on “LCFONT.” IPSJ SIG Technical Report, 115, 63–70. https://ipsj.ixsq.nii.ac.jp/records/36793
data(img_A) # load example image from the package
result <- cog_contour(img_A, origin = "bottomleft")
result$statistics # summary data frame
result$points # contour polygon vertices (x, y, angle)
result$origin # image origin specification
Run the code above in your browser using DataLab