cnuernber / dtype-next

A Clojure library designed to aid in the implementation of high performance algorithms and systems.
Other
319 stars 18 forks source link

tensor to BufferedImage function #92

Closed daslu closed 4 months ago

daslu commented 6 months ago

link to Zulip topic

Often, when working with images, I find it useful to convert them to dtype-next tensors using tech.v3.libs.buffered-image/as-ubyte-tensor, then process them as tensors, then convert them back to images.

To convert back tensors to images, some helper function is usually handy, something like the following (thanks to @harold's adaptation).

(require '[tech.v3.libs.buffered-image :as bufimg]
         '[tech.v3.datatype :as dtype]
         '[tech.v3.tensor :as tensor]
         '[tech.v3.datatype.functional :as fun])

(defn tensor->image
  [tensor img-type]
  (let [[h w] (dtype/shape tensor)
        new-img (bufimg/new-image h w img-type)]
    (dtype/copy! tensor
                 (tensor/ensure-tensor new-img))
    new-img))

An image processing example using tensors:

(def raw-image
  (bufimg/load "https://upload.wikimedia.org/wikipedia/commons/1/1e/Gay_head_cliffs_MV.JPG"))

raw-image

image

(-> raw-image   
    bufimg/as-ubyte-tensor
    (fun/* 0.3) ;; make it a bit darker
    (tensor->image :byte-bgr))

image

Would it make sense to add such a tensor->image function to dtype-next?

(I added it to another library (the WIP Noj library), but then realized it should probably belong to dtype-next.)

harold commented 6 months ago

I think this is quite cool - definitely similar things strewn about that could use this.

After thinking a bit more today nice documentation (and possibly a nice default value?) for the type parameter might be nice.

harold commented 6 months ago

@cnuernber - in case you missed this. (:

cnuernber commented 6 months ago

I think this is great - I think it should go into the libs/buffered-image namespace as that has all the constants involved. There could be default constants for 1 channel, 3 channel, and 4 channel tensors respectively.

Related to this is colorizing tensors.