Skip to content
R wrapper of the OpenCV library
C++ R C
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
R Warning about Mojave Apr 4, 2019
man Typo Apr 2, 2019
src Remove unused libs from flags Aug 13, 2019
tools Fix include path Mar 30, 2019
.Rbuildignore Ignore readme Apr 1, 2019
.gitignore First commit May 21, 2018
.travis.yml Moved the PPA again Jun 11, 2019
DESCRIPTION Add upstream URL Apr 4, 2019
LICENSE Update readme Apr 1, 2019 Some cleanups May 22, 2018
NAMESPACE Adding destroy method to release immediately Apr 2, 2019 Update the readme Apr 5, 2019
appveyor.yml Add appveyor May 26, 2018
configure Update configure Jun 13, 2019
opencv.Rproj First commit May 21, 2018

Bindings to 'OpenCV' Computer Vision Library

Experimenting with computer vision and machine learning in R. This package exposes some of the available 'OpenCV' algorithms, such as edge, body or face detection. These can either be applied to analyze static images, or to filter live video footage from a camera device.

Build Status AppVeyor Build Status CRAN_Status_Badge CRAN RStudio mirror downloads


On Windows and MacOS, the package can be installed directoy from CRAN:


Install from source

To install from source on MacOS, you need to install the opencv library from homebrew:

brew install opencv

On Ubuntu or Fedora you need libopencv-dev or opencv-devel:

sudo apt-get install libopencv-dev

And then install the R bindings:


Basic stuff:

Face recognition:

unconf <- ocv_read('')
faces <- ocv_face(unconf)
ocv_write(faces, 'faces.jpg')

Or get the face location data:

facemask <- ocv_facemask(unconf)
attr(facemask, 'faces')

Live Webcam Examples

Live face detection:


Edge detection:


Combine with Graphics

Replaces the background with a plot:


# get webcam size
test <- ocv_picture()
bitmap <- ocv_bitmap(test)
width <- dim(bitmap)[2]
height <- dim(bitmap)[3]

png('bg.png', width = width, height = height)
print(ggplot2::qplot(speed, dist, data = cars, geom = c("smooth", "point")))
bg <- ocv_read('bg.png')
  mask <- ocv_mog2(input)
  return(ocv_copyto(input, bg, mask))

Put your face in the plot:

# Overlay face filter
  mask <- ocv_facemask(input)
  ocv_copyto(input, bg, mask)

Live Face Survey

Go stand on the left if you're a tidier


# get webcam size
test <- ocv_picture()
bitmap <- ocv_bitmap(test)
width <- dim(bitmap)[2]
height <- dim(bitmap)[3]

# generates the plot
makeplot <- function(x){
  png('bg.png', width = width, height = height, res = 96)
  groups <- seq(0, width, length.out = 4)
  left <- rep("left", sum(x < groups[2]))
  middle <- rep("middle", sum(x >= groups[2] & x < groups[3]))
  right <- rep("right", sum(x >= groups[3]))
  f <- factor(c(left, middle, right), levels = c('left', 'middle', 'right'),
              labels = c("Tidy!", "Whatever Works", "Base!"))
  color = I(c("#F1BB7B", "#FD6467", "#5B1A18"))
  plot(f, ylim = c(0, 5),
       main = "Are you a tidyer or baser?", col = color)

# overlays faces on the plot
  mask <- ocv_facemask(input)
  faces <- attr(mask, 'faces')
  bg <- makeplot(faces$x)
  return(ocv_copyto(input, bg, mask))
You can’t perform that action at this time.