Clojure中与imshow等效的函数是什么?

4
我正在寻找一种方法来可视化在Clojure编写的模拟中被更新的2D Java数组,就像我使用Matplotlib中的imshow可视化Numpy数组一样。
最好的方法是什么?或者我可以将数组保存到磁盘中,并在Matplotlib中进行可视化。如何最好地处理这个问题?

这是基于Java代码 这里 的尝试,但导致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中的processing。它也非常慢:

(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]))

我尝试了你的第一个代码,它可以非常快速地生成一个黑色矩形。 - John Lawrence Aspden
链接已经失效。请提供新的链接。 - John Lawrence Aspden
2个回答

2
使用Octave的Java包将Java对象导入Octave,然后调用Octave的imshow函数。

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(仅限文本)不在同一级别,因此我只是建议您在找到更漂亮的东西之前使用它。


我真的很想可视化一个包含大约100万像素且随时间变化的方形数组。但是这对我来说在构建模拟时检查函数输出将非常有用。谢谢。 - 2daaa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接