Mercurial > cortex
comparison org/gabor.org @ 364:b599a189433b
render video and images.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 07 Mar 2013 07:42:32 +0000 |
parents | 9fa92af29c3a |
children | 1727b68634ea |
comparison
equal
deleted
inserted
replaced
363:9fa92af29c3a | 364:b599a189433b |
---|---|
14 (ns cortex.gabor | 14 (ns cortex.gabor |
15 (:import org.opencv.core.CvType) | 15 (:import org.opencv.core.CvType) |
16 (:import java.awt.image.BufferedImage) | 16 (:import java.awt.image.BufferedImage) |
17 (:import ij.ImagePlus) | 17 (:import ij.ImagePlus) |
18 (:import org.opencv.core.Mat) | 18 (:import org.opencv.core.Mat) |
19 (:use (cortex world sense util vision)) | 19 (:use (cortex world sense util vision import)) |
20 (:import com.jme3.post.SceneProcessor) | 20 (:import com.jme3.post.SceneProcessor) |
21 (:import (com.jme3.util BufferUtils Screenshots)) | 21 (:import (com.jme3.util BufferUtils Screenshots)) |
22 (:import java.nio.ByteBuffer) | 22 (:import java.nio.ByteBuffer) |
23 (:import java.awt.image.BufferedImage) | 23 (:import java.awt.image.BufferedImage) |
24 (:import (com.jme3.renderer ViewPort Camera)) | 24 (:import (com.jme3.renderer ViewPort Camera)) |
28 (:import com.jme3.texture.FrameBuffer) | 28 (:import com.jme3.texture.FrameBuffer) |
29 (:import (com.jme3.scene Node Spatial))) | 29 (:import (com.jme3.scene Node Spatial))) |
30 | 30 |
31 | 31 |
32 (cortex.import/mega-import-jme3) | 32 (cortex.import/mega-import-jme3) |
33 | 33 (use 'clojure.math.numeric-tower) |
34 (defn load-opencv | 34 (defn load-opencv |
35 "Load the opencv native library. Must be called before any OpenCV | 35 "Load the opencv native library. Must be called before any OpenCV |
36 stuff is used." | 36 stuff is used." |
37 [] | 37 [] |
38 (clojure.lang.RT/loadLibrary "opencv_java249")) | 38 (clojure.lang.RT/loadLibrary "opencv_java249")) |
39 | 39 |
40 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset] | 40 (load-opencv) |
41 | |
42 (defn gabor-kernel | |
43 ([sigma wavelength theta] | |
44 (gabor-kernel sigma wavelength theta 1 0)) | |
45 ([sigma wavelength] | |
46 (gabor-kernel sigma wavelength 0 1 0)) | |
47 ([sigma wavelength theta aspect-ratio phase-offset] | |
41 | 48 |
42 ;; first, find the size of the kernel which is required | 49 ;; first, find the size of the kernel which is required |
43 (let [square #(expt % 2) | 50 (let [square #(expt % 2) |
44 rotated (fn [[x y]] | 51 rotated (fn [[x y]] |
45 [(+ (* x (Math/cos theta)) (* y (Math/sin theta))) | 52 [(+ (* x (Math/cos theta)) (* y (Math/sin theta))) |
73 | 80 |
74 mat-width (+ 1 (* 2 half-width)) | 81 mat-width (+ 1 (* 2 half-width)) |
75 mat (Mat. mat-width mat-width CvType/CV_32F)] | 82 mat (Mat. mat-width mat-width CvType/CV_32F)] |
76 | 83 |
77 (.put mat 0 0 (float-array (map gabor grid))) | 84 (.put mat 0 0 (float-array (map gabor grid))) |
78 mat)) | 85 mat))) |
79 | 86 |
80 | 87 |
81 (defn draw-kernel! [kernel img-path] | 88 (defn draw-kernel! [kernel img-path] |
82 (let [output img-path | 89 (let [output img-path |
83 size (.size kernel) | 90 size (.size kernel) |
95 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals) | 102 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals) |
96 new-mat (Mat. height width CvType/CV_32F)] | 103 new-mat (Mat. height width CvType/CV_32F)] |
97 (.put new-mat 0 0 (float-array scaled-vals)) | 104 (.put new-mat 0 0 (float-array scaled-vals)) |
98 (org.opencv.highgui.Highgui/imwrite output new-mat)))) | 105 (org.opencv.highgui.Highgui/imwrite output new-mat)))) |
99 | 106 |
107 ;; some cool examples | |
108 #+end_src | |
109 | |
110 | |
111 | |
112 | |
113 [[../images/gabor-50-10.png]] | |
114 | |
115 #+begin_src clojure | |
116 (def img-base "/home/r/proj/cortex/images/") | |
117 | |
118 (draw-kernel! (gabor-kernel 50 10 0 1 0) | |
119 (str img-base "gabor-50-10.png")) | |
120 #+end_src | |
121 | |
122 | |
123 [[../images/gabor-50-10-pi-over-4.png]] | |
124 | |
125 #+begin_src clojure | |
126 (draw-kernel! (gabor-kernel 50 10 (/ Math/PI 4) 1 0) | |
127 (str img-base "gabor-50-10-pi-over-4.png")) | |
128 #+end_src | |
129 | |
130 | |
131 [[../images/gabor-50-10-pi-over-2.png]] | |
132 | |
133 #+begin_src clojure | |
134 (draw-kernel! (gabor-kernel 50 10 (/ Math/PI 2) 1 0) | |
135 (str img-base "gabor-50-10-pi-over-2.png")) | |
136 #+end_src | |
137 | |
138 | |
139 [[../images/gabor-50-50.png]] | |
140 | |
141 | |
142 | |
143 #+begin_src clojure | |
144 (draw-kernel! (gabor-kernel 50 50 0 1 0) | |
145 (str img-base "gabor-50-50.png")) | |
146 | |
147 #+end_src | |
148 | |
149 [[../images/gabor-50-10-0-3.png]] | |
150 | |
151 #+begin_src clojure | |
152 (draw-kernel! (gabor-kernel 50 10 0 3 0) | |
153 (str img-base "gabor-50-10-0-3.png")) | |
154 #+end_src | |
155 | |
156 | |
157 | |
158 [[../images/gabor-50-4-pi-over3-3.png]] | |
159 #+begin_src clojure | |
160 (draw-kernel! (gabor-kernel 50 4 (/ Math/PI 3) 3 0) | |
161 (str img-base "gabor-50-4-pi-over3-3.png")) | |
162 #+end_src | |
163 | |
164 | |
165 | |
166 | |
167 | |
168 #:name gabor-tail | |
169 #+begin_src clojure | |
100 (defn show-kernel [kernel] | 170 (defn show-kernel [kernel] |
101 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"] | 171 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"] |
102 (draw-kernel! kernel img-path) | 172 (draw-kernel! kernel img-path) |
103 (view (ImagePlus. output)))) | 173 (view (ImagePlus. img-path)))) |
104 | 174 |
105 (defn print-kernel [kernel] | 175 (defn print-kernel [kernel] |
106 (println (.dump kernel))) | 176 (println (.dump kernel))) |
107 | 177 |
108 | 178 |
169 | 239 |
170 (import com.aurellem.capture.Capture) | 240 (import com.aurellem.capture.Capture) |
171 | 241 |
172 (import java.io.File) | 242 (import java.io.File) |
173 | 243 |
244 (def base "/home/r/proj/cortex/render/gabor-1/") | |
174 | 245 |
175 (defn brick-wall-game-run [record?] | 246 (defn brick-wall-game-run [record?] |
176 (doto | 247 (let [capture-dir (File. base "main")] |
177 (world | 248 |
178 (doto (Node.) (.attachChild (floor*)) | 249 (.mkdir (File. base "main")) |
179 (.attachChild (brick-wall*)) | 250 (doto |
180 ) | 251 (world |
181 {"key-f" (fn [game value] | 252 (doto (Node.) (.attachChild (floor*)) |
182 (if (not value) (add-element game (brick-wall*)))) | 253 (.attachChild (brick-wall*)) |
183 "key-space" (fire-cannon-ball )} | 254 ) |
184 (fn [world] | 255 {"key-f" (fn [game value] |
185 (position-camera world | 256 (if (not value) (add-element game (brick-wall*)))) |
186 (Vector3f. 1.382548, 4.0383573, 5.994235) | 257 "key-space" (fire-cannon-ball )} |
187 (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586)) | 258 (fn [world] |
188 | 259 (position-camera world |
189 ;;(speed-up world) | 260 (Vector3f. 1.382548, 4.0383573, 5.994235) |
190 | 261 (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586)) |
191 (if record? | 262 |
192 (Capture/captureVideo | 263 ;;(speed-up world) |
193 world | 264 |
194 (File. | 265 (if record? |
195 "/home/r/proj/cortex/render/gabor-1/main"))) | 266 (Capture/captureVideo |
267 world capture-dir)) | |
268 | |
196 (add-camera! world (.getCamera world) no-op)) | 269 (add-camera! world (.getCamera world) no-op)) |
197 (fn [& _])) | 270 (fn [& _])) |
198 (.start))) | 271 (.start)))) |
199 | 272 |
200 (defn convolve-practice [kernel] | 273 (defn convolve-preview [kernel] |
201 (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png" | 274 (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png" |
202 | 275 |
203 | 276 |
204 output "/home/r/ppp.png" | 277 output "/home/r/ppp.png" |
205 | 278 |
206 i (org.opencv.highgui.Highgui/imread input) | 279 i (org.opencv.highgui.Highgui/imread input) |
207 | 280 |
208 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0) | 281 ;;kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0) |
209 | 282 |
210 new-mat (Mat.) | 283 new-mat (Mat.) |
211 | 284 |
212 ] | 285 ] |
213 | 286 |
216 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat) | 289 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat) |
217 | 290 |
218 (view (ImagePlus. input)) | 291 (view (ImagePlus. input)) |
219 (view (ImagePlus. output)))) | 292 (view (ImagePlus. output)))) |
220 | 293 |
221 | 294 (use 'clojure.java.shell) |
222 | 295 |
223 (defn generate-gabor-images [] | 296 |
224 (gabor-kernel 2.8 1 0 3.5 0) | 297 (defn apply-gabor [kernel source dest] |
225 | 298 (let [i (org.opencv.highgui.Highgui/imread source) |
226 | 299 new-mat (Mat.)] |
227 | 300 |
301 (println dest) | |
302 (if (not (.exists (File. dest))) | |
303 (do | |
304 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel) | |
305 (org.opencv.highgui.Highgui/imwrite dest new-mat) | |
306 (println "mogrify" "-modulate" "1000%" dest) | |
307 (sh "mogrify" "-modulate" "1000%" dest))))) | |
308 | |
309 | |
310 (import java.io.File) | |
311 | |
312 (defn images [path] | |
313 (sort (rest (file-seq (File. path))))) | |
314 | |
315 | |
316 | |
317 (defn pics [file] | |
318 (images (str base file))) | |
319 | |
320 (defn generate-gabor-images [kernel name] | |
321 (draw-kernel! kernel (str base name ".png")) | |
322 | |
323 (.mkdir (File. (str base name))) | |
324 | |
325 (let [main (map #(.getCanonicalPath %) (pics "main")) | |
326 targets (map #(str base name "/" (format "%07d.png" %)) | |
327 (range 0 (count main)))] | |
328 (dorun (pmap (partial apply-gabor kernel) main targets)))) | |
329 | |
330 | |
331 (def banks | |
332 [[(gabor-kernel 2.8 3.5) "bank-1-1"] | |
333 [(gabor-kernel 2.8 3.5 (/ Math/PI 2)) "bank-1-1-rot"] | |
334 | |
335 ;; [(gabor-kernel 3.6 4.6) "bank-1-2"] | |
336 ;; [(gabor-kernel 4.5 5.6) "bank-2-1"] | |
337 ;; [(gabor-kernel 6.3 7.9) "bank-3-1"] | |
338 ;; [(gabor-kernel 7.3 9.1) "bank-3-2"] | |
339 | |
340 [(gabor-kernel 12.3 15.4) "bank-6-1"] | |
341 | |
342 | |
343 ;; [(gabor-kernel 17 21.2) "bank-8-1"] | |
344 ;; [(gabor-kernel 18.2 22.8) "bank-8-2"] | |
345 ]) | |
346 | |
347 | |
348 (defn make-all-images [] | |
349 (dorun (map (partial apply generate-gabor-images) banks))) | |
350 | |
351 | |
352 | |
353 (defn compile-left-right [] | |
354 (.mkdir (File. (str base "left-right"))) | |
355 (let [main (pics "main") | |
356 left (pics "bank-1-1") | |
357 right (pics "bank-1-1-rot") | |
358 left-kernel (repeat 20000 (File. (str base "bank-1-1.png"))) | |
359 right-kernel (repeat 20000 (File. (str base "bank-1-1-rot.png"))) | |
360 targets (map | |
361 #(File. (str base "left-right/" (format "%07d.png" %))) | |
362 (range 0 (count main)))] | |
363 | |
364 (dorun | |
365 (pmap | |
366 (comp | |
367 (fn [[main left right left-kernel right-kernel target]] | |
368 (println target) | |
369 (if (not (.exists (File. target))) | |
370 (sh "convert" | |
371 "-size" "1940x515" "xc:white" | |
372 main "-geometry" "+0+0" "-composite" | |
373 left "-geometry" "+650+0" "-composite" | |
374 right "-geometry" "+1300+0" "-composite" | |
375 left-kernel "-geometry" "+960+485" "-composite" | |
376 right-kernel "-geometry" "+1610+485" "-composite" | |
377 target))) | |
378 (fn [& args] (map #(.getCanonicalPath %) args))) | |
379 main left right left-kernel right-kernel targets)))) | |
380 | |
381 | |
382 (defn compile-big-small [] | |
383 (.mkdir (File. (str base "big-small"))) | |
384 (let [main (pics "main") | |
385 left (pics "bank-1-1") | |
386 right (pics "bank-6-1") | |
387 small-kernel (repeat 20000 (File. (str base "bank-1-1.png"))) | |
388 big-kernel (repeat 20000 (File. (str base "bank-6-1.png"))) | |
389 targets (map | |
390 #(File. (str base "big-small/" (format "%07d.png" %))) | |
391 (range 0 (count main)))] | |
392 | |
393 (dorun | |
394 (pmap | |
395 (comp | |
396 (fn [[main left right small-kernel big-kernel target]] | |
397 (println target) | |
398 (if (not (.exists (File. target))) | |
399 (sh "convert" | |
400 "-size" "1940x610" "xc:white" | |
401 main "-geometry" "+0+0" "-composite" | |
402 left "-geometry" "+650+0" "-composite" | |
403 right "-geometry" "+1300+0" "-composite" | |
404 small-kernel "-geometry" "+960+485" "-composite" | |
405 big-kernel "-geometry" "+1560+485" "-composite" | |
406 target))) | |
407 (fn [& args] (map #(.getCanonicalPath %) args))) | |
408 main left right small-kernel big-kernel targets)))) | |
409 | |
410 | |
411 (defn regen-everything [] | |
412 (make-all-images) | |
413 (compile-left-right) | |
414 (compile-big-small)) | |
415 | |
416 | |
417 #+end_src | |
418 | |
419 #+name: make-left-right | |
420 #+begin_src sh | |
421 #!/bin/sh | |
422 | |
423 ffmpeg -framerate 60 -i ./left-right/%07d.png -b:v 9000k\ | |
424 -c:v mpeg4 -r 60 gabor-rotation.avi | |
425 | |
426 #+end_src | |
427 | |
428 | |
429 #+name: make-big-small | |
430 #+begin_src sh | |
431 #!/bin/sh | |
432 | |
433 ffmpeg -framerate 60 -i ./big-small/%07d.png -b:v 9000k\ | |
434 -c:v mpeg4 -r 60 gabor-big-small.avi | |
228 | 435 |
229 #+end_src | 436 #+end_src |
230 | 437 |
231 | 438 |
232 | 439 |
233 * COMMENT Generate Source | 440 * COMMENT Generate Source |
234 #+begin_src clojure :tangle ../src/cortex/gabor.clj | 441 #+begin_src clojure :tangle ../src/cortex/gabor.clj |
235 <<gabor>> | 442 <<gabor>> |
236 #+end_src | 443 <<gabor-tail>> |
444 #+end_src | |
445 | |
446 #+begin_src clojure :tangle ../render/gabor-1/make-rotation.sh | |
447 <<make-left-right>> | |
448 #+end_src | |
449 | |
450 #+begin_src clojure :tangle ../render/gabor-1/make-big-small.sh | |
451 <<make-big-small>> | |
452 #+end_src |