diff src/rlm/vision.clj @ 0:78a630e650d2

initial import
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 00:57:08 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/rlm/vision.clj	Tue Oct 18 00:57:08 2011 -0700
     1.3 @@ -0,0 +1,116 @@
     1.4 +(ns rlm.vision
     1.5 +  (:use [clojure.contrib def trace])
     1.6 +  (:use [rlm image-utils])
     1.7 +  (:use [cantor])
     1.8 +  (:import java.awt.image.BufferedImage
     1.9 +	   	   [ij ImagePlus IJ]
    1.10 +
    1.11 +	   ))
    1.12 +
    1.13 +(defn select-last [& args]
    1.14 +  (last args))
    1.15 +  
    1.16 +
    1.17 +;this is to finally get these stupid color problems out of the way!
    1.18 +
    1.19 +(defmulti gen-color (fn [& args]	
    1.20 +	      (class (last args))))
    1.21 +
    1.22 +
    1.23 +
    1.24 +
    1.25 +(defmethod gen-color (class 5)
    1.26 +  ([r g b]
    1.27 +     (gen-color (vector r g b 255)))
    1.28 +  ([r g b a]
    1.29 +     (gen-color (vector r g b a))))
    1.30 +
    1.31 +(defmethod gen-color (class :black)
    1.32 +  [key]
    1.33 +  (get {:black (gen-color {:r 0 :g 0 :b 0})
    1.34 +	:white (gen-color {:r 255 :g 255 :b 255})}
    1.35 +       key
    1.36 +       (gen-color {:r 0 :g 0 :b 0})))
    1.37 +
    1.38 +(defmethod gen-color (class [5 10 255 50])
    1.39 +  [[r g b a]]
    1.40 +  (gen-color {:r r :g g :b b :a a}))
    1.41 +
    1.42 +(defmethod gen-color (class {:r 5 :g 10 :b 255 :a 50})
    1.43 +  [{:keys [r g b a] :or {r 0 g 0 b 0 a 255}}]
    1.44 +  (let [r (int (rem r 256))
    1.45 +	g (int (rem g 256))
    1.46 +	b (int (rem b 256))
    1.47 +	a (int (rem a 256))
    1.48 +	]
    1.49 +    (+
    1.50 +     (bit-shift-left a 24)
    1.51 +     (bit-shift-left r 16)
    1.52 +     (bit-shift-left g  8)
    1.53 +     b)))
    1.54 +
    1.55 +(def color (memoize gen-color))
    1.56 +
    1.57 +  
    1.58 +
    1.59 +
    1.60 +(defn black-lattice [x y]
    1.61 +  (if (and
    1.62 +      (< (rem y 500) 10)
    1.63 +      (< (rem x 500) 10)) (color :black) (color :white)))
    1.64 +
    1.65 +
    1.66 +
    1.67 +(defn snake-problem-2 [x y]
    1.68 +  (let [E (+ 4 (* 3 (java.lang.Math/cos x)) (java.lang.Math/cos (* 3 x)))
    1.69 +	E (int (* E 255/9))]
    1.70 +    
    1.71 +    (color E E E)))
    1.72 +
    1.73 +
    1.74 +
    1.75 +(defn function->image [[xmin xmax] [ymin ymax] f] 
    1.76 +  (let [width (- xmax xmin)
    1.77 +	height (- ymax ymin)
    1.78 +	image (BufferedImage. width height BufferedImage/TYPE_INT_RGB)]
    1.79 +
    1.80 +    (doall 
    1.81 +    (for [y (range ymin ymax) x (range xmin xmax)]
    1.82 +      (.setRGB image (- x xmin) (- y ymin) (f x y))))
    1.83 +
    1.84 +    (ImagePlus. "functionally-generated" image)))
    1.85 +
    1.86 +(defn better-function->image [[xmin xmax] [ymin ymax] f] 
    1.87 +  
    1.88 +  (let [width (- xmax xmin)
    1.89 +	height (- ymax ymin)
    1.90 +	image (BufferedImage. width height BufferedImage/TYPE_INT_RGB)
    1.91 +	set-color (fn [[x y]] (.setRGB image (- x xmin) (- y ymin) (f x y)))
    1.92 +	domain (for [x (range xmin xmax) y (range ymin ymax)] [x y])]
    1.93 +	
    1.94 +    (dorun (pmap set-color domain)) 
    1.95 +    (ImagePlus. "functionally-generated" image)))
    1.96 +
    1.97 +
    1.98 +
    1.99 +
   1.100 +
   1.101 +(def a-face (normalize (cross
   1.102 +	     (vec3 1 0 0)
   1.103 +	     (sub
   1.104 +	      (vec3 0 0 (/ (* 2 (expt 3 1/2))))
   1.105 +	      (vec3 (/ (- (sqrt 3)) 2) (- 1/2) 0)))))
   1.106 +
   1.107 +(def c-face (mul  (normalize (cross
   1.108 +	     (vec3 (/ (sqrt 3) 2) 3/2 0)
   1.109 +	     (sub
   1.110 +	      (vec3 0 0 (/ (* 2 (expt 3 1/2))))
   1.111 +	      (vec3 (/ (- (sqrt 3)) 2)    (- 1/2)  0)))) -1))
   1.112 +
   1.113 +
   1.114 +(def b-face (mul (normalize
   1.115 +	     (cross
   1.116 +	      (vec3 (/ (sqrt 3) 2) (- 3/2) 0)
   1.117 +	      (sub
   1.118 +	       (vec3 0 0 (/ (* 2 (expt 3 1/2))))
   1.119 +	       (vec3 (/ (sqrt 3) 2) (- 1/2) 0)))) -1))