comparison org/gabor.org @ 360:fc5bb270596a

completed kernel visualization code.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Mar 2013 03:12:25 +0000
parents 744ae7ef9b14
children 09461dce2e2f
comparison
equal deleted inserted replaced
359:744ae7ef9b14 360:fc5bb270596a
29 (defn make-kernel [] 29 (defn make-kernel []
30 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)] 30 (let [r (org.opencv.core.Mat. 5 5 CvType/CV_32F)]
31 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25)))) 31 (.put r 0 0 (float-array (map (fn [_] (rand)) (range 25))))
32 (println (.dump r)) 32 (println (.dump r))
33 33
34 ;;r 34 r
35 35
36 (org.opencv.core.MatOfFloat. (float-array [0.5])) 36
37 )) 37 ))
38 38
39 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset] 39 (defn gabor-kernel [sigma aspect-ratio theta wavelength phase-offset]
40 40
41 ;; first, find the size of the kernel which is required 41 ;; first, find the size of the kernel which is required
64 (for [y (reverse axis) x axis] (vector x y))) 64 (for [y (reverse axis) x axis] (vector x y)))
65 65
66 scale (reduce + (map gaussian grid)) 66 scale (reduce + (map gaussian grid))
67 67
68 gabor (fn [[x y :as coord]] 68 gabor (fn [[x y :as coord]]
69 (* (sinusoid coord) (gaussian coord) scale)) 69 (* (sinusoid coord) (gaussian coord) (/ scale)))
70 70
71 mat-width (+ 1 (* 2 half-width)) 71 mat-width (+ 1 (* 2 half-width))
72 mat (Mat. mat-width mat-width CvType/CV_32F)] 72 mat (Mat. mat-width mat-width CvType/CV_32F)]
73 73
74 74
79 79
80 )) 80 ))
81 81
82 82
83 (defn show-kernel [kernel] 83 (defn show-kernel [kernel]
84 (let [output "/home/r/proj/cortex/tmp/kernel.png"] 84 (let [output "/home/r/proj/cortex/tmp/kernel.png"
85 (org.opencv.highgui.Highgui/imwrite output kernel) 85 size (.size kernel)
86 (view (ImagePlus. output)))) 86 width (int (.width size))
87 height (int (.height size))
88 tmp-array (float-array (* width height))]
89
90 ;; read values from matrix.
91 (.get kernel 0 0 tmp-array)
92
93 ;; find overall dynamic range of the filter
94 (let [vals (vec tmp-array)
95 low (apply min vals)
96 high (apply max vals)
97 scaled-vals (map #(* 255 (- % low) (/ (- high low))) vals)
98 new-mat (Mat. height width CvType/CV_32F)]
99 (.put new-mat 0 0 (float-array scaled-vals))
100 (org.opencv.highgui.Highgui/imwrite output new-mat)
101 (view (ImagePlus. output)))))
87 102
88 (defn print-kernel [kernel] 103 (defn print-kernel [kernel]
89 (println (.dump kernel))) 104 (println (.dump kernel)))
90 105
91 106
92 107
93 108
94 109
95
96 110
97 111
98 112
99 113
100 (defn convolve-practice [] 114 (defn convolve-practice []
103 117
104 output "/home/r/ppp.png" 118 output "/home/r/ppp.png"
105 119
106 i (org.opencv.highgui.Highgui/imread input) 120 i (org.opencv.highgui.Highgui/imread input)
107 121
108 kernel (make-kernel) 122 kernel (gabor-kernel 10 1 (/ Math/PI 2) 10 0)
109 123
110 new-mat (Mat.) 124 new-mat (Mat.)
111 125
112 ] 126 ]
113 127
114 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F (make-kernel)) 128 (org.opencv.imgproc.Imgproc/filter2D i new-mat CvType/CV_32F kernel)
115 129
116 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat) 130 (org.opencv.highgui.Highgui/imwrite "/home/r/ppp.png" new-mat)
117 131
118 (view (ImagePlus. input)) 132 (view (ImagePlus. input))
119 (view (ImagePlus. output)) 133 (view (ImagePlus. output))