2010-09-28 34 views
4

我正在尋找一種方法來可視化在clojure編寫的模擬中更新的2d java數組,這與我將在matplotlib中使用imshow以可視化numpy數組的方式相同。相當於在Clojure中的imshow?

這樣做的最好方法是什麼?或者我可以將數組保存到磁盤並在matplotlib中將其可視化。最好的辦法是什麼?


下面是一個基於Java的代碼here我的嘗試,但使BufferedImage的很慢。有什麼辦法加速它:

(import 
'(java.awt Color Graphics Graphics2D Dimension GradientPaint BorderLayout) 
'(java.awt.image BufferedImage) 
'(javax.swing JPanel JFrame)) 

(def L 1024) 

(def image (BufferedImage. (* L 1) (* L 1) (. BufferedImage TYPE_INT_RGB))) 
(def g2 (. image createGraphics)) 

(defn get-color-map [] 
    (let [STEPS 100 
     colormap (BufferedImage. STEPS 1 (BufferedImage/TYPE_INT_RGB)) 
     g (.createGraphics colormap) 
     paint (GradientPaint. 0 0 (Color/RED) STEPS 0 (Color/GREEN)) 
     ] 
    (doto g 
     (.setPaint paint) 
     (.fillRect 0 0 STEPS 1)) 
    colormap)) 

(defn get-color [x start finish colormap] 
    (let [y (/ (- x start) (- finish start)) 
     STEPS 100] 
    (Color. (.getRGB colormap (int (* y STEPS)) 0)))) 

(defn fill-image [^"[[D" arr ^Graphics2D g sideX sideY ^BufferedImage colormap] 
    (dotimes [i (alength arr)] 
    (dotimes [j (alength ^"[D" (aget arr 0))] 
     (doto g 
     (.setColor (get-color (aget ^"[[D" arr (int i) (int j)) -10.0 10.0 colormap)) 
     (.fillRect (int (* i sideX)) (int (* j sideY)) sideX sideY))))) 


(def panel 
    (doto (proxy [JPanel] [] 
      (paintComponent [g] (.drawImage g image 0 0 nil))))) 

(def frame 
    (doto (JFrame. "Heat Map") 
     (.add panel BorderLayout/CENTER) 
     (.pack) 
     (.setLocationRelativeTo nil) 
     (.setVisible true))) 

而這裏是一個嘗試使用從incanter處理。這也是很慢:

(let [sktch (sketch 
      (setup [] 
        (doto this 
         ;no-loop 
         (size 1024 1024) 
         (framerate 15) 
         smooth)) 

       ;; define the draw function 
       (draw [] 
        (def A (gaussian-matrix 1024 0 1)) 
        (dotimes [i 1024] 
         (dotimes [j 1024] 
         (doto this 
          (stroke (int (abs (* (aget A i j) 255)))) 
          (point i j))))))] 

    (view sktch :size [1024 1024])) 
+0

我試過你的第一個代碼,它使一個黑色的矩形非常快*。 – 2014-04-04 21:50:50

+0

也鏈接被打破 – 2014-04-04 22:01:26

回答

0

它不是一個完全等效的,雖然你也許Pretty Printer Library將幫助您瞭解:

 
(pprint (for [x (range 10)] (range x)))   
(() 
(0) 
(0 1) 
(0 1 2) 
(0 1 2 3) 
(0 1 2 3 4) 
(0 1 2 3 4 5) 
(0 1 2 3 4 5 6) 
(0 1 2 3 4 5 6 7) 
(0 1 2 3 4 5 6 7 8)) 
nil 

這不是興田同等規模imshow(即純文本),所以我我真的認爲,這是你可能想使用的東西,直到你找到更漂亮的東西。

+0

我真的很想看到一個方形陣列,約100萬像素的時間在變化。但是,當我建立模擬時,這對我檢查我的函數輸出是有用的。謝謝。 – 2daaa 2010-09-28 17:52:43

2

使用Octave的java package將Java對象轉換爲Octave,然後調用Octave的imshow。