Skip to contents

2.1 - Creating video streams

Video stream objects are created from video files by passing the path to a video file to the video() function. For instance:

# Find the path to the Balloon.mp4 video provided with Rvision
path_to_video <- system.file("sample_vid", "Balloon.mp4", package = "Rvision")

# Open the video file stream
my_video <- video(filename = path_to_video)

Once a Video object is not required anymore, it can be released from memory as follows:

release(my_video)

Note that Video objects are wrappers around OpenCV’s VideoCapture pointers. They will not persist between R sessions.


2.2 - Creating camera streams

Camera Stream objects are created by starting a camera stream (e.g. from a webcam) using the stream() function. For instance:

# Start the default camera stream
my_stream <- stream(index = 0)

The index argument takes an integer number corresponding to the position of the camera in the list of video capturing devices available on your computer. 0 corresponds to the default camera, which is usually the embedded webcam on most computers. Note that the order of the list of video capturing devices might change after each computer restart, or if cameras are connected/disconnected during a session.

Once a Stream object is not required anymore, it can be released from memory as follows:

release(my_stream)

Note that Stream objects are wrappers around OpenCV’s VideoCapture pointers. They will not persist between R sessions.


2.3 - Creating images

There are multiple ways to create Image objects: from files, from Video objects, from Stream objects, and from R arrays.

Note that Image objects are wrappers around OpenCV’s Mat and UMat objects. They will not persist between R sessions.

2.3.1 - Images from files

Image objects are created from image files by passing the path to an image file to the image() function. For instance:

# Find the path to the balloon1.png image provided with Rvision
path_to_image <- system.file("sample_img", "balloon1.png", package = "Rvision")

# Load the image in memory
my_image <- image(filename = path_to_image)

2.3.2 - Images from video streams

Image objects are created from video streams by capturing frames from them with the readNext() and readFrame() functions. For instance:

# Capture the next available frame from the my_video object created earlier
my_image <- readNext(my_video)

# Capture frame 100 from the my_video object created earlier
my_image <- readFrame(my_video, 100)

2.3.3 - Images from camera streams

Image objects are created from video streams by capturing frames from them with the readNext() function. For instance:

# Capture the next available frame from the my_stream object created earlier
my_image <- readNext(my_stream)

2.3.4 - Images from R arrays and matrices

Image objects are created from R arrays and matrices by passing the array or matrix directly to the image() function. For instance:

# Create a 100 x 100 x 3 random array
my_array <- array(rnorm(30000), dim = c(100, 100, 3))

# Load the image in memory
my_image <- image(my_array)

2.4 - Writing video streams to file

Videos can be written to files using VideoWriter objects. When creating a VideoWriter object, the dimensions, frame rate, and codec of the final video file must be specified. For instance:

# Create a 1280x720 video file called file.mp4 on the desktop, using the x264
# codec at 30 frames per second
my_writer <- videoWriter("~/Desktop/file.mp4", fourcc = "x264", fps = 30, 
                         height = 720, width = 1280)

Once a VideoWriter object has been created, individual frames can be written to it sequentially. For instance:

# Capture the next 30 frames from the my_stream camera stream created earlier 
# and write them to file.mp4
for (i in seq_len(30)) {
  writeFrame(my_writer, readNext(my_stream))
}

Once all frames have been written to the VideoWriter object, it can be closed and released from memory as follows:

release(my_writer)

Note that if you do not close and release the VideoWriter object prior to exiting the R session, you will not be able to play it with your system’s video player.

Note that VideoWriter objects are wrappers around OpenCV’s VideoWriter pointers. They will not persist between R sessions.


2.4 - Writing images to file

Images can be written to files using the write.Image() function. For instance:

# Write the my_image object created earlier to a PNG file called file.png on the
# desktop
write.Image(my_image, "~/Desktop/file.png")

Rvision will guess the format of the image file from the file extension. Most common file formats are supported.