Mercurial > cortex
view 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 |
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)21 )23 (defn load-opencv24 "Load the opencv native library. Must be called before any OpenCV25 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 ;;r36 (org.opencv.core.MatOfFloat. (float-array [0.5]))37 ))39 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]41 ;; first, find the size of the kernel which is required42 (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/cos55 (+ (* 2 Math/PI (/ x' wavelength))56 phase-offset))))58 half-width (max59 (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 mat78 ;;(map gabor grid)80 ))83 (defn show-kernel [kernel]84 (let [output "/home/r/proj/cortex/tmp/kernel.png"]85 (org.opencv.highgui.Highgui/imwrite output kernel)86 (view (ImagePlus. output))))88 (defn print-kernel [kernel]89 (println (.dump kernel)))100 (defn convolve-practice []101 (let [input "/home/r/proj/cortex/images/dominos.jpg"104 output "/home/r/ppp.png"106 i (org.opencv.highgui.Highgui/imread input)108 kernel (make-kernel)110 new-mat (Mat.)112 ]114 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F (make-kernel))116 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)118 (view (ImagePlus. input))119 (view (ImagePlus. output))121 ))129 (comment130 ;; these work131 (def i (org.opencv.highgui.Highgui/imread132 "/home/r/proj/cortex/images/dominos.jpg"))134 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" i)135 )136 #+end_src140 * COMMENT Generate Source141 #+begin_src clojure :tangle ../src/cortex/gabor.clj142 <<gabor>>143 #+end_src