B4J Question Convert OpenCV MAT-object to ... a bytes array

peacemaker

Expert
Licensed User
Longtime User
HI, All

How to convert custom object (say, MAT object of jOpenCV lib) to a bytes array for sending by B4XSerialization ?
@JordiCP, maybe do you know as the lib author ?

It's for speed optimization:
1) the video frame is pre-processed into the MAT
2) object examples in the vocabulary are also to be pre-processed into the MATs, and to be saved into a DB
3) And it's needed p.1 is to be passed to the p.2 by network.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
SOLVED:

B4X:
Sub MatToJSON(myMat As OCVMat) As String
    Dim size As Long = myMat.cols * myMat.rows * myMat.elemSize
    Dim b(size) As Byte
    myMat.get(0, 0, b)
    Dim m As Map
    m.Initialize
    m.Put("rows", myMat.rows)
    m.Put("cols", myMat.cols)
    m.Put("type", myMat.type)
    Dim dataString As String = su.EncodeBase64(b)   'StringUtils
    m.Put("data", dataString)
    Dim jg As JSONGenerator
    jg.Initialize(m)
  
    Return jg.ToString
End Sub

Sub MatFromJSON(jsonMAT As String) As OCVMat
    Dim jp As JSONParser
    jp.Initialize(jsonMAT)
    Dim m As Map = jp.NextObject
    Dim cols As Int = m.Get("cols")
    Dim rows As Int = m.Get("rows")
    Dim typ As Int = m.Get("type")
    Dim dataString As String = m.Get("data").As(String)
    Dim b() As Byte = su.DecodeBase64(dataString)
    Dim myMat As OCVMat
    myMat.Initialize2(rows, cols, typ)
    myMat.put4(0, 0, b)

    Return myMat
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I like it

this structure is highly exportable and even allows you to run processing 'services' in the same or other server(s) just by changing the endpoint/port

perhaps, for the local approach, there is some unneeded overhead here (encode/decode base64 a large array of bytes, depending on your Mat dimensions). But if this overhead is not too much, it is a good solution.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I googled half a day, but not found a solution to save the mat into the byte array, together with the mat dimensions.
How can be encoded without Base64 ?

If to encode the keypoints as OCVMatOfKeyPoint and descriptions as MATs together.... it's more hard task...
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I googled half a day, but not found a solution to save the mat into the byte array, together with the mat dimensions.
I fear there's not a 'direct' way accessible with B4J --> A simple workaround to dump al the OCVMat info into a single array would be to assign, for instance, bytes in positions 0,1 to the number of cols. Bytes 2,3 could be rows, bytes 4,5 elemSize, and then all the OCVMat data...
B4X:
   Dim size As Long = myMat.cols * myMat.rows * myMat.elemSize
   Dim b(6+size) As Byte
   ...
You can use ByteConverter in order to manipulate the arrays like HERE.

Anyhow, now that you have a working approach to make your processing 'distributed', I would only focus in this part, if needed, when everything else has already been optimised.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks,@JordiCP
Question - what functions percent of OpenCV v.3.4 did you implement in the wrapper now? Not all ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…