Mercurial > cortex
view 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 |
line wrap: on
line source
1 #+title: Fun with Gabor Filters2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: gabor filters in clojure with opencv5 #+keywords: computer vision, jMonkeyEngine3, clojure, opencv6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org8 #+babel: :mkdirp yes :noweb yes :exports both12 #+name: gabor13 #+begin_src clojure14 (ns cortex.gabor15 (: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-opencv23 "Load the opencv native library. Must be called before any OpenCV24 stuff is used."25 []26 (clojure.lang.RT/loadLibrary "opencv_java249"))28 (defn make-kernel []29 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)]30 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25))))31 (println (.dump r))33 r36 ))38 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]40 ;; first, find the size of the kernel which is required41 (let [square #(expt % 2)42 rotated (fn [[x y]]43 [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))44 (- (* y (Math/cos theta)) (* x (Math/sin theta)))])46 gaussian (fn [[x y]]47 (let [[x' y'] (rotated [x y])]48 (Math/exp (- (/ (+ (square x')49 (square (* aspect-ratio y')))50 (* 2 (square sigma)))))))51 sinusoid (fn [[x y]]52 (let [[x' y'] (rotated [x y])]53 (Math/cos54 (+ (* 2 Math/PI (/ x' wavelength))55 phase-offset))))57 half-width58 (let [std-dev-capture 5]59 (max60 (int (* std-dev-capture (/ sigma aspect-ratio)))61 (int (* std-dev-capture sigma))62 (int (* std-dev-capture (/ aspect-ratio sigma)))))64 grid (let [axis (range (- half-width) (inc half-width))]65 (for [y (reverse axis) x axis] (vector x y)))67 scale (reduce + (map gaussian grid))69 gabor (fn [[x y :as coord]]70 (* (sinusoid coord) (gaussian coord) (/ scale)))72 mat-width (+ 1 (* 2 half-width))73 mat (Mat. mat-width mat-width CvType/CV_32F)]75 (.put mat 0 0 (float-array (map gabor grid)))76 mat))79 (defn draw-kernel! [kernel img-path]80 (let [output img-path81 size (.size kernel)82 width (int (.width size))83 height (int (.height size))84 tmp-array (float-array (* width height))]86 ;; read values from matrix.87 (.get kernel 0 0 tmp-array)89 ;; find overall dynamic range of the filter90 (let [vals (vec tmp-array)91 low (apply min vals)92 high (apply max vals)93 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)94 new-mat (Mat. height width CvType/CV_32F)]95 (.put new-mat 0 0 (float-array scaled-vals))96 (org.opencv.highgui.Highgui/imwrite output new-mat))))98 (defn show-kernel [kernel]99 (let [img-path "/home/r/proj/cortex/tmp/kernel.png"]100 (draw-kernel kernel img-path)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))))141 (comment142 ;; these work143 (def i (org.opencv.highgui.Highgui/imread144 "/home/r/proj/cortex/images/dominos.jpg"))146 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" i)147 )148 #+end_src152 * COMMENT Generate Source153 #+begin_src clojure :tangle ../src/cortex/gabor.clj154 <<gabor>>155 #+end_src