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@356
|
11
|
rlm@356
|
12 #+name: gabor
|
rlm@356
|
13 #+begin_src clojure
|
rlm@357
|
14 (ns cortex.gabor
|
rlm@357
|
15 (:import org.opencv.core.CvType)
|
rlm@357
|
16 (:import java.awt.image.BufferedImage)
|
rlm@357
|
17 (:import ij.ImagePlus)
|
rlm@358
|
18 (:import org.opencv.core.Mat)
|
rlm@357
|
19 (:use cortex.sense)
|
rlm@361
|
20 (:use cortex.util))
|
rlm@356
|
21
|
rlm@356
|
22 (defn load-opencv
|
rlm@356
|
23 "Load the opencv native library. Must be called before any OpenCV
|
rlm@356
|
24 stuff is used."
|
rlm@356
|
25 []
|
rlm@356
|
26 (clojure.lang.RT/loadLibrary "opencv_java249"))
|
rlm@356
|
27
|
rlm@359
|
28 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
|
rlm@359
|
29
|
rlm@359
|
30 ;; first, find the size of the kernel which is required
|
rlm@359
|
31 (let [square #(expt % 2)
|
rlm@359
|
32 rotated (fn [[x y]]
|
rlm@359
|
33 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
|
rlm@359
|
34 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
|
rlm@359
|
35
|
rlm@359
|
36 gaussian (fn [[x y]]
|
rlm@359
|
37 (let [[x' y'] (rotated [x y])]
|
rlm@359
|
38 (Math/exp (- (/ (+ (square x')
|
rlm@359
|
39 (square (* aspect-ratio y')))
|
rlm@359
|
40 (* 2 (square sigma)))))))
|
rlm@359
|
41 sinusoid (fn [[x y]]
|
rlm@359
|
42 (let [[x' y'] (rotated [x y])]
|
rlm@359
|
43 (Math/cos
|
rlm@359
|
44 (+ (* 2 Math/PI (/ x' wavelength))
|
rlm@359
|
45 phase-offset))))
|
rlm@359
|
46
|
rlm@361
|
47 half-width
|
rlm@361
|
48 (let [std-dev-capture 5]
|
rlm@361
|
49 (max
|
rlm@361
|
50 (int (* std-dev-capture (/ sigma aspect-ratio)))
|
rlm@361
|
51 (int (* std-dev-capture sigma))
|
rlm@361
|
52 (int (* std-dev-capture (/ aspect-ratio sigma)))))
|
rlm@359
|
53
|
rlm@359
|
54 grid (let [axis (range (- half-width) (inc half-width))]
|
rlm@359
|
55 (for [y (reverse axis) x axis] (vector x y)))
|
rlm@359
|
56
|
rlm@359
|
57 scale (reduce + (map gaussian grid))
|
rlm@359
|
58
|
rlm@359
|
59 gabor (fn [[x y :as coord]]
|
rlm@360
|
60 (* (sinusoid coord) (gaussian coord) (/ scale)))
|
rlm@359
|
61
|
rlm@359
|
62 mat-width (+ 1 (* 2 half-width))
|
rlm@359
|
63 mat (Mat. mat-width mat-width CvType/CV_32F)]
|
rlm@359
|
64
|
rlm@359
|
65 (.put mat 0 0 (float-array (map gabor grid)))
|
rlm@361
|
66 mat))
|
rlm@359
|
67
|
rlm@359
|
68
|
rlm@361
|
69 (defn draw-kernel! [kernel img-path]
|
rlm@361
|
70 (let [output img-path
|
rlm@360
|
71 size (.size kernel)
|
rlm@360
|
72 width (int (.width size))
|
rlm@360
|
73 height (int (.height size))
|
rlm@360
|
74 tmp-array (float-array (* width height))]
|
rlm@360
|
75
|
rlm@360
|
76 ;; read values from matrix.
|
rlm@360
|
77 (.get kernel 0 0 tmp-array)
|
rlm@360
|
78
|
rlm@360
|
79 ;; find overall dynamic range of the filter
|
rlm@360
|
80 (let [vals (vec tmp-array)
|
rlm@360
|
81 low (apply min vals)
|
rlm@360
|
82 high (apply max vals)
|
rlm@360
|
83 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
|
rlm@360
|
84 new-mat (Mat. height width CvType/CV_32F)]
|
rlm@360
|
85 (.put new-mat 0 0 (float-array scaled-vals))
|
rlm@361
|
86 (org.opencv.highgui.Highgui/imwrite output new-mat))))
|
rlm@361
|
87
|
rlm@361
|
88 (defn show-kernel [kernel]
|
rlm@361
|
89 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
|
rlm@362
|
90 (draw-kernel! kernel img-path)
|
rlm@361
|
91 (view (ImagePlus. output))))
|
rlm@359
|
92
|
rlm@359
|
93 (defn print-kernel [kernel]
|
rlm@359
|
94 (println (.dump kernel)))
|
rlm@359
|
95
|
rlm@357
|
96 (defn convolve-practice []
|
rlm@357
|
97 (let [input "/home/r/proj/cortex/images/dominos.jpg"
|
rlm@357
|
98
|
rlm@357
|
99
|
rlm@357
|
100 output "/home/r/ppp.png"
|
rlm@356
|
101
|
rlm@357
|
102 i (org.opencv.highgui.Highgui/imread input)
|
rlm@358
|
103
|
rlm@360
|
104 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
|
rlm@358
|
105
|
rlm@358
|
106 new-mat (Mat.)
|
rlm@358
|
107
|
rlm@357
|
108 ]
|
rlm@356
|
109
|
rlm@360
|
110 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
|
rlm@358
|
111
|
rlm@358
|
112 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
|
rlm@358
|
113
|
rlm@357
|
114 (view (ImagePlus. input))
|
rlm@361
|
115 (view (ImagePlus. output))))
|
rlm@357
|
116
|
rlm@356
|
117 #+end_src
|
rlm@356
|
118
|
rlm@356
|
119
|
rlm@356
|
120
|
rlm@356
|
121 * COMMENT Generate Source
|
rlm@356
|
122 #+begin_src clojure :tangle ../src/cortex/gabor.clj
|
rlm@356
|
123 <<gabor>>
|
rlm@356
|
124 #+end_src
|