view org/gabor.org @ 360:fc5bb270596a

completed kernel visualization code.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 03:12:25 +0000
parents 744ae7ef9b14
children 09461dce2e2f
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)
21 )
23 (defn load-opencv
24 "Load the opencv native library. Must be called before any OpenCV
25 stuff is used."
26 []
27 (clojure.lang.RT/loadLibrary "opencv_java249"))
29 (defn make-kernel []
30 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)]
31 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25))))
32 (println (.dump r))
34 r
37 ))
39 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
41 ;; first, find the size of the kernel which is required
42 (let [square #(expt % 2)
43 rotated (fn [[x y]]
44 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
45 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
47 gaussian (fn [[x y]]
48 (let [[x' y'] (rotated [x y])]
49 (Math/exp (- (/ (+ (square x')
50 (square (* aspect-ratio y')))
51 (* 2 (square sigma)))))))
52 sinusoid (fn [[x y]]
53 (let [[x' y'] (rotated [x y])]
54 (Math/cos
55 (+ (* 2 Math/PI (/ x' wavelength))
56 phase-offset))))
58 half-width (max
59 (int (* 5 (/ sigma aspect-ratio)))
60 (int (* 5 sigma))
61 (int (* 5 (/ aspect-ratio sigma))))
63 grid (let [axis (range (- half-width) (inc half-width))]
64 (for [y (reverse axis) x axis] (vector x y)))
66 scale (reduce + (map gaussian grid))
68 gabor (fn [[x y :as coord]]
69 (* (sinusoid coord) (gaussian coord) (/ scale)))
71 mat-width (+ 1 (* 2 half-width))
72 mat (Mat. mat-width mat-width CvType/CV_32F)]
75 (.put mat 0 0 (float-array (map gabor grid)))
76 mat
78 ;;(map gabor grid)
80 ))
83 (defn show-kernel [kernel]
84 (let [output "/home/r/proj/cortex/tmp/kernel.png"
85 size (.size kernel)
86 width (int (.width size))
87 height (int (.height size))
88 tmp-array (float-array (* width height))]
90 ;; read values from matrix.
91 (.get kernel 0 0 tmp-array)
93 ;; find overall dynamic range of the filter
94 (let [vals (vec tmp-array)
95 low (apply min vals)
96 high (apply max vals)
97 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
98 new-mat (Mat. height width CvType/CV_32F)]
99 (.put new-mat 0 0 (float-array scaled-vals))
100 (org.opencv.highgui.Highgui/imwrite output new-mat)
101 (view (ImagePlus. output)))))
103 (defn print-kernel [kernel]
104 (println (.dump kernel)))
114 (defn convolve-practice []
115 (let [input "/home/r/proj/cortex/images/dominos.jpg"
118 output "/home/r/ppp.png"
120 i (org.opencv.highgui.Highgui/imread input)
122 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
124 new-mat (Mat.)
126 ]
128 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
130 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
132 (view (ImagePlus. input))
133 (view (ImagePlus. output))
135 ))
143 (comment
144 ;; these work
145 (def i (org.opencv.highgui.Highgui/imread
146 "/home/r/proj/cortex/images/dominos.jpg"))
148 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" i)
149 )
150 #+end_src
154 * COMMENT Generate Source
155 #+begin_src clojure :tangle ../src/cortex/gabor.clj
156 <<gabor>>
157 #+end_src