view org/gabor.org @ 363:9fa92af29c3a

saving ...
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 05:36:17 +0000
parents 4b229dc028b6
children b599a189433b
line wrap: on
line source
1 #+title: Fun with Gabor Filters
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: gabor filters in clojure with opencv
5 #+keywords: computer vision, jMonkeyEngine3, clojure, opencv
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
8 #+babel: :mkdirp yes :noweb yes :exports both
12 #+name: gabor
13 #+begin_src clojure
14 (ns cortex.gabor
15 (:import org.opencv.core.CvType)
16 (:import java.awt.image.BufferedImage)
17 (:import ij.ImagePlus)
18 (:import org.opencv.core.Mat)
19 (:use (cortex world sense util vision))
20 (:import com.jme3.post.SceneProcessor)
21 (:import (com.jme3.util BufferUtils Screenshots))
22 (:import java.nio.ByteBuffer)
23 (:import java.awt.image.BufferedImage)
24 (:import (com.jme3.renderer ViewPort Camera))
25 (:import (com.jme3.math ColorRGBA Vector3f Matrix3f Vector2f))
26 (:import com.jme3.renderer.Renderer)
27 (:import com.jme3.app.Application)
28 (:import com.jme3.texture.FrameBuffer)
29 (:import (com.jme3.scene Node Spatial)))
32 (cortex.import/mega-import-jme3)
34 (defn load-opencv
35 "Load the opencv native library. Must be called before any OpenCV
36 stuff is used."
37 []
38 (clojure.lang.RT/loadLibrary "opencv_java249"))
40 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
42 ;; first, find the size of the kernel which is required
43 (let [square #(expt % 2)
44 rotated (fn [[x y]]
45 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
46 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
48 gaussian (fn [[x y]]
49 (let [[x' y'] (rotated [x y])]
50 (Math/exp (- (/ (+ (square x')
51 (square (* aspect-ratio y')))
52 (* 2 (square sigma)))))))
53 sinusoid (fn [[x y]]
54 (let [[x' y'] (rotated [x y])]
55 (Math/cos
56 (+ (* 2 Math/PI (/ x' wavelength))
57 phase-offset))))
59 half-width
60 (let [std-dev-capture 5]
61 (max
62 (int (* std-dev-capture (/ sigma aspect-ratio)))
63 (int (* std-dev-capture sigma))
64 (int (* std-dev-capture (/ aspect-ratio sigma)))))
66 grid (let [axis (range (- half-width) (inc half-width))]
67 (for [y (reverse axis) x axis] (vector x y)))
69 scale (reduce + (map gaussian grid))
71 gabor (fn [[x y :as coord]]
72 (* (sinusoid coord) (gaussian coord) (/ scale)))
74 mat-width (+ 1 (* 2 half-width))
75 mat (Mat. mat-width mat-width CvType/CV_32F)]
77 (.put mat 0 0 (float-array (map gabor grid)))
78 mat))
81 (defn draw-kernel! [kernel img-path]
82 (let [output img-path
83 size (.size kernel)
84 width (int (.width size))
85 height (int (.height size))
86 tmp-array (float-array (* width height))]
88 ;; read values from matrix.
89 (.get kernel 0 0 tmp-array)
91 ;; find overall dynamic range of the filter
92 (let [vals (vec tmp-array)
93 low (apply min vals)
94 high (apply max vals)
95 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
96 new-mat (Mat. height width CvType/CV_32F)]
97 (.put new-mat 0 0 (float-array scaled-vals))
98 (org.opencv.highgui.Highgui/imwrite output new-mat))))
100 (defn show-kernel [kernel]
101 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
102 (draw-kernel! kernel img-path)
103 (view (ImagePlus. output))))
105 (defn print-kernel [kernel]
106 (println (.dump kernel)))
109 (def brick-length 0.48)
110 (def brick-width 0.24)
111 (def brick-height 0.12)
112 (def gravity (Vector3f. 0 -9.81 0))
115 (defn brick* [position]
116 (println "get brick.")
117 (doto (box brick-length brick-height brick-width
118 :position position :name "brick"
119 :material "Common/MatDefs/Misc/Unshaded.j3md"
120 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
121 :mass 34)
122 (->
123 (.getMesh)
124 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
125 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
126 )
127 )
130 (defn floor*
131 "make a sturdy, unmovable physical floor"
132 []
133 (box 10 0.1 5 :name "floor" :mass 0 :color ColorRGBA/Gray :position (Vector3f. 0 0 0)))
135 (defn floor* []
136 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
137 :material "Common/MatDefs/Misc/Unshaded.j3md"
138 :texture "Textures/BronzeCopper030.jpg"
139 :position (Vector3f. 0 0 0 )
140 :mass 0)
141 (->
142 (.getMesh)
143 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
144 (->
145 (.getMaterial)
146 (.getTextureParam "ColorMap")
147 (.getTextureValue)
148 (.setWrap Texture$WrapMode/Repeat))
149 (.setShadowMode RenderQueue$ShadowMode/Receive)
150 ))
153 (defn brick-wall* []
154 (let [node (Node. "brick-wall")]
155 (dorun
156 (map
157 (comp #(.attachChild node %) brick*)
158 (for [y (range 10)
159 x (range 4)
160 z (range 1)]
161 (Vector3f.
162 (+ (* 2 x brick-length)
163 (if (even? (+ y z))
164 (/ brick-length 4) (/ brick-length -4)))
165 (+ (* brick-height (inc (* 2 y))))
166 (* 2 z brick-width) ))))
167 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
168 node))
170 (import com.aurellem.capture.Capture)
172 (import java.io.File)
175 (defn brick-wall-game-run [record?]
176 (doto
177 (world
178 (doto (Node.) (.attachChild (floor*))
179 (.attachChild (brick-wall*))
180 )
181 {"key-f" (fn [game value]
182 (if (not value) (add-element game (brick-wall*))))
183 "key-space" (fire-cannon-ball )}
184 (fn [world]
185 (position-camera world
186 (Vector3f. 1.382548, 4.0383573, 5.994235)
187 (Quaternion. 0.0013082094, 0.98581666, -0.1676442, 0.0076932586))
189 ;;(speed-up world)
191 (if record?
192 (Capture/captureVideo
193 world
194 (File.
195 "/home/r/proj/cortex/render/gabor-1/main")))
196 (add-camera! world (.getCamera world) no-op))
197 (fn [& _]))
198 (.start)))
200 (defn convolve-practice [kernel]
201 (let [input "/home/r/proj/cortex/render/gabor-1/main/0000032.png"
204 output "/home/r/ppp.png"
206 i (org.opencv.highgui.Highgui/imread input)
208 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
210 new-mat (Mat.)
212 ]
214 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
216 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
218 (view (ImagePlus. input))
219 (view (ImagePlus. output))))
223 (defn generate-gabor-images []
224 (gabor-kernel 2.8 1 0 3.5 0)
229 #+end_src
233 * COMMENT Generate Source
234 #+begin_src clojure :tangle ../src/cortex/gabor.clj
235 <<gabor>>
236 #+end_src