annotate org/gabor.org @ 359:744ae7ef9b14

gabor filter looks right.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 02:37:49 +0000
parents b72fea69b2e1
children fc5bb270596a
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@357 20 (:use cortex.util)
rlm@357 21 )
rlm@356 22
rlm@356 23 (defn load-opencv
rlm@356 24 "Load the opencv native library. Must be called before any OpenCV
rlm@356 25 stuff is used."
rlm@356 26 []
rlm@356 27 (clojure.lang.RT/loadLibrary "opencv_java249"))
rlm@356 28
rlm@358 29 (defn make-kernel []
rlm@358 30 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)]
rlm@358 31 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25))))
rlm@358 32 (println (.dump r))
rlm@358 33
rlm@358 34 ;;r
rlm@358 35
rlm@359 36 (org.opencv.core.MatOfFloat. (float-array [0.5]))
rlm@358 37 ))
rlm@356 38
rlm@359 39 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
rlm@359 40
rlm@359 41 ;; first, find the size of the kernel which is required
rlm@359 42 (let [square #(expt % 2)
rlm@359 43 rotated (fn [[x y]]
rlm@359 44 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
rlm@359 45 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])
rlm@359 46
rlm@359 47 gaussian (fn [[x y]]
rlm@359 48 (let [[x' y'] (rotated [x y])]
rlm@359 49 (Math/exp (- (/ (+ (square x')
rlm@359 50 (square (* aspect-ratio y')))
rlm@359 51 (* 2 (square sigma)))))))
rlm@359 52 sinusoid (fn [[x y]]
rlm@359 53 (let [[x' y'] (rotated [x y])]
rlm@359 54 (Math/cos
rlm@359 55 (+ (* 2 Math/PI (/ x' wavelength))
rlm@359 56 phase-offset))))
rlm@359 57
rlm@359 58 half-width (max
rlm@359 59 (int (* 5 (/ sigma aspect-ratio)))
rlm@359 60 (int (* 5 sigma))
rlm@359 61 (int (* 5 (/ aspect-ratio sigma))))
rlm@359 62
rlm@359 63 grid (let [axis (range (- half-width) (inc half-width))]
rlm@359 64 (for [y (reverse axis) x axis] (vector x y)))
rlm@359 65
rlm@359 66 scale (reduce + (map gaussian grid))
rlm@359 67
rlm@359 68 gabor (fn [[x y :as coord]]
rlm@359 69 (* (sinusoid coord) (gaussian coord) scale))
rlm@359 70
rlm@359 71 mat-width (+ 1 (* 2 half-width))
rlm@359 72 mat (Mat. mat-width mat-width CvType/CV_32F)]
rlm@359 73
rlm@359 74
rlm@359 75 (.put mat 0 0 (float-array (map gabor grid)))
rlm@359 76 mat
rlm@359 77
rlm@359 78 ;;(map gabor grid)
rlm@359 79
rlm@359 80 ))
rlm@359 81
rlm@359 82
rlm@359 83 (defn show-kernel [kernel]
rlm@359 84 (let [output "/home/r/proj/cortex/tmp/kernel.png"]
rlm@359 85 (org.opencv.highgui.Highgui/imwrite output kernel)
rlm@359 86 (view (ImagePlus. output))))
rlm@359 87
rlm@359 88 (defn print-kernel [kernel]
rlm@359 89 (println (.dump kernel)))
rlm@359 90
rlm@359 91
rlm@359 92
rlm@359 93
rlm@359 94
rlm@359 95
rlm@359 96
rlm@359 97
rlm@359 98
rlm@359 99
rlm@357 100 (defn convolve-practice []
rlm@357 101 (let [input "/home/r/proj/cortex/images/dominos.jpg"
rlm@357 102
rlm@357 103
rlm@357 104 output "/home/r/ppp.png"
rlm@356 105
rlm@357 106 i (org.opencv.highgui.Highgui/imread input)
rlm@358 107
rlm@358 108 kernel (make-kernel)
rlm@358 109
rlm@358 110 new-mat (Mat.)
rlm@358 111
rlm@357 112 ]
rlm@356 113
rlm@358 114 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F (make-kernel))
rlm@358 115
rlm@358 116 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
rlm@358 117
rlm@357 118 (view (ImagePlus. input))
rlm@357 119 (view (ImagePlus. output))
rlm@358 120
rlm@357 121 ))
rlm@357 122
rlm@357 123
rlm@357 124
rlm@357 125
rlm@357 126
rlm@357 127
rlm@357 128
rlm@357 129 (comment
rlm@357 130 ;; these work
rlm@357 131 (def i (org.opencv.highgui.Highgui/imread
rlm@357 132 "/home/r/proj/cortex/images/dominos.jpg"))
rlm@357 133
rlm@357 134 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" i)
rlm@357 135 )
rlm@356 136 #+end_src
rlm@356 137
rlm@356 138
rlm@356 139
rlm@356 140 * COMMENT Generate Source
rlm@356 141 #+begin_src clojure :tangle ../src/cortex/gabor.clj
rlm@356 142 <<gabor>>
rlm@356 143 #+end_src