hellonico / origami

Lowest barrier of entry to Image Processing, Computer Vision and Neural Networks on the JavaVM
https://hellonico.github.io/origami-docs/#/
Eclipse Public License 1.0
122 stars 7 forks source link

How to easily convert a mat object to a clojure vector? #32

Closed zendevil closed 4 years ago

zendevil commented 4 years ago

There doesn't seem to be an easy way to convert a mat object to a clojure vector for debugging. Also, for doing as something as multiplying a scalar with a mat, what does one do, this?:

(doseq [x (.width input)
            y  (.height input)
            :let [data1 (* 255.0 (get (.get mat (int x) (int y)) 0))]]
      (.put output
            (int x) (int y)
            (double-array [data1])))

Are there any good ways to do this?

hellonico commented 4 years ago

I completely missed this update ! Sorry, will answer very soon.

hellonico commented 4 years ago
; we start by creating a 3x3 mat, where each pixel value 
; is made of 1 channel, so CV_8UC1.
(def mat (new-mat 3 3 CV_8UC1))

; let's see what the inside of the mat looks like with dump
; dump just prints out the content, 
; and returns nil
(dump mat)
; [0 0 0]
; [0 0 0]
; [0 0 0]
; nil

; we can retrieve the content as byte array using << on the mat
(<< mat)
; #object["[B" 0x33a36d5d "[B@33a36d5d"]

; it's hard to read what the byte array is, 
; so in clojure we can make a sequence out of it.
(seq (<< mat))
; (0 0 0 0 0 0 0 0 0)

; let's replace the value in the mat, 
; by using the function >>, 
; that puts values inside the mat 
; using a byte array, here created from a range.
(>> mat (byte-array (range 9)))

; we can check the values again using seq and <<
(seq (<< mat))
; (0 1 2 3 4 5 6 7 8)

; to multiply the content of the mat, 
; you can use, multiply! and a scalar.
(multiply! mat (new-scalar 2.0))

; we can check the values again using seq and <<, 
; and see that each value of the mat, 
; has been multiplied by 2
(seq (<< mat))
; (0 2 4 6 8 10 12 14 16)
hellonico commented 4 years ago

Added a jupyter notebook with the above code:

https://github.com/hellonico/origami-fun/blob/master/jupyter/simple_mats.ipynb

hellonico commented 4 years ago

probably good, so closing since one month opened. Open a new ticket if you need some more info !