Scala Breeze: can you create a DenseMatrix of Int Array elements?

798 Views Asked by At

I just found Scala Breeze as a high performance linear algebra library for Scala applications.

I wonder if there is a way to initialize a DenseMatrix with an Int Array as element unit.

This is the OpenCV functionality i am trying to port to Breeze:

val rgb_raw = Array[Byte] (....) //ByteArray RGB values dim 480x360
val rgb_mat = new Mat (360, 480, CvType.CV_8UC3)
rgb_mat.put(0,0,rgb_raw)

I wish it was something as easy as:

val rgb_mat = new DenseMatrix(360,480, rgb_raw)

or

val rgb_mat = new DenseMatrix[Array[Int,Int,Int]](360,480,rgb_raw)

I haven't found anything in the documentation that points me in the right direction.

1

There are 1 best solutions below

0
On

This works for me

import breeze.linalg.DenseMatrix

val range = 0 to 360*480

val arr = range map(_=>0.toByte)).toArray

new DenseMatrix(360,480, arr)

res27: breeze.linalg.DenseMatrix[Byte] = 
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  ... (480 total)

new DenseMatrix(360,480, range.toArray)

res28: breeze.linalg.DenseMatrix[Int] = 
0   360  720  1080  1440  1800  2160  2520  2880  3240  3600  ... (480 total)

val arrr = range.map(_=>arr).toArray

new DenseMatrix(360,480, arrr)

res31: breeze.linalg.DenseMatrix[Array[Byte]] = 
[B@30276fff  [B@30276fff  [B@30276fff  [B@30276fff  ... (480 total)