/
z2_io.Rmd
181 lines (132 loc) · 5.6 KB
/
z2_io.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
title: "2 - Input/output operations"
output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{2 - Input/output operations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
release(my_video)
```
Note that `Video` objects are wrappers around `OpenCV`'s
[`VideoCapture`](https://docs.opencv.org/4.6.0/d8/dfe/classcv_1_1VideoCapture.html)
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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
release(my_stream)
```
Note that `Stream` objects are wrappers around `OpenCV`'s
[`VideoCapture`](https://docs.opencv.org/4.6.0/d8/dfe/classcv_1_1VideoCapture.html)
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`](https://docs.opencv.org/4.6.0/d3/d63/classcv_1_1Mat.html) and
[`UMat`](https://docs.opencv.org/4.6.0/d7/d45/classcv_1_1UMat.html) 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
# 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:
```{r eval=FALSE, echo=TRUE}
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`](https://docs.opencv.org/4.6.0/dd/d9e/classcv_1_1VideoWriter.html)
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:
```{r eval=FALSE, echo=TRUE}
# 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.