-
Notifications
You must be signed in to change notification settings - Fork 22
/
Cheatsheet.cheatmd
277 lines (244 loc) · 5.24 KB
/
Cheatsheet.cheatmd
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Evision Quick Start
This document is a cheatsheet on how to use `evision`.
## Read & Write An Image
### Read An Image
#### Evision.imread
```elixir
# Read an image
iex> img = Evision.imread("image.png")
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {1080, 1920, 3},
ref: #Reference<0.664400266.3733323795.187022>
}
# Read and get the number of channels
iex> %Evision.Mat{channels: c} = img = Evision.imread("image.png")
iex> c
3
```
### Read a color image as a grayscale one
#### Evision.imread with `flags`
```elixir
iex> img = Evision.imread("image.png", flags: Evision.Constant.cv_IMREAD_GRAYSCALE())
%Evision.Mat{
channels: 1,
dims: 2,
type: {:u, 8},
raw_type: 0,
shape: {1080, 1920},
ref: #Reference<0.664400266.3733323795.187022>
}
```
### Read a PNG image that has an alpha channel
#### Evision.imread with `flags`
```elixir
iex> img = Evision.imread("image.png", flags: Evision.Constant.cv_IMREAD_UNCHANGED())
%Evision.Mat{
channels: 4,
dims: 2,
type: {:u, 8},
raw_type: 24,
shape: {1080, 1920, 4},
ref: #Reference<0.664400266.3733323795.187022>
}
```
### Write An Image
#### Evision.imwrite
The file extension decides the image encoder of the output image.
```elixir
# as PNG
Evision.imwrite("filename.png", image)
# as JPEG
Evision.imwrite("filename.jpeg", image)
```
## Access A Sub-region/matrix
### Get A Sub-area of An Image
```elixir
iex> img = Evision.imread("image.png")
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {1080, 1920, 3},
ref: #Reference<0.664400266.3733323795.187022>
}
# Note that Elixir Range is inclusive, 0..1 gives [0, 1]
iex> img[[0..100, 0..100]]
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {101, 101, 3},
ref: #Reference<0.664400266.3733323795.187023>
}
```
### Extract One Channel of An Image
```elixir
iex> img = Evision.imread("image.png")
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {1080, 1920, 3},
ref: #Reference<0.664400266.3733323795.187022>
}
# by default OpenCV uses BGR format, therefore
# the following code will extract the red-channel
iex> img[[:all, :all, 2]]
%Evision.Mat{
channels: 1,
dims: 2,
type: {:u, 8},
raw_type: 0,
shape: {1080, 1920},
ref: #Reference<0.664400266.3733323795.187023>
}
```
### Extract An Abritray Continuous Sub-matrix
```elixir
iex> img = Evision.imread("image.png")
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {1080, 1920, 3},
ref: #Reference<0.664400266.3733323795.187022>
}
# as of now, the step size has to be 1
iex> img[[100..200, 10..50, 0..1]]
%Evision.Mat{
channels: 2,
dims: 2,
type: {:u, 8},
raw_type: 8,
shape: {101, 41, 2},
ref: #Reference<0.664400266.3733323795.187024>
}
```
## Interact with Nx.Tensor
### Convert to Nx.Tensor
```elixir
iex> img = Evision.imread("image.png")
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {1080, 1920, 3},
ref: #Reference<0.664400266.3733323795.187022>
}
iex> t = Evision.Mat.to_nx(img)
#Nx.Tensor<
u8[1080][1920][3]
Evision.Backend
[
[
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, 0],
[128, 128, ...],
...
],
...
]
>
```
It works the same for any `Evision.Mat`.
```elixir
iex> mat = Evision.Mat.ones({2, 3, 4, 5}, :u8)
%Evision.Mat{
channels: 1,
dims: 4,
type: {:u, 8},
raw_type: 0,
shape: {2, 3, 4, 5},
ref: #Reference<0.2233780127.1059454995.175627>
}
iex> t = Evision.Mat.to_nx(mat)
#Nx.Tensor<
u8[2][3][4][5]
Evision.Backend
[
[
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
],
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
],
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
...
]
],
...
]
>
```
### Convert From Nx.Tensor
From abritray `Nx.Tensor`.
```elixir
iex> t = Nx.iota({3, 5, 7}, type: :u8)
iex> Evision.Mat.from_nx(t)
%Evision.Mat{
channels: 1,
dims: 4,
type: {:u, 8},
raw_type: 0,
shape: {2, 3, 4, 5},
ref: #Reference<0.2233780127.1059454995.175631>
}
```
However, please note that type `:s64`, `:u32` and `:u64` are not supported by OpenCV.
Another thing to note is that, some OpenCV functions expect the input to be a "valid 2D image",
in such cases, `Evision.Mat.from_nx_2d/1` should be used instead. Please see the cheat below.
```elixir
iex> image_tensor = Nx.broadcast(Nx.tensor(0, type: :u8), {720, 1280, 3})
iex> Evision.Mat.from_nx_2d(image_tensor)
%Evision.Mat{
channels: 3,
dims: 2,
type: {:u, 8},
raw_type: 16,
shape: {720, 1280, 3},
ref: #Reference<0.2233780127.1059454995.175632>
}
# compare the results
# note the differences in `channels`, `dims` and `raw_type`
iex> Evision.Mat.from_nx(image_tensor)
%Evision.Mat{
channels: 1,
dims: 3,
type: {:u, 8},
raw_type: 0,
shape: {720, 1280, 3},
ref: #Reference<0.2233780127.1059454995.175633>
}
```