changeset 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
files images/gabor-50-10-0-3.png images/gabor-50-10-pi-over-2.png images/gabor-50-10-pi-over-4.png images/gabor-50-10.png images/gabor-50-4-pi-over3-3.png images/gabor-50-50.png org/gabor.org
diffstat 7 files changed, 246 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file images/gabor-50-10-0-3.png has changed
     2.1 Binary file images/gabor-50-10-pi-over-2.png has changed
     3.1 Binary file images/gabor-50-10-pi-over-4.png has changed
     4.1 Binary file images/gabor-50-10.png has changed
     5.1 Binary file images/gabor-50-4-pi-over3-3.png has changed
     6.1 Binary file images/gabor-50-50.png has changed
     7.1 --- a/org/gabor.org	Thu Mar 07 05:36:17 2013 +0000
     7.2 +++ b/org/gabor.org	Thu Mar 07 07:42:32 2013 +0000
     7.3 @@ -16,7 +16,7 @@
     7.4    (:import java.awt.image.BufferedImage)
     7.5    (:import ij.ImagePlus)
     7.6    (:import org.opencv.core.Mat)
     7.7 -  (:use (cortex world sense util vision))
     7.8 +  (:use (cortex world sense util vision import))
     7.9    (:import com.jme3.post.SceneProcessor)
    7.10    (:import (com.jme3.util BufferUtils Screenshots))
    7.11    (:import java.nio.ByteBuffer)
    7.12 @@ -30,14 +30,21 @@
    7.13  
    7.14  
    7.15  (cortex.import/mega-import-jme3)
    7.16 -
    7.17 +(use  'clojure.math.numeric-tower)
    7.18  (defn load-opencv
    7.19    "Load the opencv native library. Must be called before any OpenCV
    7.20    stuff is used."
    7.21    []
    7.22    (clojure.lang.RT/loadLibrary "opencv_java249"))
    7.23  
    7.24 -(defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
    7.25 +(load-opencv)
    7.26 +
    7.27 +(defn gabor-kernel
    7.28 + ([sigma wavelength theta]
    7.29 +     (gabor-kernel sigma wavelength theta 1 0))
    7.30 +  ([sigma wavelength]
    7.31 +     (gabor-kernel sigma wavelength 0 1 0))
    7.32 +  ([sigma wavelength theta aspect-ratio phase-offset]
    7.33    
    7.34    ;; first, find the size of the kernel which is required
    7.35    (let [square #(expt % 2)
    7.36 @@ -75,7 +82,7 @@
    7.37          mat (Mat. mat-width mat-width CvType/CV_32F)]
    7.38      
    7.39      (.put mat 0 0 (float-array (map gabor grid)))
    7.40 -    mat))
    7.41 +    mat)))
    7.42  
    7.43  
    7.44  (defn draw-kernel! [kernel img-path]
    7.45 @@ -97,10 +104,73 @@
    7.46        (.put new-mat 0 0 (float-array scaled-vals))
    7.47        (org.opencv.highgui.Highgui/imwrite output new-mat))))
    7.48  
    7.49 +;; some cool examples
    7.50 +#+end_src
    7.51 +
    7.52 +
    7.53 +
    7.54 +
    7.55 +[[../images/gabor-50-10.png]]
    7.56 +
    7.57 +#+begin_src clojure
    7.58 +(def img-base "/home/r/proj/cortex/images/")
    7.59 +
    7.60 +(draw-kernel! (gabor-kernel 50 10 0 1 0)
    7.61 +              (str img-base "gabor-50-10.png"))
    7.62 +#+end_src
    7.63 +
    7.64 +
    7.65 +[[../images/gabor-50-10-pi-over-4.png]]
    7.66 +
    7.67 +#+begin_src clojure
    7.68 +(draw-kernel! (gabor-kernel 50 10 (/ Math/PI 4) 1 0)
    7.69 +              (str img-base "gabor-50-10-pi-over-4.png"))
    7.70 +#+end_src
    7.71 +
    7.72 +
    7.73 +[[../images/gabor-50-10-pi-over-2.png]]
    7.74 +
    7.75 +#+begin_src clojure
    7.76 +(draw-kernel! (gabor-kernel 50 10 (/ Math/PI 2) 1 0)
    7.77 +              (str img-base "gabor-50-10-pi-over-2.png"))
    7.78 +#+end_src
    7.79 +
    7.80 +
    7.81 +[[../images/gabor-50-50.png]]
    7.82 +
    7.83 +
    7.84 +
    7.85 +#+begin_src clojure
    7.86 +(draw-kernel! (gabor-kernel 50 50 0 1 0)
    7.87 +              (str img-base "gabor-50-50.png"))
    7.88 +
    7.89 +#+end_src
    7.90 +
    7.91 +[[../images/gabor-50-10-0-3.png]]
    7.92 +
    7.93 +#+begin_src clojure
    7.94 +(draw-kernel! (gabor-kernel 50 10 0 3 0)
    7.95 +              (str img-base "gabor-50-10-0-3.png"))
    7.96 +#+end_src
    7.97 +
    7.98 +
    7.99 +
   7.100 +[[../images/gabor-50-4-pi-over3-3.png]]
   7.101 +#+begin_src clojure
   7.102 +(draw-kernel! (gabor-kernel 50 4 (/ Math/PI 3) 3 0)
   7.103 +              (str img-base "gabor-50-4-pi-over3-3.png"))
   7.104 +#+end_src
   7.105 +
   7.106 +
   7.107 +
   7.108 +     
   7.109 +
   7.110 +#:name gabor-tail
   7.111 +#+begin_src clojure
   7.112  (defn show-kernel [kernel]
   7.113    (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
   7.114      (draw-kernel! kernel img-path)
   7.115 -    (view (ImagePlus. output))))
   7.116 +    (view (ImagePlus. img-path))))
   7.117  
   7.118  (defn print-kernel [kernel]
   7.119    (println (.dump kernel)))
   7.120 @@ -171,33 +241,36 @@
   7.121  
   7.122  (import java.io.File)
   7.123  
   7.124 +(def base "/home/r/proj/cortex/render/gabor-1/")
   7.125  
   7.126  (defn brick-wall-game-run [record?]
   7.127 -  (doto
   7.128 -      (world
   7.129 -       (doto (Node.) (.attachChild (floor*))
   7.130 -	     (.attachChild (brick-wall*))
   7.131 -	     )
   7.132 -       {"key-f" (fn [game value] 
   7.133 -		  (if (not value) (add-element game (brick-wall*))))
   7.134 -	"key-space" (fire-cannon-ball )}
   7.135 -       (fn [world]
   7.136 -         (position-camera world
   7.137 -          (Vector3f. 1.382548, 4.0383573, 5.994235)
   7.138 -          (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586))
   7.139 +  (let [capture-dir (File. base "main")]
   7.140 +    
   7.141 +    (.mkdir (File. base "main"))
   7.142 +    (doto
   7.143 +        (world
   7.144 +         (doto (Node.) (.attachChild (floor*))
   7.145 +               (.attachChild (brick-wall*))
   7.146 +               )
   7.147 +         {"key-f" (fn [game value] 
   7.148 +                    (if (not value) (add-element game (brick-wall*))))
   7.149 +          "key-space" (fire-cannon-ball )}
   7.150 +         (fn [world]
   7.151 +           (position-camera world
   7.152 +                            (Vector3f. 1.382548, 4.0383573, 5.994235)
   7.153 +                            (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586))
   7.154  
   7.155 -         ;;(speed-up world)
   7.156 -         
   7.157 -         (if record?
   7.158 -           (Capture/captureVideo
   7.159 -            world
   7.160 -            (File.
   7.161 -             "/home/r/proj/cortex/render/gabor-1/main")))
   7.162 +           ;;(speed-up world)
   7.163 +           
   7.164 +           (if record?
   7.165 +             (Capture/captureVideo
   7.166 +              world capture-dir))
   7.167 +              
   7.168             (add-camera! world (.getCamera world) no-op))
   7.169 -       (fn [& _]))     
   7.170 -    (.start)))
   7.171 +         (fn [& _]))     
   7.172 +      (.start))))
   7.173  
   7.174 -(defn convolve-practice [kernel]
   7.175 +(defn convolve-preview [kernel]
   7.176    (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png"
   7.177          
   7.178          
   7.179 @@ -205,7 +278,7 @@
   7.180  
   7.181          i (org.opencv.highgui.Highgui/imread input)
   7.182  
   7.183 -        kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
   7.184 +        ;;kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
   7.185  
   7.186          new-mat (Mat.)
   7.187                  
   7.188 @@ -218,13 +291,147 @@
   7.189      (view (ImagePlus. input))
   7.190      (view (ImagePlus. output))))
   7.191  
   7.192 +(use 'clojure.java.shell)
   7.193  
   7.194  
   7.195 -(defn generate-gabor-images []
   7.196 -  (gabor-kernel 2.8 1 0 3.5 0)
   7.197 +(defn apply-gabor [kernel source dest]
   7.198 +    (let [i (org.opencv.highgui.Highgui/imread source)
   7.199 +          new-mat (Mat.)]
   7.200 +      
   7.201 +      (println dest)
   7.202 +      (if (not (.exists (File. dest)))
   7.203 +        (do
   7.204 +          (org.opencv.imgproc.Imgproc/filter2D  i new-mat CvType/CV_32F kernel)
   7.205 +          (org.opencv.highgui.Highgui/imwrite dest new-mat)
   7.206 +          (println "mogrify" "-modulate" "1000%" dest)
   7.207 +          (sh "mogrify" "-modulate" "1000%" dest)))))
   7.208  
   7.209  
   7.210 +(import java.io.File)
   7.211  
   7.212 +(defn images [path]
   7.213 +  (sort (rest (file-seq (File. path)))))
   7.214 +
   7.215 +
   7.216 +
   7.217 +(defn pics [file]
   7.218 +  (images (str base file)))
   7.219 +
   7.220 +(defn generate-gabor-images [kernel name]
   7.221 +  (draw-kernel! kernel (str base name ".png"))
   7.222 +
   7.223 +  (.mkdir (File. (str base name)))
   7.224 +  
   7.225 +  (let [main (map #(.getCanonicalPath %) (pics "main"))
   7.226 +        targets (map #(str base name "/" (format "%07d.png" %))
   7.227 +                     (range 0 (count main)))]
   7.228 +    (dorun (pmap (partial apply-gabor kernel) main targets))))
   7.229 +
   7.230 +
   7.231 +(def banks
   7.232 +  [[(gabor-kernel 2.8 3.5) "bank-1-1"]
   7.233 +   [(gabor-kernel 2.8 3.5 (/ Math/PI 2)) "bank-1-1-rot"]
   7.234 +   
   7.235 +;;   [(gabor-kernel 3.6 4.6) "bank-1-2"]
   7.236 +;;   [(gabor-kernel 4.5 5.6) "bank-2-1"]
   7.237 +;;   [(gabor-kernel 6.3 7.9) "bank-3-1"]
   7.238 +;;   [(gabor-kernel 7.3 9.1) "bank-3-2"]
   7.239 +
   7.240 +   [(gabor-kernel 12.3 15.4) "bank-6-1"]
   7.241 +   
   7.242 +
   7.243 +;;   [(gabor-kernel 17 21.2) "bank-8-1"]
   7.244 +;;   [(gabor-kernel 18.2 22.8) "bank-8-2"]
   7.245 +   ])
   7.246 +   
   7.247 +
   7.248 +(defn make-all-images []
   7.249 +  (dorun (map (partial apply generate-gabor-images) banks)))
   7.250 +
   7.251 +
   7.252 +
   7.253 +(defn compile-left-right []
   7.254 +  (.mkdir (File. (str base "left-right")))
   7.255 +  (let [main (pics "main")
   7.256 +        left (pics "bank-1-1")
   7.257 +        right (pics "bank-1-1-rot")
   7.258 +        left-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
   7.259 +        right-kernel (repeat 20000 (File. (str base "bank-1-1-rot.png")))
   7.260 +        targets (map
   7.261 +                 #(File. (str base "left-right/" (format "%07d.png" %)))
   7.262 +                 (range 0 (count main)))]
   7.263 +    
   7.264 +    (dorun
   7.265 +     (pmap
   7.266 +      (comp
   7.267 +       (fn [[main left right left-kernel right-kernel target]]
   7.268 +         (println target)
   7.269 +         (if (not (.exists (File. target)))
   7.270 +           (sh "convert"
   7.271 +               "-size" "1940x515" "xc:white"
   7.272 +               main      "-geometry" "+0+0"    "-composite"
   7.273 +               left "-geometry" "+650+0"   "-composite"
   7.274 +               right     "-geometry" "+1300+0"  "-composite"
   7.275 +               left-kernel      "-geometry" "+960+485"  "-composite"
   7.276 +               right-kernel      "-geometry" "+1610+485"  "-composite"
   7.277 +               target)))
   7.278 +       (fn [& args] (map #(.getCanonicalPath %) args)))
   7.279 +      main left right left-kernel right-kernel targets))))
   7.280 +
   7.281 +
   7.282 +(defn compile-big-small []
   7.283 +  (.mkdir (File. (str base "big-small")))
   7.284 +  (let [main (pics "main")
   7.285 +        left (pics "bank-1-1")
   7.286 +        right (pics "bank-6-1")
   7.287 +        small-kernel (repeat 20000 (File. (str base "bank-1-1.png")))
   7.288 +        big-kernel (repeat 20000 (File. (str base "bank-6-1.png")))
   7.289 +        targets (map
   7.290 +                 #(File. (str base "big-small/" (format "%07d.png" %)))
   7.291 +                 (range 0 (count main)))]
   7.292 +    
   7.293 +    (dorun
   7.294 +     (pmap
   7.295 +      (comp
   7.296 +       (fn [[main left right small-kernel big-kernel target]]
   7.297 +         (println target)
   7.298 +         (if (not (.exists (File. target)))
   7.299 +           (sh "convert"
   7.300 +               "-size" "1940x610" "xc:white"
   7.301 +               main      "-geometry" "+0+0"    "-composite"
   7.302 +               left "-geometry" "+650+0"   "-composite"
   7.303 +               right     "-geometry" "+1300+0"  "-composite"
   7.304 +               small-kernel      "-geometry" "+960+485"  "-composite"
   7.305 +               big-kernel      "-geometry" "+1560+485"  "-composite"
   7.306 +               target)))
   7.307 +       (fn [& args] (map #(.getCanonicalPath %) args)))
   7.308 +      main left right small-kernel big-kernel targets))))
   7.309 +
   7.310 +
   7.311 +(defn regen-everything []
   7.312 +  (make-all-images)
   7.313 +  (compile-left-right)
   7.314 +  (compile-big-small))
   7.315 +
   7.316 +      
   7.317 +#+end_src
   7.318 +
   7.319 +#+name: make-left-right
   7.320 +#+begin_src sh
   7.321 +#!/bin/sh
   7.322 +
   7.323 +ffmpeg -framerate 60 -i ./left-right/%07d.png -b:v 9000k\
   7.324 +     -c:v mpeg4 -r 60 gabor-rotation.avi
   7.325 +
   7.326 +#+end_src
   7.327 +
   7.328 +
   7.329 +#+name: make-big-small
   7.330 +#+begin_src sh
   7.331 +#!/bin/sh
   7.332 +
   7.333 +ffmpeg -framerate 60 -i ./big-small/%07d.png -b:v 9000k\
   7.334 +     -c:v mpeg4 -r 60 gabor-big-small.avi
   7.335  
   7.336  #+end_src
   7.337  
   7.338 @@ -233,4 +440,13 @@
   7.339  * COMMENT Generate Source
   7.340  #+begin_src clojure :tangle ../src/cortex/gabor.clj
   7.341  <<gabor>>
   7.342 +<<gabor-tail>>
   7.343  #+end_src
   7.344 +
   7.345 +#+begin_src clojure :tangle ../render/gabor-1/make-rotation.sh
   7.346 +<<make-left-right>>
   7.347 +#+end_src
   7.348 +
   7.349 +#+begin_src clojure :tangle ../render/gabor-1/make-big-small.sh
   7.350 +<<make-big-small>>
   7.351 +#+end_src