1 Package installation

1.1 Requirements

Before installing Rvision, you will need to install the latest version of the devtools package in R.You can install devtools as follows:

install.package("devtools")

1.2 Installing Rvision

You can install Rvision as follows:

devtools::install_github("swarm-lab/Rvision")

Rvision depends on ROpenCVLite to access OpenCV’s functionalities. If not already installed, ROpenCVLite will be installed first by the above command line. This may take some time as it will download, compile and install OpenCV for you (compilation time will depend on your computer). I suggest going out for a cup of tea or coffee while ROpenCVLite is installing ;-)

Back to top


2 Image operations

2.1 Read an image file

library(Rvision)

# Locate sample file
samplePath <- system.file("sample_img", "bunny.png", package = "Rvision")

# Read image file
img1 <- image(samplePath)
img1
## C++ object <0x7ffe3de9bd50> of class 'Image' <0x7ffe3dcabb30>

2.2 Display image

plot(img1)

2.3 Image properties

dim(img1)
## [1]  720 1280    4
nrow(img1)
## [1] 720
ncol(img1)
## [1] 1280
nchan(img1)
## [1] 4
bitdepth(img1)
## [1] "8U"
colorspace(img1)
## [1] "BGRA"

2.4 Save image to disk

outfile = tempfile(fileext = ".png")
write.Image(img1, file = outfile)
## [1] TRUE
file.exists(outfile)
## [1] TRUE

2.5 Convert color image to grayscale

img2 <- changeColorSpace(img1, "GRAY")
plot(img2)

2.6 Convert grayscale image to black and white

img3 <- img2 > 128   # Gray values > 128 are turned to white (255)
                    # Gray values <= 128 are turned to black (0)
plot(img3)

Back to top