annotate org/gabor.org @ 373:6205677359e9

minor formatting.
author Robert McIntyre <rlm@mit.edu>
date Tue, 12 Mar 2013 04:22:40 +0000
parents 182f890a9a9d
children 31f15781db36
rev   line source
rlm@356 1 #+title: Fun with Gabor Filters
rlm@356 2 #+author: Robert McIntyre
rlm@356 3 #+email: rlm@mit.edu
rlm@356 4 #+description: gabor filters in clojure with opencv
rlm@356 5 #+keywords: computer vision, jMonkeyEngine3, clojure, opencv
rlm@356 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@356 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@356 8 #+babel: :mkdirp yes :noweb yes :exports both
rlm@356 9
rlm@356 10
rlm@368 11 Gabor filters were invented by the same guy who invented holograms.
rlm@368 12
rlm@368 13 They work well as edge detectors and are related to the human visual
rlm@368 14 system.
rlm@356 15
rlm@356 16 #+name: gabor
rlm@356 17 #+begin_src clojure
rlm@357 18 (ns cortex.gabor
rlm@357 19 (:import org.opencv.core.CvType)
rlm@357 20 (:import java.awt.image.BufferedImage)
rlm@357 21 (:import ij.ImagePlus)
rlm@358 22 (:import org.opencv.core.Mat)
rlm@364 23 (:use (cortex world sense util vision import))
rlm@363 24 (:import com.jme3.post.SceneProcessor)
rlm@363 25 (:import (com.jme3.util BufferUtils Screenshots))
rlm@363 26 (:import java.nio.ByteBuffer)
rlm@363 27 (:import java.awt.image.BufferedImage)
rlm@363 28 (:import (com.jme3.renderer ViewPort Camera))
rlm@363 29 (:import (com.jme3.math ColorRGBA Vector3f Matrix3f Vector2f))
rlm@363 30 (:import com.jme3.renderer.Renderer)
rlm@363 31 (:import com.jme3.app.Application)
rlm@363 32 (:import com.jme3.texture.FrameBuffer)
rlm@363 33 (:import (com.jme3.scene Node Spatial)))
rlm@363 34
rlm@363 35
rlm@363 36 (cortex.import/mega-import-jme3)
rlm@364 37 (use 'clojure.math.numeric-tower)
rlm@356 38 (defn load-opencv
rlm@356 39 "Load the opencv native library. Must be called before any OpenCV
rlm@356 40 stuff is used."
rlm@356 41 []
rlm@356 42 (clojure.lang.RT/loadLibrary "opencv_java249"))
rlm@356 43
rlm@364 44 (load-opencv)
rlm@364 45
rlm@364 46 (defn gabor-kernel
rlm@364 47 ([sigma wavelength theta]
rlm@364 48 (gabor-kernel sigma wavelength theta 1 0))
rlm@364 49 ([sigma wavelength]
rlm@364 50 (gabor-kernel sigma wavelength 0 1 0))
rlm@364 51 ([sigma wavelength theta aspect-ratio phase-offset]
rlm@359 52
rlm@359 53 ;; first, find the size of the kernel which is required
rlm@359 54 (let [square #(expt % 2)
rlm@359 55 rotated (fn [[x y]]
rlm@359 56 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
rlm@359 57 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
rlm@359 58
rlm@359 59 gaussian (fn [[x y]]
rlm@359 60 (let [[x' y'] (rotated [x y])]
rlm@359 61 (Math/exp (- (/ (+ (square x')
rlm@359 62 (square (* aspect-ratio y')))
rlm@359 63 (* 2 (square sigma)))))))
rlm@359 64 sinusoid (fn [[x y]]
rlm@359 65 (let [[x' y'] (rotated [x y])]
rlm@359 66 (Math/cos
rlm@359 67 (+ (* 2 Math/PI (/ x' wavelength))
rlm@359 68 phase-offset))))
rlm@359 69
rlm@361 70 half-width
rlm@361 71 (let [std-dev-capture 5]
rlm@361 72 (max
rlm@361 73 (int (* std-dev-capture (/ sigma aspect-ratio)))
rlm@361 74 (int (* std-dev-capture sigma))
rlm@361 75 (int (* std-dev-capture (/ aspect-ratio sigma)))))
rlm@359 76
rlm@359 77 grid (let [axis (range (- half-width) (inc half-width))]
rlm@359 78 (for [y (reverse axis) x axis] (vector x y)))
rlm@359 79
rlm@359 80 scale (reduce + (map gaussian grid))
rlm@359 81
rlm@359 82 gabor (fn [[x y :as coord]]
rlm@360 83 (* (sinusoid coord) (gaussian coord) (/ scale)))
rlm@359 84
rlm@359 85 mat-width (+ 1 (* 2 half-width))
rlm@359 86 mat (Mat. mat-width mat-width CvType/CV_32F)]
rlm@359 87
rlm@359 88 (.put mat 0 0 (float-array (map gabor grid)))
rlm@364 89 mat)))
rlm@359 90
rlm@359 91
rlm@361 92 (defn draw-kernel! [kernel img-path]
rlm@361 93 (let [output img-path
rlm@360 94 size (.size kernel)
rlm@360 95 width (int (.width size))
rlm@360 96 height (int (.height size))
rlm@360 97 tmp-array (float-array (* width height))]
rlm@360 98
rlm@360 99 ;; read values from matrix.
rlm@360 100 (.get kernel 0 0 tmp-array)
rlm@360 101
rlm@360 102 ;; find overall dynamic range of the filter
rlm@360 103 (let [vals (vec tmp-array)
rlm@360 104 low (apply min vals)
rlm@360 105 high (apply max vals)
rlm@360 106 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
rlm@360 107 new-mat (Mat. height width CvType/CV_32F)]
rlm@360 108 (.put new-mat 0 0 (float-array scaled-vals))
rlm@361 109 (org.opencv.highgui.Highgui/imwrite output new-mat))))
rlm@361 110
rlm@364 111 ;; some cool examples
rlm@364 112 #+end_src
rlm@364 113
rlm@364 114
rlm@364 115
rlm@364 116
rlm@364 117 [[../images/gabor-50-10.png]]
rlm@364 118
rlm@364 119 #+begin_src clojure
rlm@364 120 (def img-base "/home/r/proj/cortex/images/")
rlm@364 121
rlm@364 122 (draw-kernel! (gabor-kernel 50 10 0 1 0)
rlm@364 123 (str img-base "gabor-50-10.png"))
rlm@364 124 #+end_src
rlm@364 125
rlm@364 126
rlm@364 127 [[../images/gabor-50-10-pi-over-4.png]]
rlm@364 128
rlm@364 129 #+begin_src clojure
rlm@364 130 (draw-kernel! (gabor-kernel 50 10 (/ Math/PI 4) 1 0)
rlm@364 131 (str img-base "gabor-50-10-pi-over-4.png"))
rlm@364 132 #+end_src
rlm@364 133
rlm@364 134
rlm@364 135 [[../images/gabor-50-10-pi-over-2.png]]
rlm@364 136
rlm@364 137 #+begin_src clojure
rlm@364 138 (draw-kernel! (gabor-kernel 50 10 (/ Math/PI 2) 1 0)
rlm@364 139 (str img-base "gabor-50-10-pi-over-2.png"))
rlm@364 140 #+end_src
rlm@364 141
rlm@364 142
rlm@364 143 [[../images/gabor-50-50.png]]
rlm@364 144
rlm@364 145
rlm@364 146
rlm@364 147 #+begin_src clojure
rlm@364 148 (draw-kernel! (gabor-kernel 50 50 0 1 0)
rlm@364 149 (str img-base "gabor-50-50.png"))
rlm@364 150
rlm@364 151 #+end_src
rlm@364 152
rlm@364 153 [[../images/gabor-50-10-0-3.png]]
rlm@364 154
rlm@364 155 #+begin_src clojure
rlm@364 156 (draw-kernel! (gabor-kernel 50 10 0 3 0)
rlm@364 157 (str img-base "gabor-50-10-0-3.png"))
rlm@364 158 #+end_src
rlm@364 159
rlm@364 160
rlm@364 161
rlm@364 162 [[../images/gabor-50-4-pi-over3-3.png]]
rlm@364 163 #+begin_src clojure
rlm@364 164 (draw-kernel! (gabor-kernel 50 4 (/ Math/PI 3) 3 0)
rlm@364 165 (str img-base "gabor-50-4-pi-over3-3.png"))
rlm@364 166 #+end_src
rlm@364 167
rlm@364 168
rlm@364 169
rlm@364 170
rlm@364 171
rlm@364 172 #:name gabor-tail
rlm@364 173 #+begin_src clojure
rlm@361 174 (defn show-kernel [kernel]
rlm@361 175 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
rlm@362 176 (draw-kernel! kernel img-path)
rlm@364 177 (view (ImagePlus. img-path))))
rlm@359 178
rlm@359 179 (defn print-kernel [kernel]
rlm@359 180 (println (.dump kernel)))
rlm@359 181
rlm@363 182
rlm@363 183 (def brick-length 0.48)
rlm@363 184 (def brick-width 0.24)
rlm@363 185 (def brick-height 0.12)
rlm@363 186 (def gravity (Vector3f. 0 -9.81 0))
rlm@363 187
rlm@363 188
rlm@363 189 (defn brick* [position]
rlm@363 190 (println "get brick.")
rlm@363 191 (doto (box brick-length brick-height brick-width
rlm@363 192 :position position :name "brick"
rlm@363 193 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@363 194 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@363 195 :mass 34)
rlm@363 196 (->
rlm@363 197 (.getMesh)
rlm@363 198 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@363 199 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@363 200 )
rlm@363 201 )
rlm@363 202
rlm@363 203
rlm@363 204 (defn floor*
rlm@363 205 "make a sturdy, unmovable physical floor"
rlm@363 206 []
rlm@366 207 (box 10 0.1 5 :name "floor" :mass 0
rlm@366 208 :color ColorRGBA/Gray :position (Vector3f. 0 0 0)))
rlm@363 209
rlm@363 210 (defn floor* []
rlm@363 211 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@363 212 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@363 213 :texture "Textures/BronzeCopper030.jpg"
rlm@363 214 :position (Vector3f. 0 0 0 )
rlm@363 215 :mass 0)
rlm@363 216 (->
rlm@363 217 (.getMesh)
rlm@363 218 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@363 219 (->
rlm@363 220 (.getMaterial)
rlm@363 221 (.getTextureParam "ColorMap")
rlm@363 222 (.getTextureValue)
rlm@363 223 (.setWrap Texture$WrapMode/Repeat))
rlm@363 224 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@363 225 ))
rlm@363 226
rlm@363 227
rlm@363 228 (defn brick-wall* []
rlm@363 229 (let [node (Node. "brick-wall")]
rlm@363 230 (dorun
rlm@363 231 (map
rlm@363 232 (comp #(.attachChild node %) brick*)
rlm@363 233 (for [y (range 10)
rlm@363 234 x (range 4)
rlm@363 235 z (range 1)]
rlm@363 236 (Vector3f.
rlm@363 237 (+ (* 2 x brick-length)
rlm@363 238 (if (even? (+ y z))
rlm@363 239 (/ brick-length 4) (/ brick-length -4)))
rlm@363 240 (+ (* brick-height (inc (* 2 y))))
rlm@363 241 (* 2 z brick-width) ))))
rlm@363 242 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@363 243 node))
rlm@363 244
rlm@363 245 (import com.aurellem.capture.Capture)
rlm@363 246
rlm@363 247 (import java.io.File)
rlm@363 248
rlm@364 249 (def base "/home/r/proj/cortex/render/gabor-1/")
rlm@363 250
rlm@363 251 (defn brick-wall-game-run [record?]
rlm@364 252 (let [capture-dir (File. base "main")]
rlm@364 253
rlm@364 254 (.mkdir (File. base "main"))
rlm@364 255 (doto
rlm@364 256 (world
rlm@364 257 (doto (Node.) (.attachChild (floor*))
rlm@364 258 (.attachChild (brick-wall*))
rlm@364 259 )
rlm@364 260 {"key-f" (fn [game value]
rlm@364 261 (if (not value) (add-element game (brick-wall*))))
rlm@364 262 "key-space" (fire-cannon-ball )}
rlm@364 263 (fn [world]
rlm@366 264 (position-camera
rlm@366 265 world
rlm@366 266 (Vector3f. 1.382548, 4.0383573, 5.994235)
rlm@366 267 (Quaternion. 0.0013082094, 0.98581666,
rlm@366 268 -0.1676442, 0.0076932586))
rlm@363 269
rlm@364 270 ;;(speed-up world)
rlm@364 271
rlm@364 272 (if record?
rlm@364 273 (Capture/captureVideo
rlm@364 274 world capture-dir))
rlm@364 275
rlm@363 276 (add-camera! world (.getCamera world) no-op))
rlm@364 277 (fn [& _]))
rlm@364 278 (.start))))
rlm@363 279
rlm@364 280 (defn convolve-preview [kernel]
rlm@363 281 (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png"
rlm@357 282
rlm@357 283
rlm@357 284 output "/home/r/ppp.png"
rlm@356 285
rlm@357 286 i (org.opencv.highgui.Highgui/imread input)
rlm@358 287
rlm@364 288 ;;kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
rlm@358 289
rlm@358 290 new-mat (Mat.)
rlm@358 291
rlm@357 292 ]
rlm@356 293
rlm@373 294 (org.opencv.imgproc.Imgproc/filter2D
rlm@373 295 i new-mat CvType/CV_32F kernel)
rlm@358 296
rlm@358 297 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
rlm@358 298
rlm@357 299 (view (ImagePlus. input))
rlm@361 300 (view (ImagePlus. output))))
rlm@357 301
rlm@364 302 (use 'clojure.java.shell)
rlm@363 303
rlm@363 304
rlm@364 305 (defn apply-gabor [kernel source dest]
rlm@364 306 (let [i (org.opencv.highgui.Highgui/imread source)
rlm@364 307 new-mat (Mat.)]
rlm@364 308
rlm@364 309 (println dest)
rlm@364 310 (if (not (.exists (File. dest)))
rlm@364 311 (do
rlm@373 312 (org.opencv.imgproc.Imgproc/filter2D
rlm@373 313 i new-mat CvType/CV_32F kernel)
rlm@364 314 (org.opencv.highgui.Highgui/imwrite dest new-mat)
rlm@364 315 (println "mogrify" "-modulate" "1000%" dest)
rlm@364 316 (sh "mogrify" "-modulate" "1000%" dest)))))
rlm@363 317
rlm@363 318
rlm@364 319 (import java.io.File)
rlm@363 320
rlm@364 321 (defn images [path]
rlm@364 322 (sort (rest (file-seq (File. path)))))
rlm@364 323
rlm@364 324
rlm@364 325
rlm@364 326 (defn pics [file]
rlm@364 327 (images (str base file)))
rlm@364 328
rlm@364 329 (defn generate-gabor-images [kernel name]
rlm@364 330 (draw-kernel! kernel (str base name ".png"))
rlm@364 331
rlm@364 332 (.mkdir (File. (str base name)))
rlm@364 333
rlm@364 334 (let [main (map #(.getCanonicalPath %) (pics "main"))
rlm@364 335 targets (map #(str base name "/" (format "%07d.png" %))
rlm@364 336 (range 0 (count main)))]
rlm@364 337 (dorun (pmap (partial apply-gabor kernel) main targets))))
rlm@364 338
rlm@364 339
rlm@364 340 (def banks
rlm@364 341 [[(gabor-kernel 2.8 3.5) "bank-1-1"]
rlm@364 342 [(gabor-kernel 2.8 3.5 (/ Math/PI 2)) "bank-1-1-rot"]
rlm@364 343
rlm@364 344 ;; [(gabor-kernel 3.6 4.6) "bank-1-2"]
rlm@364 345 ;; [(gabor-kernel 4.5 5.6) "bank-2-1"]
rlm@364 346 ;; [(gabor-kernel 6.3 7.9) "bank-3-1"]
rlm@364 347 ;; [(gabor-kernel 7.3 9.1) "bank-3-2"]
rlm@364 348
rlm@364 349 [(gabor-kernel 12.3 15.4) "bank-6-1"]
rlm@364 350
rlm@364 351
rlm@364 352 ;; [(gabor-kernel 17 21.2) "bank-8-1"]
rlm@364 353 ;; [(gabor-kernel 18.2 22.8) "bank-8-2"]
rlm@364 354 ])
rlm@364 355
rlm@364 356
rlm@364 357 (defn make-all-images []
rlm@364 358 (dorun (map (partial apply generate-gabor-images) banks)))
rlm@364 359
rlm@364 360
rlm@364 361
rlm@364 362 (defn compile-left-right []
rlm@364 363 (.mkdir (File. (str base "left-right")))
rlm@364 364 (let [main (pics "main")
rlm@364 365 left (pics "bank-1-1")
rlm@364 366 right (pics "bank-1-1-rot")
rlm@364 367 left-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
rlm@364 368 right-kernel (repeat 20000 (File. (str base "bank-1-1-rot.png")))
rlm@364 369 targets (map
rlm@364 370 #(File. (str base "left-right/" (format "%07d.png" %)))
rlm@364 371 (range 0 (count main)))]
rlm@364 372
rlm@364 373 (dorun
rlm@364 374 (pmap
rlm@364 375 (comp
rlm@364 376 (fn [[main left right left-kernel right-kernel target]]
rlm@364 377 (println target)
rlm@364 378 (if (not (.exists (File. target)))
rlm@364 379 (sh "convert"
rlm@364 380 "-size" "1940x515" "xc:white"
rlm@364 381 main "-geometry" "+0+0" "-composite"
rlm@364 382 left "-geometry" "+650+0" "-composite"
rlm@364 383 right "-geometry" "+1300+0" "-composite"
rlm@364 384 left-kernel "-geometry" "+960+485" "-composite"
rlm@364 385 right-kernel "-geometry" "+1610+485" "-composite"
rlm@364 386 target)))
rlm@364 387 (fn [& args] (map #(.getCanonicalPath %) args)))
rlm@364 388 main left right left-kernel right-kernel targets))))
rlm@364 389
rlm@364 390
rlm@364 391 (defn compile-big-small []
rlm@364 392 (.mkdir (File. (str base "big-small")))
rlm@364 393 (let [main (pics "main")
rlm@364 394 left (pics "bank-1-1")
rlm@364 395 right (pics "bank-6-1")
rlm@364 396 small-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
rlm@364 397 big-kernel (repeat 20000 (File. (str base "bank-6-1.png")))
rlm@364 398 targets (map
rlm@364 399 #(File. (str base "big-small/" (format "%07d.png" %)))
rlm@364 400 (range 0 (count main)))]
rlm@364 401
rlm@364 402 (dorun
rlm@364 403 (pmap
rlm@364 404 (comp
rlm@364 405 (fn [[main left right small-kernel big-kernel target]]
rlm@364 406 (println target)
rlm@364 407 (if (not (.exists (File. target)))
rlm@364 408 (sh "convert"
rlm@364 409 "-size" "1940x610" "xc:white"
rlm@364 410 main "-geometry" "+0+0" "-composite"
rlm@364 411 left "-geometry" "+650+0" "-composite"
rlm@364 412 right "-geometry" "+1300+0" "-composite"
rlm@364 413 small-kernel "-geometry" "+960+485" "-composite"
rlm@364 414 big-kernel "-geometry" "+1560+485" "-composite"
rlm@364 415 target)))
rlm@364 416 (fn [& args] (map #(.getCanonicalPath %) args)))
rlm@364 417 main left right small-kernel big-kernel targets))))
rlm@364 418
rlm@364 419
rlm@364 420 (defn regen-everything []
rlm@364 421 (make-all-images)
rlm@364 422 (compile-left-right)
rlm@364 423 (compile-big-small))
rlm@364 424
rlm@364 425
rlm@364 426 #+end_src
rlm@364 427
rlm@364 428
rlm@370 429 #+name: make-videos
rlm@370 430 #+begin_src makefile
rlm@370 431 scale:
rlm@370 432 ffmpeg -framerate 60 -i ./big-small/%07d.png -b:v 9000k\
rlm@370 433 -c:v theora -r 60 gabor-scale.ogg
rlm@364 434
rlm@370 435 rotation:
rlm@370 436 ffmpeg -framerate 60 -i ./left-right/%07d.png -b:v 9000k\
rlm@370 437 -c:v theora -r 60 gabor-rotation.ogg
rlm@370 438
rlm@370 439 all: rotation scale
rlm@370 440
rlm@370 441 clean:
rlm@370 442 rm gabor-rotation.org gabor-scale.ogg
rlm@364 443 #+end_src
rlm@364 444
rlm@364 445
rlm@365 446 #+begin_html
rlm@365 447 <div class="figure">
rlm@370 448 <video controls="controls">
rlm@372 449 <source src="../video/gabor-rotation.ogg" type="video/ogg"
rlm@365 450 preload="none" poster="../images/aurellem-1280x480.png" />
rlm@365 451 </video>
rlm@367 452 <br> <a href="http://youtu.be/bxujjO97B-U"> YouTube </a>
rlm@365 453 <p>Two gabor filters with different values of theta are compared. The
rlm@365 454 horizontally aligned one does better in this example.</p>
rlm@365 455 </div>
rlm@365 456 #+end_html
rlm@365 457
rlm@365 458
rlm@365 459
rlm@365 460 #+begin_html
rlm@365 461 <div class="figure">
rlm@370 462 <video controls="controls">
rlm@370 463 <source src="../video/gabor-scale.ogg" type="video/ogg"
rlm@365 464 preload="none" poster="../images/aurellem-1280x480.png" />
rlm@365 465 </video>
rlm@367 466 <br> <a href="http://youtu.be/-EsfA2ceWUk"> YouTube </a>
rlm@365 467 <p>Here we compare a gabor filter from bank 1 with one from bank 6.</p>
rlm@365 468 </div>
rlm@365 469 #+end_html
rlm@365 470
rlm@365 471
rlm@356 472
rlm@356 473 * COMMENT Generate Source
rlm@356 474 #+begin_src clojure :tangle ../src/cortex/gabor.clj
rlm@356 475 <<gabor>>
rlm@364 476 <<gabor-tail>>
rlm@356 477 #+end_src
rlm@364 478
rlm@370 479 #+begin_src Makefile :tangle ../render/gabor-1/Makefile
rlm@370 480 <<make-videos>>
rlm@370 481 #+end_src