comparison org/test-creature.org @ 92:e70ec4bba96b

saving progress...
author Robert McIntyre <rlm@mit.edu>
date Mon, 09 Jan 2012 06:05:29 -0700
parents 2bcc7636cfea
children 7b739503836a
comparison
equal deleted inserted replaced
91:2bcc7636cfea 92:e70ec4bba96b
376 (extend-type ImagePlus 376 (extend-type ImagePlus
377 Frame 377 Frame
378 (frame [image+] 378 (frame [image+]
379 (frame (.getBufferedImage image+)))) 379 (frame (.getBufferedImage image+))))
380 380
381 (defn rgb->int [r g b] 381 (def white -1)
382 (+ (bit-shift-left r 16)
383 (bit-shift-left g 8)
384 b))
385
386
387 382
388 (defn filter-pixels 383 (defn filter-pixels
389 "List the coordinates of all pixels matching pred." 384 "List the coordinates of all pixels matching pred."
385 {:author "Dylan Holmes"}
390 [pred #^ImageProcessor ip] 386 [pred #^ImageProcessor ip]
391 (let 387 (let
392 [width (.getWidth ip) 388 [width (.getWidth ip)
393 height (.getHeight ip)] 389 height (.getHeight ip)]
394 ((fn accumulate [x y matches] 390 ((fn accumulate [x y matches]
398 (pred (.getPixel ip x y)) 394 (pred (.getPixel ip x y))
399 (recur (inc x) y (conj matches (Vector2f. x y))) 395 (recur (inc x) y (conj matches (Vector2f. x y)))
400 :else (recur (inc x) y matches))) 396 :else (recur (inc x) y matches)))
401 0 0 []))) 397 0 0 [])))
402 398
403
404
405
406
407 (defn filter-pixels*
408 [pred #^ImageProcessor ip]
409 (let
410 [width (.getWidth ip)
411 height (.getHeight ip)
412 coords (ref [])
413 process
414 (fn [[start end]]
415 (loop [i start]
416 (if (<= i end)
417 (do
418 (let [column (rem i height)
419 row (unchecked-divide i width)]
420 (if (pred (.getPixel ip row column))
421 (dosync (ref-set
422 coords
423 (conj @coords (Vector2f. column row)))))
424
425 (recur (inc i)))))))
426 ]
427
428
429 (dorun
430 (pmap process (partition
431 2
432 (conj (vec (range 0 (* width height) 100))
433 (* width height)))))
434 @coords))
435
436
437
438 (comment
439 ((->
440 f
441 (partial x)
442 (partial y)
443 (partial z))))
444
445 (defn filter-pixels**
446 [pred #^ImageProcessor ip]
447 (let [width (.getWidth ip)
448 height (.getHeight ip)]
449 ((fn f [x1 x2 y1 y2]
450 (println x1)
451 (if
452 (and
453 (= x1 (dec x2))
454 (= y1 (dec y2)))
455 (if (pred (.getPixel ip x1 y1))
456 [[x1 y1]]
457 [])
458 (let
459 [xm (+ x1 (/ (- x2 x1) 2))
460 ym (+ y1 (/ (- y2 y1) 2))]
461 (apply concat
462 (pvalues
463 ;;(f x1 xm y1 ym)
464 ;;(f xm x2 y1 ym)
465 ;;(f x1 xm ym y2)
466 (f xm x2 ym y2))))))
467 0 width 0 height)))
468
469
470
471
472
473
474
475
476 (defn white-coordinates*
477 [#^ImageProcessor ip]
478 (filter-pixels** #(== % -1) ip))
479
480
481 (defn white-coordinates 399 (defn white-coordinates
482 "List the coordinates of all the white pixels in an image." 400 "List the coordinates of all the white pixels in an image."
483 [#^ImageProcessor ip] 401 [#^ImageProcessor ip]
484 (let [height (.getHeight ip) 402 (filter-pixels #(= % white) ip))
485 width (.getWidth ip)
486 coords (transient [])]
487 (dorun
488 (for [x (range width)
489 y (range height)]
490 (let [pixel (.getPixel ip x y)]
491 (if (= pixel -1)
492 (conj! coords (Vector2f. x y))))))
493 (persistent! coords)))
494
495
496
497
498
499
500 403
501
502 (def white {:r 255, :g 255, :b 255})
503 (def black {:r 0, :g 0, :b 0})
504
505
506 (defn same-side? [p1 p2 ref p] 404 (defn same-side? [p1 p2 ref p]
507 (<= 405 (<=
508 0 406 0
509 (.dot 407 (.dot
510 (.cross (.subtract p2 p1) (.subtract p p1)) 408 (.cross (.subtract p2 p1) (.subtract p p1))