What's the best way to process an image in clojure?
Asked Answered
F

3

7

As part of a python simulation I have I take a 2d array and take the gradient of this array. This is done in scipy/numpy by convolving the 2d array with a filter with the appropriate weights.

So my question is if I want to do this in clojure reasonably fast does it make sense to do this in pure clojure, or is it better to use a Java image processing library and call into it from clojure?

Freestyle answered 2/9, 2010 at 17:57 Comment(0)
D
5

A well optimized loop in Clojure will have performance close to Java code.

I will advise using either the primitive vectors or directly arrays from clojure to get very good performance.

I have read a blogpost on the subject: http://www.bestinclass.dk/index.clj/2010/03/functional-fluid-dynamics-in-clojure.html

The good process is to start with code that is clear and works and tweek in for perfomance afterwards.

Often, performance in Clojure revolves around getting rid of reflection and using primitives. This is explained here: http://clojure.org/java_interop

Drosophila answered 3/9, 2010 at 12:37 Comment(0)
A
1

You can use Java image processing libraries from Clojure. Here is something to get started.

(defn getPixels [^BufferedImage image] (-> image .getRaster .getDataBuffer .getData))

I don't know if this will compile(some help I am) I just translated it from some Scala code of mine in my head.

http://clojure.org/java_interop

Amyotonia answered 2/9, 2010 at 18:57 Comment(1)
Thanks, but I'm really more interested in understanding the performance differences in using Java libraries vs writing pure Clojure.Freestyle
R
1

If you really want optimal performance, you're probably going to need to do your calculations in native code (which, as I understand it, is what NumPy does). We used JAI on a previous project, and it worked well, but we weren't using it for anything advanced. It does seem to have support for kernels and convolutions, so it might work for your purposes. However, I don't believe that it is as flexible as NumPy.

Raseda answered 8/9, 2010 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.