annotate org/gabor.org @ 429:b5d0f0adf19f

improvements by Dylan!
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Mar 2014 20:56:56 -0400
parents 057d47fc4789
children 42ddfe406c0a
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@376 167
rlm@364 168
rlm@375 169 #+name: gabor-tail
rlm@364 170 #+begin_src clojure
rlm@361 171 (defn show-kernel [kernel]
rlm@361 172 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
rlm@362 173 (draw-kernel! kernel img-path)
rlm@364 174 (view (ImagePlus. img-path))))
rlm@359 175
rlm@359 176 (defn print-kernel [kernel]
rlm@359 177 (println (.dump kernel)))
rlm@359 178
rlm@363 179
rlm@363 180 (def brick-length 0.48)
rlm@363 181 (def brick-width 0.24)
rlm@363 182 (def brick-height 0.12)
rlm@363 183 (def gravity (Vector3f. 0 -9.81 0))
rlm@363 184
rlm@363 185
rlm@363 186 (defn brick* [position]
rlm@363 187 (println "get brick.")
rlm@363 188 (doto (box brick-length brick-height brick-width
rlm@363 189 :position position :name "brick"
rlm@363 190 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@363 191 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@363 192 :mass 34)
rlm@363 193 (->
rlm@363 194 (.getMesh)
rlm@363 195 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@363 196 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@363 197 )
rlm@363 198 )
rlm@363 199
rlm@363 200
rlm@363 201 (defn floor*
rlm@363 202 "make a sturdy, unmovable physical floor"
rlm@363 203 []
rlm@366 204 (box 10 0.1 5 :name "floor" :mass 0
rlm@366 205 :color ColorRGBA/Gray :position (Vector3f. 0 0 0)))
rlm@363 206
rlm@363 207 (defn floor* []
rlm@363 208 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@363 209 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@363 210 :texture "Textures/BronzeCopper030.jpg"
rlm@363 211 :position (Vector3f. 0 0 0 )
rlm@363 212 :mass 0)
rlm@363 213 (->
rlm@363 214 (.getMesh)
rlm@363 215 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@363 216 (->
rlm@363 217 (.getMaterial)
rlm@363 218 (.getTextureParam "ColorMap")
rlm@363 219 (.getTextureValue)
rlm@363 220 (.setWrap Texture$WrapMode/Repeat))
rlm@363 221 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@363 222 ))
rlm@363 223
rlm@363 224
rlm@363 225 (defn brick-wall* []
rlm@363 226 (let [node (Node. "brick-wall")]
rlm@363 227 (dorun
rlm@363 228 (map
rlm@363 229 (comp #(.attachChild node %) brick*)
rlm@363 230 (for [y (range 10)
rlm@363 231 x (range 4)
rlm@363 232 z (range 1)]
rlm@363 233 (Vector3f.
rlm@363 234 (+ (* 2 x brick-length)
rlm@363 235 (if (even? (+ y z))
rlm@363 236 (/ brick-length 4) (/ brick-length -4)))
rlm@363 237 (+ (* brick-height (inc (* 2 y))))
rlm@363 238 (* 2 z brick-width) ))))
rlm@363 239 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@363 240 node))
rlm@363 241
rlm@363 242 (import com.aurellem.capture.Capture)
rlm@363 243
rlm@363 244 (import java.io.File)
rlm@363 245
rlm@364 246 (def base "/home/r/proj/cortex/render/gabor-1/")
rlm@363 247
rlm@363 248 (defn brick-wall-game-run [record?]
rlm@364 249 (let [capture-dir (File. base "main")]
rlm@364 250
rlm@364 251 (.mkdir (File. base "main"))
rlm@364 252 (doto
rlm@364 253 (world
rlm@364 254 (doto (Node.) (.attachChild (floor*))
rlm@364 255 (.attachChild (brick-wall*))
rlm@364 256 )
rlm@364 257 {"key-f" (fn [game value]
rlm@364 258 (if (not value) (add-element game (brick-wall*))))
rlm@364 259 "key-space" (fire-cannon-ball )}
rlm@364 260 (fn [world]
rlm@366 261 (position-camera
rlm@366 262 world
rlm@366 263 (Vector3f. 1.382548, 4.0383573, 5.994235)
rlm@366 264 (Quaternion. 0.0013082094, 0.98581666,
rlm@366 265 -0.1676442, 0.0076932586))
rlm@363 266
rlm@364 267 ;;(speed-up world)
rlm@364 268
rlm@364 269 (if record?
rlm@364 270 (Capture/captureVideo
rlm@364 271 world capture-dir))
rlm@364 272
rlm@363 273 (add-camera! world (.getCamera world) no-op))
rlm@364 274 (fn [& _]))
rlm@364 275 (.start))))
rlm@363 276
rlm@364 277 (defn convolve-preview [kernel]
rlm@363 278 (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png"
rlm@357 279
rlm@357 280
rlm@357 281 output "/home/r/ppp.png"
rlm@356 282
rlm@357 283 i (org.opencv.highgui.Highgui/imread input)
rlm@358 284
rlm@364 285 ;;kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
rlm@358 286
rlm@358 287 new-mat (Mat.)
rlm@358 288
rlm@357 289 ]
rlm@356 290
rlm@373 291 (org.opencv.imgproc.Imgproc/filter2D
rlm@373 292 i new-mat CvType/CV_32F kernel)
rlm@358 293
rlm@358 294 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
rlm@358 295
rlm@357 296 (view (ImagePlus. input))
rlm@361 297 (view (ImagePlus. output))))
rlm@357 298
rlm@364 299 (use 'clojure.java.shell)
rlm@363 300
rlm@363 301
rlm@364 302 (defn apply-gabor [kernel source dest]
rlm@364 303 (let [i (org.opencv.highgui.Highgui/imread source)
rlm@364 304 new-mat (Mat.)]
rlm@364 305
rlm@364 306 (println dest)
rlm@364 307 (if (not (.exists (File. dest)))
rlm@364 308 (do
rlm@373 309 (org.opencv.imgproc.Imgproc/filter2D
rlm@373 310 i new-mat CvType/CV_32F kernel)
rlm@364 311 (org.opencv.highgui.Highgui/imwrite dest new-mat)
rlm@364 312 (println "mogrify" "-modulate" "1000%" dest)
rlm@364 313 (sh "mogrify" "-modulate" "1000%" dest)))))
rlm@363 314
rlm@363 315
rlm@364 316 (import java.io.File)
rlm@363 317
rlm@364 318 (defn images [path]
rlm@364 319 (sort (rest (file-seq (File. path)))))
rlm@364 320
rlm@364 321
rlm@364 322
rlm@364 323 (defn pics [file]
rlm@364 324 (images (str base file)))
rlm@364 325
rlm@364 326 (defn generate-gabor-images [kernel name]
rlm@364 327 (draw-kernel! kernel (str base name ".png"))
rlm@364 328
rlm@364 329 (.mkdir (File. (str base name)))
rlm@364 330
rlm@364 331 (let [main (map #(.getCanonicalPath %) (pics "main"))
rlm@364 332 targets (map #(str base name "/" (format "%07d.png" %))
rlm@364 333 (range 0 (count main)))]
rlm@364 334 (dorun (pmap (partial apply-gabor kernel) main targets))))
rlm@364 335
rlm@364 336
rlm@364 337 (def banks
rlm@364 338 [[(gabor-kernel 2.8 3.5) "bank-1-1"]
rlm@364 339 [(gabor-kernel 2.8 3.5 (/ Math/PI 2)) "bank-1-1-rot"]
rlm@364 340
rlm@364 341 ;; [(gabor-kernel 3.6 4.6) "bank-1-2"]
rlm@364 342 ;; [(gabor-kernel 4.5 5.6) "bank-2-1"]
rlm@364 343 ;; [(gabor-kernel 6.3 7.9) "bank-3-1"]
rlm@364 344 ;; [(gabor-kernel 7.3 9.1) "bank-3-2"]
rlm@364 345
rlm@364 346 [(gabor-kernel 12.3 15.4) "bank-6-1"]
rlm@364 347
rlm@364 348
rlm@364 349 ;; [(gabor-kernel 17 21.2) "bank-8-1"]
rlm@364 350 ;; [(gabor-kernel 18.2 22.8) "bank-8-2"]
rlm@364 351 ])
rlm@364 352
rlm@364 353
rlm@364 354 (defn make-all-images []
rlm@364 355 (dorun (map (partial apply generate-gabor-images) banks)))
rlm@364 356
rlm@364 357
rlm@364 358
rlm@364 359 (defn compile-left-right []
rlm@364 360 (.mkdir (File. (str base "left-right")))
rlm@364 361 (let [main (pics "main")
rlm@364 362 left (pics "bank-1-1")
rlm@364 363 right (pics "bank-1-1-rot")
rlm@364 364 left-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
rlm@364 365 right-kernel (repeat 20000 (File. (str base "bank-1-1-rot.png")))
rlm@364 366 targets (map
rlm@364 367 #(File. (str base "left-right/" (format "%07d.png" %)))
rlm@364 368 (range 0 (count main)))]
rlm@364 369
rlm@364 370 (dorun
rlm@364 371 (pmap
rlm@364 372 (comp
rlm@364 373 (fn [[main left right left-kernel right-kernel target]]
rlm@364 374 (println target)
rlm@364 375 (if (not (.exists (File. target)))
rlm@364 376 (sh "convert"
rlm@364 377 "-size" "1940x515" "xc:white"
rlm@364 378 main "-geometry" "+0+0" "-composite"
rlm@364 379 left "-geometry" "+650+0" "-composite"
rlm@364 380 right "-geometry" "+1300+0" "-composite"
rlm@364 381 left-kernel "-geometry" "+960+485" "-composite"
rlm@364 382 right-kernel "-geometry" "+1610+485" "-composite"
rlm@364 383 target)))
rlm@364 384 (fn [& args] (map #(.getCanonicalPath %) args)))
rlm@364 385 main left right left-kernel right-kernel targets))))
rlm@364 386
rlm@364 387
rlm@364 388 (defn compile-big-small []
rlm@364 389 (.mkdir (File. (str base "big-small")))
rlm@364 390 (let [main (pics "main")
rlm@364 391 left (pics "bank-1-1")
rlm@364 392 right (pics "bank-6-1")
rlm@364 393 small-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
rlm@364 394 big-kernel (repeat 20000 (File. (str base "bank-6-1.png")))
rlm@364 395 targets (map
rlm@364 396 #(File. (str base "big-small/" (format "%07d.png" %)))
rlm@364 397 (range 0 (count main)))]
rlm@364 398
rlm@364 399 (dorun
rlm@364 400 (pmap
rlm@364 401 (comp
rlm@364 402 (fn [[main left right small-kernel big-kernel target]]
rlm@364 403 (println target)
rlm@364 404 (if (not (.exists (File. target)))
rlm@364 405 (sh "convert"
rlm@364 406 "-size" "1940x610" "xc:white"
rlm@364 407 main "-geometry" "+0+0" "-composite"
rlm@364 408 left "-geometry" "+650+0" "-composite"
rlm@364 409 right "-geometry" "+1300+0" "-composite"
rlm@364 410 small-kernel "-geometry" "+960+485" "-composite"
rlm@364 411 big-kernel "-geometry" "+1560+485" "-composite"
rlm@364 412 target)))
rlm@364 413 (fn [& args] (map #(.getCanonicalPath %) args)))
rlm@364 414 main left right small-kernel big-kernel targets))))
rlm@364 415
rlm@364 416
rlm@364 417 (defn regen-everything []
rlm@364 418 (make-all-images)
rlm@364 419 (compile-left-right)
rlm@364 420 (compile-big-small))
rlm@364 421
rlm@364 422
rlm@364 423 #+end_src
rlm@364 424
rlm@364 425
rlm@370 426 #+name: make-videos
rlm@370 427 #+begin_src makefile
rlm@370 428 scale:
rlm@370 429 ffmpeg -framerate 60 -i ./big-small/%07d.png -b:v 9000k\
rlm@370 430 -c:v theora -r 60 gabor-scale.ogg
rlm@364 431
rlm@370 432 rotation:
rlm@370 433 ffmpeg -framerate 60 -i ./left-right/%07d.png -b:v 9000k\
rlm@370 434 -c:v theora -r 60 gabor-rotation.ogg
rlm@370 435
rlm@370 436 all: rotation scale
rlm@370 437
rlm@370 438 clean:
rlm@370 439 rm gabor-rotation.org gabor-scale.ogg
rlm@364 440 #+end_src
rlm@364 441
rlm@364 442
rlm@365 443 #+begin_html
rlm@365 444 <div class="figure">
rlm@370 445 <video controls="controls">
rlm@372 446 <source src="../video/gabor-rotation.ogg" type="video/ogg"
rlm@365 447 preload="none" poster="../images/aurellem-1280x480.png" />
rlm@365 448 </video>
rlm@367 449 <br> <a href="http://youtu.be/bxujjO97B-U"> YouTube </a>
rlm@365 450 <p>Two gabor filters with different values of theta are compared. The
rlm@365 451 horizontally aligned one does better in this example.</p>
rlm@365 452 </div>
rlm@365 453 #+end_html
rlm@365 454
rlm@365 455
rlm@365 456
rlm@365 457 #+begin_html
rlm@365 458 <div class="figure">
rlm@370 459 <video controls="controls">
rlm@370 460 <source src="../video/gabor-scale.ogg" type="video/ogg"
rlm@365 461 preload="none" poster="../images/aurellem-1280x480.png" />
rlm@365 462 </video>
rlm@367 463 <br> <a href="http://youtu.be/-EsfA2ceWUk"> YouTube </a>
rlm@365 464 <p>Here we compare a gabor filter from bank 1 with one from bank 6.</p>
rlm@365 465 </div>
rlm@365 466 #+end_html
rlm@365 467
rlm@374 468 * Source Listing
rlm@374 469 - [[../src/cortex/gabor.clj][cortex.gabor]]
rlm@374 470 #+html: <ul> <li> <a href="../org/gabor.org">This org file</a> </li> </ul>
rlm@374 471 - [[http://hg.bortreb.com ][source-repository]]
rlm@374 472
rlm@365 473
rlm@356 474
rlm@356 475 * COMMENT Generate Source
rlm@356 476 #+begin_src clojure :tangle ../src/cortex/gabor.clj
rlm@356 477 <<gabor>>
rlm@364 478 <<gabor-tail>>
rlm@356 479 #+end_src
rlm@364 480
rlm@370 481 #+begin_src Makefile :tangle ../render/gabor-1/Makefile
rlm@370 482 <<make-videos>>
rlm@370 483 #+end_src