Skip to contents

reduce reduces a 2D Image object to a 1D Image object by treating the image rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of a raster image. It is similar in spirit to the apply function in base R.

Usage

reduce(image, dim, fun = "sum", target = "new")

Arguments

image

An Image object.

dim

The dimension of the image which the function will be applied over. 1 indicates rows (i.e., the image is reduced to a single column), 2 indicates columns (i.e., the image is reduced to a single row).

fun

The function to be applied. It can take the following values:

  • "sum"

  • "mean"

  • "max"

  • "min"

target

The location where the results should be stored. It can take 2 values:

"new":

a new Image object is created and the results are stored inside (the default).

An Image object:

the results are stored in another existing Image object. This is fast and will not replace the content of image but will replace that of target. Note that target must have the same number of channels as image, otherwise it will be coerced to the same number of channels. It must also have the same bitdepth as image if fun is equal to max or min. If fun is equal to sum or mean the bitdepth can be larger to preserve accuracy. If dim=1, target must have 1 column and the same number of rows as image. If dim=2, target must have 1 row and the same number of columns as image.

Value

If target="new", the function returns an Image object. If target is an Image object, the function returns nothing and modifies that Image object in place.

See also

Author

Simon Garnier, garnier@njit.edu

Examples

balloon <- image(system.file("sample_img/balloon1.png", package = "Rvision"))
sum_by_row <- reduce(balloon, 1, "sum")