img_center: Center an object in an image

View source: R/img_center.R

img_centerR Documentation

Center an object in an image

Description

Center the input object within an output image of given dimensions, around a set of coordinates (by default the centroid of the object).

Usage

img_center(x, w, h, around = NULL, col = NULL)

Arguments

x

an image, of type imager::cimg().

w, h

the width and height of the output image.

around

a vector with the x,y coordinate of the point around which to center; by default the centroid of the input image.

col

the colour to use for padding; by default the background colour from img_guess_background().

Examples

x <- img_read(system.file("extdata", "amphipoda/33463695.jpg",
              package="morphr"), invert=TRUE)
img_show(x)
# center with all defaults
# = around the center of mass of grey levels
xc <- img_center(x, 350, 500) %>% img_show()
# check that the new centroid is around the middle of the image [175,250]
img_centroid(xc)

# center around the centroid of the binary image
# = does not take grey levels into account
xc <- img_center(x, 350, 500, around=img_centroid(x>0)) %>% img_show()
img_centroid(xc>0)
# the binary centroid is in the middle
img_centroid(xc)
# but the grey levels center of mass is elsewhere

jiho/morphr documentation built on May 11, 2024, 9:32 p.m.