diff 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
line wrap: on
line diff
     1.1 --- a/org/gabor.org	Thu Mar 07 05:36:17 2013 +0000
     1.2 +++ b/org/gabor.org	Thu Mar 07 07:42:32 2013 +0000
     1.3 @@ -16,7 +16,7 @@
     1.4    (:import java.awt.image.BufferedImage)
     1.5    (:import ij.ImagePlus)
     1.6    (:import org.opencv.core.Mat)
     1.7 -  (:use (cortex world sense util vision))
     1.8 +  (:use (cortex world sense util vision import))
     1.9    (:import com.jme3.post.SceneProcessor)
    1.10    (:import (com.jme3.util BufferUtils Screenshots))
    1.11    (:import java.nio.ByteBuffer)
    1.12 @@ -30,14 +30,21 @@
    1.13  
    1.14  
    1.15  (cortex.import/mega-import-jme3)
    1.16 -
    1.17 +(use  'clojure.math.numeric-tower)
    1.18  (defn load-opencv
    1.19    "Load the opencv native library. Must be called before any OpenCV
    1.20    stuff is used."
    1.21    []
    1.22    (clojure.lang.RT/loadLibrary "opencv_java249"))
    1.23  
    1.24 -(defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
    1.25 +(load-opencv)
    1.26 +
    1.27 +(defn gabor-kernel
    1.28 + ([sigma wavelength theta]
    1.29 +     (gabor-kernel sigma wavelength theta 1 0))
    1.30 +  ([sigma wavelength]
    1.31 +     (gabor-kernel sigma wavelength 0 1 0))
    1.32 +  ([sigma wavelength theta aspect-ratio phase-offset]
    1.33    
    1.34    ;; first, find the size of the kernel which is required
    1.35    (let [square #(expt % 2)
    1.36 @@ -75,7 +82,7 @@
    1.37          mat (Mat. mat-width mat-width CvType/CV_32F)]
    1.38      
    1.39      (.put mat 0 0 (float-array (map gabor grid)))
    1.40 -    mat))
    1.41 +    mat)))
    1.42  
    1.43  
    1.44  (defn draw-kernel! [kernel img-path]
    1.45 @@ -97,10 +104,73 @@
    1.46        (.put new-mat 0 0 (float-array scaled-vals))
    1.47        (org.opencv.highgui.Highgui/imwrite output new-mat))))
    1.48  
    1.49 +;; some cool examples
    1.50 +#+end_src
    1.51 +
    1.52 +
    1.53 +
    1.54 +
    1.55 +[[../images/gabor-50-10.png]]
    1.56 +
    1.57 +#+begin_src clojure
    1.58 +(def img-base "/home/r/proj/cortex/images/")
    1.59 +
    1.60 +(draw-kernel! (gabor-kernel 50 10 0 1 0)
    1.61 +              (str img-base "gabor-50-10.png"))
    1.62 +#+end_src
    1.63 +
    1.64 +
    1.65 +[[../images/gabor-50-10-pi-over-4.png]]
    1.66 +
    1.67 +#+begin_src clojure
    1.68 +(draw-kernel! (gabor-kernel 50 10 (/ Math/PI 4) 1 0)
    1.69 +              (str img-base "gabor-50-10-pi-over-4.png"))
    1.70 +#+end_src
    1.71 +
    1.72 +
    1.73 +[[../images/gabor-50-10-pi-over-2.png]]
    1.74 +
    1.75 +#+begin_src clojure
    1.76 +(draw-kernel! (gabor-kernel 50 10 (/ Math/PI 2) 1 0)
    1.77 +              (str img-base "gabor-50-10-pi-over-2.png"))
    1.78 +#+end_src
    1.79 +
    1.80 +
    1.81 +[[../images/gabor-50-50.png]]
    1.82 +
    1.83 +
    1.84 +
    1.85 +#+begin_src clojure
    1.86 +(draw-kernel! (gabor-kernel 50 50 0 1 0)
    1.87 +              (str img-base "gabor-50-50.png"))
    1.88 +
    1.89 +#+end_src
    1.90 +
    1.91 +[[../images/gabor-50-10-0-3.png]]
    1.92 +
    1.93 +#+begin_src clojure
    1.94 +(draw-kernel! (gabor-kernel 50 10 0 3 0)
    1.95 +              (str img-base "gabor-50-10-0-3.png"))
    1.96 +#+end_src
    1.97 +
    1.98 +
    1.99 +
   1.100 +[[../images/gabor-50-4-pi-over3-3.png]]
   1.101 +#+begin_src clojure
   1.102 +(draw-kernel! (gabor-kernel 50 4 (/ Math/PI 3) 3 0)
   1.103 +              (str img-base "gabor-50-4-pi-over3-3.png"))
   1.104 +#+end_src
   1.105 +
   1.106 +
   1.107 +
   1.108 +     
   1.109 +
   1.110 +#:name gabor-tail
   1.111 +#+begin_src clojure
   1.112  (defn show-kernel [kernel]
   1.113    (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
   1.114      (draw-kernel! kernel img-path)
   1.115 -    (view (ImagePlus. output))))
   1.116 +    (view (ImagePlus. img-path))))
   1.117  
   1.118  (defn print-kernel [kernel]
   1.119    (println (.dump kernel)))
   1.120 @@ -171,33 +241,36 @@
   1.121  
   1.122  (import java.io.File)
   1.123  
   1.124 +(def base "/home/r/proj/cortex/render/gabor-1/")
   1.125  
   1.126  (defn brick-wall-game-run [record?]
   1.127 -  (doto
   1.128 -      (world
   1.129 -       (doto (Node.) (.attachChild (floor*))
   1.130 -	     (.attachChild (brick-wall*))
   1.131 -	     )
   1.132 -       {"key-f" (fn [game value] 
   1.133 -		  (if (not value) (add-element game (brick-wall*))))
   1.134 -	"key-space" (fire-cannon-ball )}
   1.135 -       (fn [world]
   1.136 -         (position-camera world
   1.137 -          (Vector3f. 1.382548, 4.0383573, 5.994235)
   1.138 -          (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586))
   1.139 +  (let [capture-dir (File. base "main")]
   1.140 +    
   1.141 +    (.mkdir (File. base "main"))
   1.142 +    (doto
   1.143 +        (world
   1.144 +         (doto (Node.) (.attachChild (floor*))
   1.145 +               (.attachChild (brick-wall*))
   1.146 +               )
   1.147 +         {"key-f" (fn [game value] 
   1.148 +                    (if (not value) (add-element game (brick-wall*))))
   1.149 +          "key-space" (fire-cannon-ball )}
   1.150 +         (fn [world]
   1.151 +           (position-camera world
   1.152 +                            (Vector3f. 1.382548, 4.0383573, 5.994235)
   1.153 +                            (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586))
   1.154  
   1.155 -         ;;(speed-up world)
   1.156 -         
   1.157 -         (if record?
   1.158 -           (Capture/captureVideo
   1.159 -            world
   1.160 -            (File.
   1.161 -             "/home/r/proj/cortex/render/gabor-1/main")))
   1.162 +           ;;(speed-up world)
   1.163 +           
   1.164 +           (if record?
   1.165 +             (Capture/captureVideo
   1.166 +              world capture-dir))
   1.167 +              
   1.168             (add-camera! world (.getCamera world) no-op))
   1.169 -       (fn [& _]))     
   1.170 -    (.start)))
   1.171 +         (fn [& _]))     
   1.172 +      (.start))))
   1.173  
   1.174 -(defn convolve-practice [kernel]
   1.175 +(defn convolve-preview [kernel]
   1.176    (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png"
   1.177          
   1.178          
   1.179 @@ -205,7 +278,7 @@
   1.180  
   1.181          i (org.opencv.highgui.Highgui/imread input)
   1.182  
   1.183 -        kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
   1.184 +        ;;kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
   1.185  
   1.186          new-mat (Mat.)
   1.187                  
   1.188 @@ -218,13 +291,147 @@
   1.189      (view (ImagePlus. input))
   1.190      (view (ImagePlus. output))))
   1.191  
   1.192 +(use 'clojure.java.shell)
   1.193  
   1.194  
   1.195 -(defn generate-gabor-images []
   1.196 -  (gabor-kernel 2.8 1 0 3.5 0)
   1.197 +(defn apply-gabor [kernel source dest]
   1.198 +    (let [i (org.opencv.highgui.Highgui/imread source)
   1.199 +          new-mat (Mat.)]
   1.200 +      
   1.201 +      (println dest)
   1.202 +      (if (not (.exists (File. dest)))
   1.203 +        (do
   1.204 +          (org.opencv.imgproc.Imgproc/filter2D  i new-mat CvType/CV_32F kernel)
   1.205 +          (org.opencv.highgui.Highgui/imwrite dest new-mat)
   1.206 +          (println "mogrify" "-modulate" "1000%" dest)
   1.207 +          (sh "mogrify" "-modulate" "1000%" dest)))))
   1.208  
   1.209  
   1.210 +(import java.io.File)
   1.211  
   1.212 +(defn images [path]
   1.213 +  (sort (rest (file-seq (File. path)))))
   1.214 +
   1.215 +
   1.216 +
   1.217 +(defn pics [file]
   1.218 +  (images (str base file)))
   1.219 +
   1.220 +(defn generate-gabor-images [kernel name]
   1.221 +  (draw-kernel! kernel (str base name ".png"))
   1.222 +
   1.223 +  (.mkdir (File. (str base name)))
   1.224 +  
   1.225 +  (let [main (map #(.getCanonicalPath %) (pics "main"))
   1.226 +        targets (map #(str base name "/" (format "%07d.png" %))
   1.227 +                     (range 0 (count main)))]
   1.228 +    (dorun (pmap (partial apply-gabor kernel) main targets))))
   1.229 +
   1.230 +
   1.231 +(def banks
   1.232 +  [[(gabor-kernel 2.8 3.5) "bank-1-1"]
   1.233 +   [(gabor-kernel 2.8 3.5 (/ Math/PI 2)) "bank-1-1-rot"]
   1.234 +   
   1.235 +;;   [(gabor-kernel 3.6 4.6) "bank-1-2"]
   1.236 +;;   [(gabor-kernel 4.5 5.6) "bank-2-1"]
   1.237 +;;   [(gabor-kernel 6.3 7.9) "bank-3-1"]
   1.238 +;;   [(gabor-kernel 7.3 9.1) "bank-3-2"]
   1.239 +
   1.240 +   [(gabor-kernel 12.3 15.4) "bank-6-1"]
   1.241 +   
   1.242 +
   1.243 +;;   [(gabor-kernel 17 21.2) "bank-8-1"]
   1.244 +;;   [(gabor-kernel 18.2 22.8) "bank-8-2"]
   1.245 +   ])
   1.246 +   
   1.247 +
   1.248 +(defn make-all-images []
   1.249 +  (dorun (map (partial apply generate-gabor-images) banks)))
   1.250 +
   1.251 +
   1.252 +
   1.253 +(defn compile-left-right []
   1.254 +  (.mkdir (File. (str base "left-right")))
   1.255 +  (let [main (pics "main")
   1.256 +        left (pics "bank-1-1")
   1.257 +        right (pics "bank-1-1-rot")
   1.258 +        left-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
   1.259 +        right-kernel (repeat 20000 (File. (str base "bank-1-1-rot.png")))
   1.260 +        targets (map
   1.261 +                 #(File. (str base "left-right/" (format "%07d.png" %)))
   1.262 +                 (range 0 (count main)))]
   1.263 +    
   1.264 +    (dorun
   1.265 +     (pmap
   1.266 +      (comp
   1.267 +       (fn [[main left right left-kernel right-kernel target]]
   1.268 +         (println target)
   1.269 +         (if (not (.exists (File. target)))
   1.270 +           (sh "convert"
   1.271 +               "-size" "1940x515" "xc:white"
   1.272 +               main      "-geometry" "+0+0"    "-composite"
   1.273 +               left "-geometry" "+650+0"   "-composite"
   1.274 +               right     "-geometry" "+1300+0"  "-composite"
   1.275 +               left-kernel      "-geometry" "+960+485"  "-composite"
   1.276 +               right-kernel      "-geometry" "+1610+485"  "-composite"
   1.277 +               target)))
   1.278 +       (fn [& args] (map #(.getCanonicalPath %) args)))
   1.279 +      main left right left-kernel right-kernel targets))))
   1.280 +
   1.281 +
   1.282 +(defn compile-big-small []
   1.283 +  (.mkdir (File. (str base "big-small")))
   1.284 +  (let [main (pics "main")
   1.285 +        left (pics "bank-1-1")
   1.286 +        right (pics "bank-6-1")
   1.287 +        small-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
   1.288 +        big-kernel (repeat 20000 (File. (str base "bank-6-1.png")))
   1.289 +        targets (map
   1.290 +                 #(File. (str base "big-small/" (format "%07d.png" %)))
   1.291 +                 (range 0 (count main)))]
   1.292 +    
   1.293 +    (dorun
   1.294 +     (pmap
   1.295 +      (comp
   1.296 +       (fn [[main left right small-kernel big-kernel target]]
   1.297 +         (println target)
   1.298 +         (if (not (.exists (File. target)))
   1.299 +           (sh "convert"
   1.300 +               "-size" "1940x610" "xc:white"
   1.301 +               main      "-geometry" "+0+0"    "-composite"
   1.302 +               left "-geometry" "+650+0"   "-composite"
   1.303 +               right     "-geometry" "+1300+0"  "-composite"
   1.304 +               small-kernel      "-geometry" "+960+485"  "-composite"
   1.305 +               big-kernel      "-geometry" "+1560+485"  "-composite"
   1.306 +               target)))
   1.307 +       (fn [& args] (map #(.getCanonicalPath %) args)))
   1.308 +      main left right small-kernel big-kernel targets))))
   1.309 +
   1.310 +
   1.311 +(defn regen-everything []
   1.312 +  (make-all-images)
   1.313 +  (compile-left-right)
   1.314 +  (compile-big-small))
   1.315 +
   1.316 +      
   1.317 +#+end_src
   1.318 +
   1.319 +#+name: make-left-right
   1.320 +#+begin_src sh
   1.321 +#!/bin/sh
   1.322 +
   1.323 +ffmpeg -framerate 60 -i ./left-right/%07d.png -b:v 9000k\
   1.324 +     -c:v mpeg4 -r 60 gabor-rotation.avi
   1.325 +
   1.326 +#+end_src
   1.327 +
   1.328 +
   1.329 +#+name: make-big-small
   1.330 +#+begin_src sh
   1.331 +#!/bin/sh
   1.332 +
   1.333 +ffmpeg -framerate 60 -i ./big-small/%07d.png -b:v 9000k\
   1.334 +     -c:v mpeg4 -r 60 gabor-big-small.avi
   1.335  
   1.336  #+end_src
   1.337  
   1.338 @@ -233,4 +440,13 @@
   1.339  * COMMENT Generate Source
   1.340  #+begin_src clojure :tangle ../src/cortex/gabor.clj
   1.341  <<gabor>>
   1.342 +<<gabor-tail>>
   1.343  #+end_src
   1.344 +
   1.345 +#+begin_src clojure :tangle ../render/gabor-1/make-rotation.sh
   1.346 +<<make-left-right>>
   1.347 +#+end_src
   1.348 +
   1.349 +#+begin_src clojure :tangle ../render/gabor-1/make-big-small.sh
   1.350 +<<make-big-small>>
   1.351 +#+end_src