## ----opts, echo = FALSE, results = "hide", warning = FALSE, message = FALSE----
knitr::opts_chunk$set(dev = "jpeg", cache = FALSE,
                      autodep = TRUE, echo = TRUE,
                      prompt = FALSE, comment = NA, 
                      warning = TRUE, message = TRUE,
                      knitr.table.format = "html",
                      fig.width = 6, fig.height = 7, dpi = 96)

## -----------------------------------------------------------------------------
suppressMessages(library(rip.opencv))
v <- as.rip(t(volcano), channel = 1)
v
class(v)
str(v)

## ----fig.height=5-------------------------------------------------------------
image(v)

## -----------------------------------------------------------------------------
V <- rip.dft(v) # uses OpenCV function cv::dft()
V
str(V)

## ----fig.width=10, fig.height=4.5---------------------------------------------
par(mfrow = c(1, 2))
image(log(Mod(V)), main = "log-modulus of DFT coefficients")
image(Arg(V), main = "argument of DFT coefficients")

## -----------------------------------------------------------------------------
logo_path <- file.path(R.home(), "doc/html/logo.jpg")
rlogo <- rip.import(logo_path, type = "color")
rlogo
dim(rlogo)
str(rlogo)

## -----------------------------------------------------------------------------
a <- as.array(rlogo) # add reverse.rgb = FALSE to retain column order
str(a)

## ----fig.width=10, fig.height=4-----------------------------------------------
par(mfrow = c(1,2))
N <- prod(dim(a)[1:2])
red <- a; red[,,-1] <- runif(N, 0, 255); image(as.rip(red))
green <- a; green[,,-2] <- runif(N, 0, 255); image(as.rip(green))

## -----------------------------------------------------------------------------
ls(rip.cv)

## -----------------------------------------------------------------------------
rip.cv$IO$imread

## -----------------------------------------------------------------------------
f <- system.file("sample/color.jpg", package = "rip.opencv", mustWork = TRUE)
(imreadModes <- rip.cv$enums$ImreadModes)
(x <- rip.cv$IO$imread(f, imreadModes["IMREAD_GRAYSCALE"]))

## ----fig.width=10, fig.height=6-----------------------------------------------
par(mfrow = c(1, 2))
image(x, rescale = FALSE)
image(255 - x, rescale = FALSE) # negative

## -----------------------------------------------------------------------------
(x <- rip.cv$IO$imread(f, imreadModes["IMREAD_COLOR"]))

## ----fig.width=10, fig.height=3-----------------------------------------------
y1 <- rip.cv$photo$decolor(x)
convcodes <- rip.cv$enums$ColorConversionCodes
y2 <- rip.cv$imgproc$cvtColor(x, convcodes["COLOR_BGR2GRAY"])
range(y1 - y2)
par(mfrow = c(1, 4))
image(x, rescale = FALSE, main = "original")
image(y1, rescale = FALSE, main = "decolor")
image(y2, rescale = FALSE, main = "cvtColor")
image(y1 - y2, rescale = TRUE, main = "difference")

## ----fig.width=10, fig.height=6-----------------------------------------------
par(mfrow = c(1, 2))
x.rgb <- rip.cv$imgproc$cvtColor(x, convcodes["COLOR_BGR2RGB"])
x.hsv <- rip.cv$imgproc$cvtColor(x, convcodes["COLOR_BGR2HSV"])
image(x.rgb)
image(x.hsv)

## ----fig.width=10, fig.height=4-----------------------------------------------
par(mfrow = c(1, 3))
image(rip.cv$photo$pencilSketch(x, color = TRUE, 80, 0.1, 0.02), rescale = FALSE)
image(y <- rip.cv$photo$edgePreservingFilter(x, 2L, 60), rescale = FALSE)
image(rip.cv$photo$stylization(y, 60), rescale = FALSE)

## -----------------------------------------------------------------------------
do.call(rbind, lapply(ls(rip.cv), function(m) data.frame(Module = m, Function = .DollarNames(rip.cv[[m]], ""))))

