Skip to content

Commit c2ccc64

Browse files
committed
numerical linear algebra - svd - added generated video
1 parent 87f3f9e commit c2ccc64

File tree

1 file changed

+80
-5
lines changed
  • projects/math/numerical-linalg/notebooks

1 file changed

+80
-5
lines changed

projects/math/numerical-linalg/notebooks/svd.clj

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ first-image
201201
;; Let us take its first column, which is the first
202202
;; frame, and show it as an image:
203203

204-
(defn matrix->first-frame [m]
204+
(defn matrix->first-image [m]
205205
(-> m
206206
(.getColumn 0)
207207
dtype/->array-buffer
@@ -211,7 +211,17 @@ first-image
211211
(tensor/reshape [120 160])
212212
bufimg/tensor->image))
213213

214-
(matrix->first-frame component0)
214+
(defn matrix->first-image [m]
215+
(-> m
216+
(.getColumn 0)
217+
dtype/->array-buffer
218+
tensor-normalize
219+
(dfn/* 255)
220+
(dtype/->int-array)
221+
(tensor/reshape [120 160])
222+
bufimg/tensor->image))
223+
224+
(matrix->first-image component0)
215225

216226
;; We see it is the background image of the video.
217227

@@ -223,16 +233,81 @@ first-image
223233
(mat/sub matrix
224234
component0))
225235

226-
(matrix->first-frame residual)
236+
(matrix->first-image residual)
227237

228238
;; We see these are the people.
229239

230240

231-
;; ## Summary:
241+
;; ## Visualizing the decomposition wit the first image:
232242

233243
;; Let us summarize the decomposition:
234244

235245
(->> [matrix
236246
component0
237247
residual]
238-
(mapv matrix->first-frame))
248+
(mapv matrix->first-image))
249+
250+
;; ## Generating decomposed videos
251+
252+
(defn matrix->images [m]
253+
(-> m
254+
mat/mat->array
255+
dtype/->array-buffer
256+
tensor-normalize
257+
(dfn/* 255)
258+
dtype/->int-array
259+
(tensor/reshape [120 160 350])
260+
(tensor/transpose [2 0 1])
261+
(->> (mapv bufimg/tensor->image))))
262+
263+
(->> residual
264+
matrix->images
265+
(take 20))
266+
267+
(def frame-format
268+
(clj-media/video-format {:pixel-format :pixel-format/gray8
269+
:time-base 7
270+
:line-size 160
271+
:width 160
272+
:height 120}))
273+
274+
(defn img->frame [img pts time-base]
275+
(clj-media/make-frame
276+
{:bytes (-> img
277+
(.getData)
278+
(.getDataBuffer)
279+
(.getData))
280+
:format frame-format
281+
:time-base time-base
282+
:pts pts}))
283+
284+
(def generated-frames
285+
(let [frame-rate 7
286+
seconds 50
287+
num-frames (* seconds frame-rate)]
288+
(into []
289+
(map-indexed (fn [pts image]
290+
(img->frame image pts frame-rate)))
291+
(matrix->images residual))))
292+
293+
(def target-path
294+
"notebooks/generated-movie.mp4")
295+
296+
(clj-media/write!
297+
(clj-media/make-media frame-format generated-frames)
298+
target-path)
299+
300+
(kind/video
301+
{:src target-path})
302+
303+
304+
305+
306+
307+
308+
309+
310+
311+
312+
313+

0 commit comments

Comments
 (0)