Learn R Programming

coglyphr (version 1.0.4)

cog_stroke: Compute Stroke-Based Center of Gravity (COG)

Description

Computes the center of gravity (COG) of a character-like binary image using its stroke region.

Usage

cog_stroke(img, origin = c("bottomleft", "topleft"))

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).

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).

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

Run this code
data(img_A) # load example image from the package
result <- cog_stroke(img_A, origin = "bottomleft")

result$statistics # summary data frame
head(result$strokes) # stroke pixel coordinates
result$origin # image origin specification

Run the code above in your browser using DataLab