This page describes how to read and write videos and images with Rvision
.
You can create a Video
object from a video file as follows:
path_to_video <- system.file("sample_vid", "Balloon.mp4", package = "Rvision")
my_video <- video(filename = path_to_video)
Video
objects can be released from memory as follows:
release(my_video)
Note that Video
objects are wrappers around OpenCV
VideoCapture
objects. They will not persist between R
sessions.
You can create a Stream
object from a camera stream as follows:
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.
Stream
objects can be released from memory as follows:
release(my_stream)
Note that Stream
objects are wrappers around OpenCV
VideoCapture
objects. They will not persist between R
sessions.
There are three ways to create Image
objects: from files, from Video
objects, and from Stream
objects.
Note that Image
objects are wrappers around OpenCV
Mat
objects. They will not persist between R
sessions.
You can create an Image
object from an image file as follows:
path_to_image <- system.file("sample_img", "bunny.png", package = "Rvision")
my_image <- image(filename = path_to_image)
You can create an Image
object from the next available frame of a Video
object as follows:
my_image <- readNext(my_video)
You can create an Image
object from any arbitrary frame of a Video
object as follows (here frame number 100):
You can create an Image
object from the next available frame of a Stream
object as follows:
Videos can be written to the disk using a VideoWriter
object.
When creating a VideoWriter
object, you need to specify the codec of the video, its framerate, its height and its width. For instance, we will here create a VideoWriter
object that will save to a temporary mp4
video file using the x264
codec at 30fps.
path_to_video <- paste0(tempfile(), ".mp4")
my_writer <- videoWriter(path_to_video, fourcc = "x264", fps = 30, height = 720, width = 1280)
Once a VideoWriter
object has been created, you can write individual frames to it. For instance, we will here capture 30 frames from the default webcam and write them to the video file.
VideoWriter
objects can be released from memory as follows:
release(my_writer)
Note that VideoWriter
objects are wrappers around OpenCV
VideoWriter
objects. They will not persist between R
sessions.
Writing an image to a file is straightforward using the write.Image
function.
Rvision
will guess the format of the image file from the file extension. For instance, we will here capture 1 frame from the default webcam and write it to a temporary png image file.