view org/gabor.org @ 362:4b229dc028b6

more cleanup.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 03:28:26 +0000
parents 09461dce2e2f
children 9fa92af29c3a
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.sense)
20 (:use cortex.util))
22 (defn load-opencv
23 "Load the opencv native library. Must be called before any OpenCV
24 stuff is used."
25 []
26 (clojure.lang.RT/loadLibrary "opencv_java249"))
28 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
30 ;; first, find the size of the kernel which is required
31 (let [square #(expt % 2)
32 rotated (fn [[x y]]
33 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
34 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
36 gaussian (fn [[x y]]
37 (let [[x' y'] (rotated [x y])]
38 (Math/exp (- (/ (+ (square x')
39 (square (* aspect-ratio y')))
40 (* 2 (square sigma)))))))
41 sinusoid (fn [[x y]]
42 (let [[x' y'] (rotated [x y])]
43 (Math/cos
44 (+ (* 2 Math/PI (/ x' wavelength))
45 phase-offset))))
47 half-width
48 (let [std-dev-capture 5]
49 (max
50 (int (* std-dev-capture (/ sigma aspect-ratio)))
51 (int (* std-dev-capture sigma))
52 (int (* std-dev-capture (/ aspect-ratio sigma)))))
54 grid (let [axis (range (- half-width) (inc half-width))]
55 (for [y (reverse axis) x axis] (vector x y)))
57 scale (reduce + (map gaussian grid))
59 gabor (fn [[x y :as coord]]
60 (* (sinusoid coord) (gaussian coord) (/ scale)))
62 mat-width (+ 1 (* 2 half-width))
63 mat (Mat. mat-width mat-width CvType/CV_32F)]
65 (.put mat 0 0 (float-array (map gabor grid)))
66 mat))
69 (defn draw-kernel! [kernel img-path]
70 (let [output img-path
71 size (.size kernel)
72 width (int (.width size))
73 height (int (.height size))
74 tmp-array (float-array (* width height))]
76 ;; read values from matrix.
77 (.get kernel 0 0 tmp-array)
79 ;; find overall dynamic range of the filter
80 (let [vals (vec tmp-array)
81 low (apply min vals)
82 high (apply max vals)
83 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
84 new-mat (Mat. height width CvType/CV_32F)]
85 (.put new-mat 0 0 (float-array scaled-vals))
86 (org.opencv.highgui.Highgui/imwrite output new-mat))))
88 (defn show-kernel [kernel]
89 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
90 (draw-kernel! kernel img-path)
91 (view (ImagePlus. output))))
93 (defn print-kernel [kernel]
94 (println (.dump kernel)))
96 (defn convolve-practice []
97 (let [input "/home/r/proj/cortex/images/dominos.jpg"
100 output "/home/r/ppp.png"
102 i (org.opencv.highgui.Highgui/imread input)
104 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
106 new-mat (Mat.)
108 ]
110 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
112 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
114 (view (ImagePlus. input))
115 (view (ImagePlus. output))))
117 #+end_src
121 * COMMENT Generate Source
122 #+begin_src clojure :tangle ../src/cortex/gabor.clj
123 <<gabor>>
124 #+end_src