annotate org/gabor.org @ 361:09461dce2e2f

cleanup.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 03:27:42 +0000
parents fc5bb270596a
children 4b229dc028b6
rev   line source
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@358 28 (defn make-kernel []
rlm@358 29 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)]
rlm@358 30 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25))))
rlm@358 31 (println (.dump r))
rlm@358 32
rlm@360 33 r
rlm@358 34
rlm@360 35
rlm@358 36 ))
rlm@356 37
rlm@359 38 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
rlm@359 39
rlm@359 40 ;; first, find the size of the kernel which is required
rlm@359 41 (let [square #(expt % 2)
rlm@359 42 rotated (fn [[x y]]
rlm@359 43 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
rlm@359 44 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
rlm@359 45
rlm@359 46 gaussian (fn [[x y]]
rlm@359 47 (let [[x' y'] (rotated [x y])]
rlm@359 48 (Math/exp (- (/ (+ (square x')
rlm@359 49 (square (* aspect-ratio y')))
rlm@359 50 (* 2 (square sigma)))))))
rlm@359 51 sinusoid (fn [[x y]]
rlm@359 52 (let [[x' y'] (rotated [x y])]
rlm@359 53 (Math/cos
rlm@359 54 (+ (* 2 Math/PI (/ x' wavelength))
rlm@359 55 phase-offset))))
rlm@359 56
rlm@361 57 half-width
rlm@361 58 (let [std-dev-capture 5]
rlm@361 59 (max
rlm@361 60 (int (* std-dev-capture (/ sigma aspect-ratio)))
rlm@361 61 (int (* std-dev-capture sigma))
rlm@361 62 (int (* std-dev-capture (/ aspect-ratio sigma)))))
rlm@359 63
rlm@359 64 grid (let [axis (range (- half-width) (inc half-width))]
rlm@359 65 (for [y (reverse axis) x axis] (vector x y)))
rlm@359 66
rlm@359 67 scale (reduce + (map gaussian grid))
rlm@359 68
rlm@359 69 gabor (fn [[x y :as coord]]
rlm@360 70 (* (sinusoid coord) (gaussian coord) (/ scale)))
rlm@359 71
rlm@359 72 mat-width (+ 1 (* 2 half-width))
rlm@359 73 mat (Mat. mat-width mat-width CvType/CV_32F)]
rlm@359 74
rlm@359 75 (.put mat 0 0 (float-array (map gabor grid)))
rlm@361 76 mat))
rlm@359 77
rlm@359 78
rlm@361 79 (defn draw-kernel! [kernel img-path]
rlm@361 80 (let [output img-path
rlm@360 81 size (.size kernel)
rlm@360 82 width (int (.width size))
rlm@360 83 height (int (.height size))
rlm@360 84 tmp-array (float-array (* width height))]
rlm@360 85
rlm@360 86 ;; read values from matrix.
rlm@360 87 (.get kernel 0 0 tmp-array)
rlm@360 88
rlm@360 89 ;; find overall dynamic range of the filter
rlm@360 90 (let [vals (vec tmp-array)
rlm@360 91 low (apply min vals)
rlm@360 92 high (apply max vals)
rlm@360 93 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
rlm@360 94 new-mat (Mat. height width CvType/CV_32F)]
rlm@360 95 (.put new-mat 0 0 (float-array scaled-vals))
rlm@361 96 (org.opencv.highgui.Highgui/imwrite output new-mat))))
rlm@361 97
rlm@361 98 (defn show-kernel [kernel]
rlm@361 99 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]
rlm@361 100 (draw-kernel kernel img-path)
rlm@361 101 (view (ImagePlus. output))))
rlm@359 102
rlm@359 103 (defn print-kernel [kernel]
rlm@359 104 (println (.dump kernel)))
rlm@359 105
rlm@359 106
rlm@359 107
rlm@359 108
rlm@360 109
rlm@359 110
rlm@359 111
rlm@359 112
rlm@359 113
rlm@357 114 (defn convolve-practice []
rlm@357 115 (let [input "/home/r/proj/cortex/images/dominos.jpg"
rlm@357 116
rlm@357 117
rlm@357 118 output "/home/r/ppp.png"
rlm@356 119
rlm@357 120 i (org.opencv.highgui.Highgui/imread input)
rlm@358 121
rlm@360 122 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
rlm@358 123
rlm@358 124 new-mat (Mat.)
rlm@358 125
rlm@357 126 ]
rlm@356 127
rlm@360 128 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
rlm@358 129
rlm@358 130 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
rlm@358 131
rlm@357 132 (view (ImagePlus. input))
rlm@361 133 (view (ImagePlus. output))))
rlm@357 134
rlm@357 135
rlm@357 136
rlm@357 137
rlm@357 138
rlm@357 139
rlm@357 140
rlm@357 141 (comment
rlm@357 142 ;; these work
rlm@357 143 (def i (org.opencv.highgui.Highgui/imread
rlm@357 144 "/home/r/proj/cortex/images/dominos.jpg"))
rlm@357 145
rlm@357 146 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" i)
rlm@357 147 )
rlm@356 148 #+end_src
rlm@356 149
rlm@356 150
rlm@356 151
rlm@356 152 * COMMENT Generate Source
rlm@356 153 #+begin_src clojure :tangle ../src/cortex/gabor.clj
rlm@356 154 <<gabor>>
rlm@356 155 #+end_src