Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 423
2daaa Avatar asked Sep 02 '10 17:09

2daaa


2 Answers

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

like image 126
Nicolas Oury Avatar answered Sep 19 '22 07:09

Nicolas Oury


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

like image 38
user405163 Avatar answered Sep 21 '22 07:09

user405163