Skip to content

paithiov909/aznyan

Repository files navigation

aznyan

R-CMD-check

aznyan is a collection of image filters for R that wraps OpenCV, ported from 5PB-3-4/AviUtl_OpenCV_Scripts.

Still in development. It will probably work, but the API is likely to change.

Usage

aznyan provides functions that take a nativeRaster of image data as their first argument and return a nativeRaster after applying the effect. All these functions return a new nativeRaster and do not modify the input object in place.

A nativeRaster is a built-in datatype in R that stores image data as an integer matrix. There are several common ways to obtain a nativeRaster object:

  • Generate or manipulate a nativeRaster using nara
  • Capture the contents of a graphics device with ragg::agg_capture()
  • Read image files using functions such as
    • jpeg::readJPEG(native = TRUE)
    • png::readPNG(native = TRUE)
    • fastpng::read_png(type = "nativeraster")
    • aznyan::read_still()

For example, you can read a PNG image as a nativeRaster like this:

library(ggplot2)
library(patchwork) # for layouting native rasters

png <-
  aznyan::read_still(system.file("images/aznyan-256x256.png", package = "aznyan"))

This png object corresponds to the following image:

original image

With aznyan, you can easily apply various effects to a nativeRaster image.

blur <-
  wrap_elements(full = aznyan::median_blur(png, ksize = 8)) +
  labs(title = "Median Blur")
diffusion <-
  wrap_elements(full = aznyan::diffusion_filter(png, factor = 8)) +
  labs(title = "Diffusion Filter")
morph <-
  wrap_elements(full = aznyan::morphology(png, ksize = c(4, 4, 4))) +
  labs(title = "Morphological\nTransformation")

(blur | diffusion | morph)

In addition to general-purpose image effects, aznyan also provides photo-style color filters ported from Rustagram, such as the following:

In practice, a convenient way to use aznyan is as a custom filter via ggfx::with_custom(), which allows you to apply effects to arbitrary ggplot layers. For example:

library(ggfx)

my_effect <- \(x, ...) {
  vp <- get_viewport_area(x)
  vp <- aznyan::stylize(vp, ...)
  set_viewport_area(x, vp)
}

ggplot(mpg) +
  with_custom(
    geom_density(aes(cty, fill = drv), alpha = 0.4, adjust = 2),
    filter = my_effect
  ) +
  scale_fill_viridis_d(option = "plasma")

License

MIT License.

About

Image filters for R

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Contributors