Computes the center of gravity (COG) of a character-like binary image using its stroke region.
Usage
cog_stroke(img, origin = c("bottomleft", "topleft"))
Arguments
- img
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.- origin
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).
Value
A list containing:
statistics
A 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 in the stroke region (i.e., the total mass used to compute the COG).
strokes
A data frame of (x, y) coordinates representing the stroke region (i.e., non-white pixels).
Details
In the stroke-based method, the COG is defined as the arithmetic mean of the (x, y) coordinates of all pixels that belong to the stroke region, i.e., the foreground pixels whose intensity values are not equal to 1 (pure white). This approach assumes that each stroke pixel has unit mass and contributes equally to the center calculation. The image is assumed to be binary, where the background pixels have a value of 1, and all other pixels are treated as part of the glyph.
Mathematically, the stroke-based center of gravity \((G_x, G_y)\) is defined as the weighted mean of pixel coordinates within the stroke 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)\) belongs to the stroke region, and \(p(x, y) = 0\) otherwise. Then the horizontal and vertical components of the COG are computed as:
$$ G_x = \frac{\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y)\, x}{\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y)} $$ $$ G_y = \frac{\sum_{x=1}^{w} \sum_{y=1}^{h} p(x, y)\, 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.
Examples
data(img_A) # load example image from the package
result <- cog_stroke(img_A, origin = "bottomleft")
result$statistics # summary data frame
#> center_x center_y center_x_trim center_y_trim center_x_std center_y_std
#> 1 250.9668 233.1143 189.9668 204.1143 0.5012317 0.4628442
#> margin_left margin_right margin_top margin_bottom width_original
#> 1 61 61 31 29 500
#> height_original width_trim height_trim area
#> 1 500 378 440 49444
head(result$strokes) # stroke pixel coordinates
#> x y value
#> 1 223 32 0
#> 2 224 32 0
#> 3 225 32 0
#> 4 226 32 0
#> 5 227 32 0
#> 6 228 32 0
result$origin # image origin specification
#> [1] "bottomleft"