changeset 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
files org/gabor.org
diffstat 1 files changed, 62 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/gabor.org	Wed Mar 06 18:50:35 2013 +0000
     1.2 +++ b/org/gabor.org	Thu Mar 07 02:37:49 2013 +0000
     1.3 @@ -33,9 +33,70 @@
     1.4  
     1.5      ;;r
     1.6  
     1.7 -    (org.opencv.core.MatOfFloat. (float-array [1]))
     1.8 +    (org.opencv.core.MatOfFloat. (float-array [0.5]))
     1.9      ))
    1.10  
    1.11 +(defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
    1.12 +  
    1.13 +  ;; first, find the size of the kernel which is required
    1.14 +  (let [square #(expt % 2)
    1.15 +        rotated (fn [[x y]]
    1.16 +                  [(+ (* x (Math/cos theta)) (* y (Math/sin theta)))
    1.17 +                   (- (* y (Math/cos theta))  (* x (Math/sin theta)))])
    1.18 +                             
    1.19 +        gaussian (fn [[x y]]
    1.20 +                   (let [[x' y'] (rotated [x y])]
    1.21 +                     (Math/exp (- (/ (+ (square x')
    1.22 +                                        (square (* aspect-ratio y')))
    1.23 +                                   (* 2 (square sigma)))))))
    1.24 +        sinusoid (fn [[x y]]
    1.25 +                   (let [[x' y'] (rotated [x y])]
    1.26 +                     (Math/cos
    1.27 +                      (+ (* 2 Math/PI (/ x' wavelength))
    1.28 +                         phase-offset))))
    1.29 +
    1.30 +        half-width (max
    1.31 +                    (int (* 5 (/ sigma aspect-ratio)))
    1.32 +                    (int (* 5 sigma))
    1.33 +                    (int (* 5 (/ aspect-ratio sigma))))
    1.34 +
    1.35 +        grid (let [axis (range (- half-width) (inc half-width))]
    1.36 +               (for [y (reverse axis) x axis] (vector x y)))
    1.37 +
    1.38 +        scale (reduce + (map gaussian grid))
    1.39 +        
    1.40 +        gabor (fn [[x y :as coord]]
    1.41 +                (* (sinusoid coord) (gaussian coord) scale))
    1.42 +
    1.43 +        mat-width (+ 1 (* 2 half-width))
    1.44 +        mat (Mat. mat-width mat-width CvType/CV_32F)]
    1.45 +
    1.46 +    
    1.47 +    (.put mat 0 0 (float-array (map gabor grid)))
    1.48 +    mat
    1.49 +
    1.50 +    ;;(map gabor grid)
    1.51 +    
    1.52 +    ))
    1.53 +
    1.54 +
    1.55 +(defn show-kernel [kernel]
    1.56 +  (let [output "/home/r/proj/cortex/tmp/kernel.png"]
    1.57 +    (org.opencv.highgui.Highgui/imwrite output kernel)
    1.58 +    (view (ImagePlus. output))))
    1.59 +
    1.60 +(defn print-kernel [kernel]
    1.61 +  (println (.dump kernel)))
    1.62 +                           
    1.63 +
    1.64 +        
    1.65 +                      
    1.66 +        
    1.67 +        
    1.68 +        
    1.69 +  
    1.70 +
    1.71 +
    1.72  (defn convolve-practice []
    1.73    (let [input "/home/r/proj/cortex/images/dominos.jpg"
    1.74