Slides a logical mask by down and right pixels and returns
the shifted mask in the same shape as the input. Convenience
wrapper around the manual recipe shown in vignette section 4.5
for tuning an elliptical make_face_mask() region ("full",
"nose", "mouth", "upper_face", "lower_face") on a base
image whose features sit a few pixels off the default geometry.
For the rectangle regions ("eyes", "left_eye",
"right_eye"), prefer the region_bounds argument of
make_face_mask(): passing
region_bounds = c(row_min, row_max, col_min, col_max) in 0-1
image fractions is more precise than counting pixels and is
the canonical tuning route for those regions.
Pixels shifted off the image are dropped, and the newly exposed
edge is filled with FALSE (i.e., outside the mask).
Arguments
- mask
Logical vector of length
prod(img_dims)(column-major, as returned bymake_face_mask()) or a logical matrix.- down, right
Integer pixel offsets. Positive
downmoves the mask towards the bottom of the image; positiverightmoves it towards the right edge. Negative values move up / left. Defaults are0(no shift).- img_dims
Integer
c(nrow, ncol)(or a single integer for a square image). Required whenmaskis a vector; ignored whenmaskis already a matrix.
See also
make_face_mask() (the region_bounds route for the
rectangle regions), plot_mask_overlay(), plot_face_mask().
Examples
# Reshape the default mouth mask, slide it down 20 pixels on a
# 256-pixel image (about 8% of the height), and confirm it
# still has the right number of TRUE pixels.
m <- make_face_mask(c(256L, 256L), region = "mouth")
shifted <- shift_mask(m, down = 20L, img_dims = c(256L, 256L))
identical(sum(m), sum(shifted))
#> [1] TRUE
# Matrix-in, matrix-out.
mat <- matrix(m, 256L, 256L)
shifted_mat <- shift_mask(mat, down = 20L, right = 8L)
dim(shifted_mat)
#> [1] 256 256